2017-09-19 5 views
0

J'essaie d'inviter l'utilisateur à entrer le nom d'un fichier dans lequel il aimerait écrire, à créer ce fichier .txt, puis à écrire les lignes de texte qualifiées dans ce fichier et enregistrez-le. À l'intérieur du do, il semble que l'utilisateur saute par-dessus le nom du fichier dans lequel il souhaite enregistrer, reboucle et obtient une exception FileNotFoundException, et ne cherche même pas un fichier.Je reçois une exception FileNotFound en essayant de créer un fichier

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

double possible de [Scanner saute nextLine() après l'utilisation suivante(), nextInt() ou autre nextFoo()?] (Https://stackoverflow.com/questions/13102045/scanner-is-skipping -nextline-after-using-next-nextint-ou-autre-nextfoo) –

+0

C'était en fait juste le contraire, j'avais besoin de sauter une ligne de plus après avoir appelé la ligne de coût maximum. bonne idée cependant, merci! –

Répondre

1

Je n'avais besoin que de passer une ligne avant le début de la boucle.

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(); 
    } 

}