Java 尝试使用Array not ArrayList从用户输入的文本文件中读取特定行

Java 尝试使用Array not ArrayList从用户输入的文本文件中读取特定行,java,arrays,text-files,Java,Arrays,Text Files,我已经知道如何将文本文件中的内容读取、存储和显示到数组中,但我的问题是,当用户按下一个数字时,只能输出一行特定的内容。。e、 g.如果用户输入3,则应在文本文件的第三行显示所有内容。。这是我到目前为止的一个片段。文本文件将包含类似john,doe,johndoe@gmail.com,10101,蓝色,342.21。谢谢 public static void readLine(Record[] objects) throws FileNotFoundException { Scanne

我已经知道如何将文本文件中的内容读取、存储和显示到数组中,但我的问题是,当用户按下一个数字时,只能输出一行特定的内容。。e、 g.如果用户输入3,则应在文本文件的第三行显示所有内容。。这是我到目前为止的一个片段。文本文件将包含类似john,doe,johndoe@gmail.com,10101,蓝色,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);

    }
publicstaticvoidreadline(记录[]对象)
抛出FileNotFoundException{
扫描仪输入=新扫描仪(系统输入);
File fileobject=新文件(文件名);
扫描仪输入=新扫描仪(fileobject);
字符串[]数组;
System.out.println(“输入您希望看到的记录:”);
int userChoice=in.nextInt();
for(int i=0;i
int userChoice=in.nextInt();
for(int i=0;i

我认为你不需要
来进行
循环。只需使用
userChoise
就像
[userChoise-1]
建议:不要使用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());
}
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());
}