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

我已经弄清楚如何将文本文件中的内容读取、存储和显示到数组中,但我的问题是当用户按下数字时仅输出特定行..例如,如果用户输入 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);


    }


慕田峪9158850
浏览 112回答 2
2回答

扬帆大鱼

建议:不要使用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.&nbsp; &nbsp;&nbsp;try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {&nbsp; &nbsp; String line;&nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; // String[] myArray = {};&nbsp; &nbsp; while ((line = reader.readLine().trim()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; if (line.equals("")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; if (counter == userChoice) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // myArray = line.split("\\s{0,},\\s{0,}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // System.out.println(Arrays.toString(myArray).replaceAll("[,\\[\\]]", ""));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If you want to use an Array then un-comment the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 3 lines above and comment the line below.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(line.replace(", ", " "));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}catch (FileNotFoundException ex) {&nbsp; &nbsp; System.err.println(ex.getMessage());}catch (IOException ex) {&nbsp; &nbsp; System.err.println(ex.getMessage());}

陪伴而非守候

&nbsp;int&nbsp;userChoice&nbsp;=&nbsp;in.nextInt();&nbsp; &nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;userChoice;&nbsp;i++)&nbsp;{我认为你不需要for循环。只需使用userChoise像[userChoise-1].
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java