2017-10-19 4 views
0

다음 두 가지 버전의 동일한 메소드가 있습니다. 첫 번째는 JOptionPane을 사용하고 두 번째 것은 콘솔을 사용합니다. 사용자 입력에서 파일 경로 (문자열)를 가져 와서 파일을 읽고 읽어야합니다..csv 파일을 읽지 못했습니다. JOptionPane에서 콘솔 I/O로 전환 한 후 FileNotFoundException 오류 발생

첫 번째 방법은 정상적으로 작동하지만 콘솔을 사용하는 두 번째 방법은 FileNotFoundException 오류가 발생합니다. 둘 모두 거의 동일하면 두 번째 작품이 왜 작동하지 않습니까?

// USING JOptionPane ///////////////////////////////////////////////////////////////// ///////////////////////

공개 무효 AddFromFile을() {

String[] option1 = { "Go back to Main Manu", "Continue" }; 

    int choice4 = JOptionPane.showOptionDialog(null, 
      "Warning: this method will delete existing data before it add file data", "Monster Database", 
      JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option1, null); 

    if (choice4 == 0) 
     monitor(); 

    if (choice4 == 1) { 

     monsterAttackList.clear(); 

     Scanner input = new Scanner(System.in); 
     String filePath = JOptionPane.showInputDialog(null, "Enter a filepath"); 

     File inFile = new File(filePath); 

     try { 
      Scanner fileReader = new Scanner(inFile); 

      String line; 
      String[] part; 
      int attackID; 
      String monster; 
      String date; 
      String location; 
      String reporter; 

      while (fileReader.hasNextLine()) { 
       line = fileReader.nextLine(); 
       part = line.split(","); 
       attackID = Integer.parseInt(part[0]); 
       monster = part[1]; 
       date = part[2]; 
       location = part[3]; 
       reporter = part[4]; 

       monsterAttackList.add(new MonsterAttack(attackID, monster, date, location, reporter)); 
      } 

      fileReader.close(); // Close to unlock. 
      input.close(); 

     } catch (IOException e) { 
      JOptionPane.showMessageDialog(null, "filepath is not valid"); 
      System.err.println(e); 

     } 
    } 

} 

// SCANNER 를 사용 ///// /////////////////////////////////////////////////////////////////// /////////////////////

public void addFromFile() { 

    System.out.println("Warning: this method will delete existing data before it add file data"); 
    System.out.println("\n1. Go back to Main Manu" + "\n2. Continue \n"); 

    Scanner input = new Scanner(System.in); 
    int choice4 = input.nextInt(); 


    if (choice4 == 1) 
     monitor(); 

    if (choice4 == 2) { 

     monsterAttackList.clear(); 

     System.out.println("Enter a filepath"); 

     String filePath = input.nextLine(); 

     File inFile = new File(filePath); 

     try { 
      Scanner fileReader = new Scanner(inFile); 

      String line; 
      String[] part; 
      int attackID; 
      String monster; 
      String date; 
      String location; 
      String reporter; 

      while (fileReader.hasNextLine()) { 
       line = fileReader.nextLine(); 
       part = line.split(","); 
       attackID = Integer.parseInt(part[0]); 
       monster = part[1]; 
       date = part[2]; 
       location = part[3]; 
       reporter = part[4]; 

       monsterAttackList.add(new MonsterAttack(attackID, monster, date, location, reporter)); 
      } 

      fileReader.close(); // Close to unlock. 
      input.close(); 

     } catch (IOException e) { 

      System.out.println("filepath is not valid"); 
      System.err.println(e); 

     } 
    } 

} 

답변

0

input.nextLine()input.nextInt()으로 전화를 한 후 NextInt는 줄 바꿈 문자를 사용하지 않고 숫자 만 사용합니다.

+0

스캐너 입력 = 새 Scanner (System.in); \t \t int choice4 = input.nextInt(); \t \t if (choice4 == 1) \t \t \t monitor(); \t \t if (choice4 == 2) { \t \t \t input.nextLine(); // free line \t \t \t monsterAttackList.clear(); \t \t \t System.out.println ("파일 경로 입력"); \t \t \t 문자열 filePath = input.nextLine(); \t \t \t 파일 inFile = 새 파일 (filePath); – Neo

+0

^코드 블록처럼 보이게하려면 어떻게해야합니까? 도와 줘서 고마워하지만 여전히 같은 오류가 발생합니다. – Neo