2017-09-26 21 views
1

스위치 케이스가있는 인수 파일을 Stack에 스캔하고 있는데 .nextDouble 명령으로 값을 건너 뛰고 있습니까? ^ 2 - 3/2 6 * 8 + 2.5 3Java 스캐너 nextDouble 명령은 스위치 케이스로 값을 건너 뛰고 있습니까?

그러나, 스캐너는 수집 : ^ 2 - 3/6 * 8 + 3

while (stackScanner.hasNextLine()) {   

    switch(stackScanner.next()) { 

    case"+": { 
     operator= new operationNode("+"); 
     stack.push(operator);} 

    case"-":{ 
     operator= new operationNode("-"); 
     stack.push(operator);} 

    case"*":{ 
     operator= new operationNode("*"); 
     stack.push(operator);} 

    case"/":{ 
     operator= new operationNode("/"); 
     stack.push(operator);} 

    case"^":{ 
     operator= new operationNode("^"); 
     stack.push(operator);} 

    while(stackScanner.hasNextDouble()) { 
     stack.push(new numberNode(stackScanner.nextDouble())); 
    } 
} 

문제는 인수 파일에는 다음이 포함되어 여기 마지막 줄에 있습니다

여기에 내 코드 조각입니다.

그래서 여기에 쌍으로 나오는 첫 번째 숫자는 건너 뜁니다 (2 및 2.5).

while 루프의 끝에 stackScanner.next();을 추가하면 저장되는 숫자는 2와 2.5가됩니다.

+0

사례에 휴식이 없다는 사실과 while 루프가 switch 문 안에 있다는 것을 알고 계셨습니까? –

+0

@MauricePerry 기본값으로 사용했지만 특정 값을 읽지 못했습니다. 또한 휴식은 내 결과에 영향을 미치지 않는 것 같습니다 (?) – Gege

+0

정말 코드를 게시 했습니까? 복사하여 붙여 넣을 때 내가보고있는 결과가 표시되지 않습니다. 특히, 스택은''^, 2.0, -, *, /, ^, 3.0, /, ^, 2.0, 6.0, *, /, ^, 8.0, +, -, 2.5, 3.0]'@ MauricePerry가'break' 문이 없다는 관찰과 일치합니다. – DaveyDaveDave

답변

1

코드를 복사하고 operationNodenumberNode 클래스를 구현보다는 Stack<String>를 사용하여 약간의 수정은, I는 다음과 같은 작품이 (내가 생각하는) 당신이 기대하는 것을 발견 :이다

public static void main(String... args) { 
    Scanner stackScanner = new Scanner("^ 2 - 3/2 6 * 8 + 2.5 3"); 

    Stack<String> stack = new Stack<>(); 

    while (stackScanner.hasNextLine()) { 

     switch (stackScanner.next()) { 
      case "+": { 
       stack.push("+"); 
       break; 
      } 

      case "-": { 
       stack.push("-"); 
       break; 
      } 

      case "*": { 
       stack.push("*"); 
       break; 
      } 

      case "/": { 
       stack.push("/"); 
       break; 
      } 

      case "^": { 
       stack.push("^"); 
       break; 
      } 
     } 

     while (stackScanner.hasNextDouble()) { 
      stack.push(Double.toString(stackScanner.nextDouble())); 
     } 
    } 

    System.out.println(stack); 
} 

, 나는했습니다 break; 문이 필요하지 않은 것 같습니다 (아마도 JVM의 차이 일까?) while 루프를 switch 외부로 이동했습니다.

0

당신은 또한 double를 확인하는 방법을 쓸 필요가 :,

while (stackScanner.hasNextLine()) { 
    String nextToken = stackScanner.next(); 
    switch(nextToken) { 

    case"+": { 
     System.out.println("+"); 
     break; 
     } 

    case"-":{ 
     System.out.println("-"); 
     break; 
    } 

    case"*":{ 
     System.out.println("*"); 
     break; 
    } 

    case"/":{ 
     System.out.println("/"); 
     break; 
    } 

    case"^":{ 
     System.out.println("^"); 
     break; 
    } 

    default: 
     if(isDouble(nextToken)){ 
      //Do something 
     } 
     break; 
    } 
} 

을 예컨대을 whileswitch 포장 및 default 블록에 double의 처리를 이동해야합니다.

private boolean isDouble(String number){ 
    try{ 
     Double.parseDouble(number); 
     return true; 
    }Catch(Exception e){ 
     return false; 
    } 
} 
+0

죄송합니다 - 이것이 실제로 올바르지 않다고 생각합니다. 이렇게하면 한 줄에 하나의 토큰 만 처리됩니다. 입력이 "^ 2 - 3/2 6 * 8 + 2.5 3"한 줄이면 "^"를 처리 한 다음 중지합니다. – DaveyDaveDave