2014-10-13 3 views
0

내 프로그램의 요지는 파일에서 일부 명령을 읽은 다음 명령으로 판단하여 특정 작업을 수행한다는 것입니다. 내 문제는 내 코드가 txt 문서의 모든 행을 읽지 않고 있다는 것입니다. 특히, 이전에 input = "P"인지 확인하기 위해 테스트하는 if 문 내부에 print 문을 입력하여 테스트 한 첫 번째 네 줄을 읽습니다. 어떤 도움이라도 대단히 감사 할 것입니다. 여기 프로그램에서 파일의 모든 데이터를 읽지 못합니다

뿐만 아니라

P e1 10 5 

P e2 19 5 

P e3 11 5 

P e4 18 5 

R mouth 10 10 10 2 

S nose 14 7 2 

P p3 9 9 

P p4 20 9 

D 

Y bad command 

M p3 9 12 

M p4 20 12 

D 



public static void main(String[] args) { 

      String filename = args[0]; 
      File file = new File(filename); 

      Scanner input=null; 

      try { 

       input=new Scanner(file); 

      } 
      catch(java.io.FileNotFoundException ex) { 
       System.out.println("ERROR: Couldn't open file: " + file); 
       System.exit(1); 
      } 

      AsciiDisplay asciiDisplay= new AsciiDisplay(); 

      while(input.hasNext()) { 

       if(input.next().equals("P")) { 

        String id=input.next(); 
        int x=input.nextInt(); 
        int y=input.nextInt(); 
        Coordinate coordinate=new Coordinate(x,y); 
        Point point=new Point(id,coordinate); 
        asciiDisplay.addShape(point); 

       } 

       else if(input.next().equals("R")) { 
        String id=input.next(); 
        int x=input.nextInt(); 
        int y=input.nextInt(); 
        int length=input.nextInt(); 
        int height=input.nextInt(); 
        Coordinate coordinate= new Coordinate(x,y); 
        Rectangle rectangle= new Rectangle(id,coordinate,length,height); 
        asciiDisplay.addShape(rectangle); 

       } 

       else if(input.next().equals("S")) { 

        String id=input.next(); 
        int x=input.nextInt(); 
        int y=input.nextInt(); 
        int size=input.nextInt(); 
        Coordinate coordinate= new Coordinate(x,y); 
        Square square = new Square(id,coordinate,size); 
        asciiDisplay.addShape(square); 

       } 

       else if(input.next().equals("M")) { 

        String id=input.next(); 
        int x=input.nextInt(); 
        int y=input.nextInt(); 
        Coordinate coordinate= new Coordinate(x,y); 
        asciiDisplay.moveShape(id,coordinate); 

       } 

       else if(input.next().equals("E")) { 

        asciiDisplay.deleteAll(); 

       } 

       else if(input.next().equals("D")) { 
        asciiDisplay.updateGrid(); 
        asciiDisplay.printGrid(); 

       } 
       else if(input.next().equals("X")) { 
        System.out.println("Invalid command: X"); 


       } 


      } 
    } 
} 
+1

그래서 디버거에서 무엇을 말하고 있습니까? 디버거가 없습니까? 프로그램 흐름과 변수 내용을보기 위해 추가 한 print 문은 어떻습니까? – John3136

+0

@JarrodRoberson 아마도 속은 사람 일 테지만, 그 사람은 아닙니다. – chrylis

+2

이것은 지정된 질문과 중복되지 않습니다. 완전히 다른 대답이 있습니다. OP는 전체 내용을 문자열로 읽으려고하지 않습니다. 그들은 조금씩 처리하려고합니다. –

답변

2

귀하의 문제가 반복적으로 입력 떨어져 토큰을 당겨 유지하는 당신의 if 문에서 input.next()를 호출한다는 것이다 TXT 문서입니다.

String nextCommand = input.next(); 

다른 방법으로, 당신은 스위치를 사용할 수 있습니다 :

switch(input.next()) { 
case "P": 
    // do stuff 
    break; 
case "R": 
    // do other stuff 
    break; 
} 
1

을 당신은 너무 input.next()를 호출하는 대신, 한 번 호출하고 변수에 값을 지정해야합니다, 그 변수를 확인 여기 자주 :

if (input.next().equals("P")) { 
    ... 
} 
else if(input.next().equals("R")) { 
    ... 
} 
else if(input.next().equals("S")) { 
    ... 
} 
... 

S 검사 년대 무렵, 그것은 세 토큰을 읽을 수 있어요.

String token = input.next(); 
if (token.equals("P")) { 
    ... 
} else if (token.equals("R")) { 
    ... 
} else if (token.equals("S")) { 
    ... 
} ... 

이렇게하면 토큰을 한 번 읽은 다음이를 처리 할 수있는 올바른 코드 조각을 찾습니다.

switch (input.next()) { 
    case "P": { 
     ... 
     break; 
    } 
    case "R": { 
     ... 
     break; 
    } 
    case "S": { 
     ... 
     break; 
    } 
} 

(당신은하지 않습니다 여기에 괄호를 가지고을 가지고 있지만 블록 이 아닌 것을 제공 : 자바 7의로, 당신은 또한 switch 문을 사용하여이 작업을 수행 할 수

주 사소한 일이지만 블록 당 새로운 범위 인 을 도입하는 것이 유용 할 것입니다. 코드 을 별도의 방법으로 분리하는 것을 고려해보십시오.)

관련 문제