내 프로그램의 요지는 파일에서 일부 명령을 읽은 다음 명령으로 판단하여 특정 작업을 수행한다는 것입니다. 내 문제는 내 코드가 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");
}
}
}
}
그래서 디버거에서 무엇을 말하고 있습니까? 디버거가 없습니까? 프로그램 흐름과 변수 내용을보기 위해 추가 한 print 문은 어떻습니까? – John3136
@JarrodRoberson 아마도 속은 사람 일 테지만, 그 사람은 아닙니다. – chrylis
이것은 지정된 질문과 중복되지 않습니다. 완전히 다른 대답이 있습니다. OP는 전체 내용을 문자열로 읽으려고하지 않습니다. 그들은 조금씩 처리하려고합니다. –