텍스트 파일을 읽을 때 오류가 발생하는 것 같습니다. 이 프로그램은 줄을 읽고 첫 번째 문자를 확인한 다음 if 문에서 관련 코드를 실행해야합니다. 이 프로그램은 첫 번째 줄을 제대로 실행하고 내용을 출력하지만 다음 줄을 처리 할 수는 없습니다. 여기에 사용하여 코드 메신저입니다 :파일을 읽을 때 내 프로그램에서 오류가 발생하는 이유는 무엇입니까?
public void importStart(){
try {
FileReader fr = new FileReader("src/data.txt");
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
Scanner scan = null;
while(line != null){
scan = new Scanner(line);
String string1 = scan.next();
inputType = string1.charAt(0);
if(inputType == 'S'){
foxCount = scan.nextInt();
rabbitCount = scan.nextInt();
dragonCount = scan.nextInt();
System.out.println(inputType + " "+ foxCount + " "+ rabbitCount + " "+ dragonCount);
}
else if(inputType == 'X'){
System.out.println("Test 1");
animalType = string1.substring(2, 3);
System.out.println("Test 2");
if(animalType == "F"){
step = scan.nextInt();
}
else if(animalType == "R"){
step = scan.nextInt();
}
else if(animalType == "D"){
step = scan.nextInt();
}
System.out.println(inputType + " "+ animalType + " " + step);
}
line = reader.readLine();
}
reader.close();
}
과 내가이 오류를
주의를 받고, 첫 번째 줄이 있어야 출력이 내가 제대로 첫 번째 줄에 운영하고 방법을 알고있다. "Test 1"도 올바르게 나타나서 문제는 내 string1.substring 구현에 있다고 생각합니다. 이게 문제 야?
S 74 199 15
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
Test 1
at java.lang.String.substring(Unknown Source)
at DataDisplayGui.importStart(DataDisplayGui.java:107)
at DataDisplayGui.actionPerformed(DataDisplayGui.java:178)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
문자 2에서 시작이 오류를 얻을 것이라고? –
줄 107에 중단 점을 넣으십시오. string1은 2 자 미만입니다. –