2017-09-19 48 views
0

我試圖提示用戶輸入他們想要寫入的文件的名稱,創建該.txt文件,然後編寫文本的限定行進入該文件並保存。在這段時間內,它似乎跳過了用戶輸入的文件名稱,他們想要保存,循環回去,然後得到一個FileNotFoundException,它甚至不應該尋找一個文件。我在創建文件時遇到了FileNotFound異常

import java.util.*; 
import java.io.*; 

public class Main { 

    public static void main(String[] args) { 
     Scanner user = new Scanner(System.in); 
     Scanner docInName = null; 
     PrintWriter docOutName = null; 

     do { 
      System.out.println("Please enter the filename of the file you 
           would like to read from: "); 
      try { 
       docInName = new Scanner(new File(user.nextLine())); 
      } catch (FileNotFoundException e) { 
       System.out.println("File not found!"); 
      } 
     } while (docInName == null); 

     int lineNum = docInName.nextInt(); 
     BikePart[] bp = new BikePart[lineNum]; 
     System.out.println("please enter the max cost for a part: "); 
     int cost = user.nextInt(); 

     do { 
      System.out.println("please enter a name for the file to write to 
           (end with .txt): "); 
      String out = user.nextLine(); //PROBLEM HERE! SKIPS USER INPUT 
      try { 
       docOutName = new PrintWriter(out); 
       for (int i = 0; i < lineNum; i++) { 
        String line = docInName.nextLine(); 
        String[] elements = line.split(","); 
        bp[i] = new BikePart(elements[0], 
          Integer.parseInt(elements[1]), 
          Double.parseDouble(elements[2]), 
          Double.parseDouble(elements[3]), 
          Boolean.parseBoolean(elements[4])); 
        double temp = Double.parseDouble(elements[3]); 
        if ((temp < cost && bp[i].isOnSale() == true) 
          || (bp[i].getListPrice() < cost && 
           bp[i].isOnSale() == false)) { 
         docOutName.write(line); 
        } 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } while (docOutName == null); 
     user.close(); 
    } 

} 
+2

的可能的複製[掃描儀被使用next()之後跳過nextLine(),nextInt()或其它nextFoo()?(https://stackoverflow.com/questions/13102045/scanner-is-skipping -nextline-after-next-nextint-or-other-nextfoo) –

+0

實際上恰恰相反,我需要在調用最大成本線後再跳過一行。正確的主意,謝謝! –

回答

1

我只需要在循環開始前跳過一行。

import java.util.*; 
import java.io.*; 

public class Main { 

    public static void main(String[] args) { 
     Scanner user = new Scanner(System.in); 
     Scanner docInName = null; 
     PrintWriter docOutName = null; 

     do { 
      System.out.println("Please enter the filename of the file you would like to read from: "); 
      try { 
       docInName = new Scanner(new File(user.nextLine())); 
      } catch (FileNotFoundException e) { 
       System.out.println("File not found!"); 
      } 
     } while (docInName == null); 

     int lineNum = docInName.nextInt(); 
     BikePart[] bp = new BikePart[lineNum]; 
     System.out.println("please enter the max cost for a part: "); 
     int cost = user.nextInt(); 
     user.nextLine(); //SOLUTION HERE 

     do { 
      System.out.println("please enter a name for the file to write to (end with .txt): "); 
      String out = user.nextLine(); 
      try { 
       docOutName = new PrintWriter(out); 
       for (int i = 0; i < lineNum; i++) { 
        String line = docInName.nextLine(); 
        String[] elements = line.split(","); 
        bp[i] = new BikePart(elements[0], Integer.parseInt(elements[1]), Double.parseDouble(elements[2]), 
          Double.parseDouble(elements[3]), Boolean.parseBoolean(elements[4])); 
        double temp = Double.parseDouble(elements[3]); 
        if ((temp < cost && bp[i].isOnSale() == true) 
          || (bp[i].getListPrice() < cost && bp[i].isOnSale() == false)) { 
         docOutName.write(line); 
        } 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } while (docOutName == null); 
     user.close(); 
    } 

} 
相關問題