2014-10-20 2 views
0

현재 switch 문에 문제가 있습니다. 나는 스위치 변수를 출력했지만 올바른 것으로 보이지만 원하는 스위치에 대한 print 문이 작동하지 않습니다. switch 문은 맨 아래에서 ~ 20 행에 있습니다.자바 문자열 스위치 문이 적절한 스위치가되지 않습니다

"Person#Joe Gaucho#28000 Marguerite Parkway#949-582-4800#[email protected];" 
"Faculty#1234153524#1341244#1234150000#Adjunct;Employee#bgs210#20000#123455000;" 
"Person#Mickey Mouse#Disneyland#800 1Disney#[email protected];" 
"Exit#" 

예상 출력은 다음과 같습니다 :

테스트 스텁은

Person  name: Joe Gaucho  address: 28000 Marguerite Parkway  phoneNumber: 949-582-4800  emailAddress: [email protected] 
Faculty OfficeHours: Wed Jan 14 22:49:13 PST 1970, Wed Dec 31 16:22:21 PST 1969, Wed Jan 14 22:49:10 PST 1970 rank: Adjunct 
Employee  office: bgs210 salary: 20000.0 dateHired: Fri Jan 02 02:17:35 PST 1970 
Person name: Mickey Mouse  address: Disneyland  phoneNumber: 800 1DisneyemailAddress: [email protected] 

그리고 내 출력은 다음과 같습니다

import java.util.Scanner; 
public class Exercise10_2M { 
    static class Person{ 
     String name; 
     String address; 
     String phoneNumber; 
     String emailAddress; 

     Person(){} 
     Person(String name, String address, String phoneNumber, String email){ 
      this.name = name; 
      this.address = address; 
      this.phoneNumber = phoneNumber; 
      this.emailAddress = email; 
     } 

     @Override 
     public String toString(){ 
      return "Person\tname: " + name 
        + "\taddress: " + address 
        + "\tphoneNumber: " + phoneNumber 
        + "\temailAddress: " + emailAddress + "\n"; 
     } 
    } 
    static class Student extends Person{ 
     String classStatus; // freshmen, sophomore, sophomore+, junior, or senior 

     Student(){} 
     Student(/*String str1, String str2, String str3, String str4,*/ String str5){ 
      //super(str1, str2, str3, str4); 
      classStatus = str5; 
     } 
     @Override 
     public String toString(){ 
      return "Student\tclassStatus: "+ classStatus + "\n" /*+ super.toString()*/; 
     } 
    } 
    static class Employee extends Person{ 
     String office; 
     double salary; 
     long dateHired; 
     Employee(){} 
     Employee(String str, double doub, long long1){ 
      office = str; 
      salary = doub; 
      dateHired = long1; 
     } 
     @Override 
     public String toString(){ 
      return "Employee\tnoffice: " + office 
        + "\tsalary: " + salary 
        + "\tdateHired: " + dateHired + "\n"; 
     } 
    } 
    static class Faculty extends Employee{ 
     long[] officeHours; 
     String rank;  // either adjunct, assistantProfessor, or Professor 

     Faculty(){} 
     // !!!! FIX 

     /*Faculty(long long1, String str){ 
      officeHours = long1; 
      rank = str; 
     }*/ 

     @Override 
     public String toString(){ 
      return "Faculty\tofficeHours: " //+ officeHours[0] 
        + "\trank: " + rank + "\n"; 
     } 
    } 
    static class Staff extends Employee{ 
     String title; 

     Staff(){} 
     Staff(String str){ 
      title = str; 
     } 
     @Override 
     public String toString(){ 
      return "Staff\ttitle: " + title + "\n"; 
     } 
    } 
    public static void main(String[] args){ 
     Scanner firstScan = new Scanner(System.in); // Scanner 
     String firstInput;       // Parses from scanner 
     String[] myInput;       // Parses from firstInput 
     Person myClass = new Person();    // Class instance 
     String typeOfPerson = new String();   // Holds value for which class 
     boolean LCV = true; 

     firstScan.useDelimiter(";"); 

     do{ 
      firstInput = firstScan.next(); 
      myInput = firstInput.split("#"); 
      typeOfPerson = myInput[0]; 

      switch(typeOfPerson){ 
       case "Person": myClass = new Person(myInput[1], myInput[2], 
                 myInput[3], myInput[4]); 
              break; 
       case "Student": myClass = new Student(/*scan.next(), scan.next(), 
                 scan.next(), scan.next(),*/ 
                 myInput[1]); 
              break; 
       case "Employee": myClass = new Employee(myInput[1],    
Double.parseDouble(myInput[2]), Long.parseLong(myInput[3])); 
              break; 
       case "Faculty": System.out.println("check"); 
        //myClass = new Faculty(); 
              break; 
       case "Staff": myClass = new Staff(myInput[1]); 
              break; 
       case "Exit": LCV = false; 
            break; 
      } 
      if(LCV == true){ 
       System.out.print(myClass.toString()); 
      } 
     }while(firstScan.hasNext() && LCV == true); 
    } 
} 
:

Person name: Joe Gaucho address: 28000 Marguerite Parkway phoneNumber: 949-582-4800 emailAddress: [email protected] 
Person name: Joe Gaucho address: 28000 Marguerite Parkway phoneNumber: 949-582-4800 emailAddress: [email protected] 
Employee noffice: bgs210 salary: 20000.0 dateHired: 123455000 
Employee noffice: bgs210 salary: 20000.0 dateHired: 123455000 

이 내 코드입니다

+0

나는 당신의 테스트 스텁이'''을 포함하여 파싱중인 하나의 큰 문자열이라고 생각하고있다.? – christopher

+0

문제를 일으키지 않았지만 모든'정적 '클래스가있는 이유는 무엇입니까? –

+0

예. 모두 큰 문자열입니다. 따옴표가 포함되어 있지 않지만 세미콜론 구분 기호 때문에 스택 오버플로가 발생하지 않았습니다. – user3090014

답변

0

myClass의 값을 각 루프가 끝날 때 일부 기본값 (일반적으로 null)으로 재설정해야합니다.

  • 그것은 유형 Person
  • 그것은 새로운 Person 객체 myClass를 업데이트의 입력을 찾습니다 때문에이 반복 출력을 받고 있습니다. 루프 상단에서 진행
  • 는 ERGO Person 남아, 새로운 Faculty
  • myClass가 덮어 쓰기되지 않습니다 발견하고 그것을 다시 출력된다.

나는이 동작을 this IDEOne에 복제했습니다.

관련 문제