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();
}
}
の可能性のある重複[スキャナ)は(次を使用した後nextLine()はスキップされ、nextInt()または他のnextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping -nextline-after-using-next-nextint-or-other-nextfoo) –
実際はちょうど反対でした、私は最大コストラインを呼び出した後にもう1行をスキップする必要がありました。しかし、右のアイデア、ありがとう! –