【问题标题】:I'm getting FileNotFound exception while trying to create a file我在尝试创建文件时遇到 FileNotFound 异常
【发布时间】:2023-03-23 21:53:02
【问题描述】:

我正在尝试提示用户输入他们想要写入的文件的名称,创建该 .txt 文件,然后将符合条件的文本行写入该文件并保存。在 do while 中,它似乎跳过了用户输入他们想要保存到的文件的名称,循环返回,然后得到 FileNotFoundException,它甚至不应该在寻找文件。

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

}

【问题讨论】:

标签: java file-io java.util.scanner printwriter


【解决方案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();
    }

}

【讨论】:

    猜你喜欢
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2011-10-16
    • 2010-10-30
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多