我已经弄清楚如何将文本文件中的内容读取、存储和显示到数组中,但我的问题是当用户按下数字时仅输出特定行..例如,如果用户输入 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);
}
扬帆大鱼
陪伴而非守候
相关分类