为了账号安全,请及时绑定邮箱和手机立即绑定

尝试使用 Array 而不是 ArrayList 从具有用户输入的文本文件中读取特定行

尝试使用 Array 而不是 ArrayList 从具有用户输入的文本文件中读取特定行

慕田峪9158850 2023-08-16 18:23:10
我已经弄清楚如何将文本文件中的内容读取、存储和显示到数组中,但我的问题是当用户按下数字时仅输出特定行..例如,如果用户输入 3,它应该显示文本文件第三行中的所有内容..这是我迄今为止所拥有的内容的片段。文本文件将包含 john、doe、johndoe@gmail.com、10101、blue、342.21 等内容。谢谢!public static void readLine(Record[] objects) throws FileNotFoundException {    Scanner in = new Scanner(System.in);    File fileobject = new File(filename);    Scanner input = new Scanner(fileobject);        String[] array;        System.out.println("Enter the record you would like to see: ");        int userChoice = in.nextInt();        for (int i = 0; i < userChoice; i++) {                objects[i] = new Record();                array = input.nextLine().replaceAll(" ", "").split(",");                objects[i].firstname = array[0];                String name = array[0];                objects[i].lastname = array[1];                String lastname = array[1];                objects[i].email = array[2];                String email = array[2];                objects[i].idnumber = Double.parseDouble(array[3]);                double idnumber = Double.parseDouble(array[3]);                objects[i].color = array[4];                String color = array[4];                objects[i].balance = Double.parseDouble(array[5]);                double balance = Double.parseDouble(array[5]);                passThis(name, lastname, email, idnumber, color, balance, userChoice);        }          }    public static void passThis(String firstname, String lastname, String email, double idnumber, String color, double balance, int userChoice) {           System.out.println(firstname + " " + " " + lastname + " " + email + " " + (int)idnumber + " " + color + " " + balance);    }
查看完整描述

2 回答

?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

建议:不要使用Scanner来读取文件。它实际上很慢,但很可能适合您的用例。您可能想尝试以下方法:


Scanner in = new Scanner(System.in);

System.out.println("Enter the record you would like to see: ");

int userChoice = in.nextInt();


// Try With Resources is used here to auto-close the reader.    

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {

    String line;

    int counter = 0;

    // String[] myArray = {};

    while ((line = reader.readLine().trim()) != null) {

        if (line.equals("")) {

            continue;

        }

        counter++;

        if (counter == userChoice) {

            // myArray = line.split("\\s{0,},\\s{0,}");

            // System.out.println(Arrays.toString(myArray).replaceAll("[,\\[\\]]", ""));

            // If you want to use an Array then un-comment the 

            // 3 lines above and comment the line below.

            System.out.println(line.replace(", ", " "));

            break;

        }

    }

}

catch (FileNotFoundException ex) {

    System.err.println(ex.getMessage());

}

catch (IOException ex) {

    System.err.println(ex.getMessage());

}


查看完整回答
反对 回复 2023-08-16
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

 int userChoice = in.nextInt(); 
 for (int i = 0; i < userChoice; i++) {

我认为你不需要for循环。只需使用userChoise[userChoise-1].


查看完整回答
反对 回复 2023-08-16
  • 2 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信