我有这个包含各种信息的文本文件到目前为止我已经能够阅读除地址详细信息之外的所有内容,因为它们用空格分隔并且包含数字和字母,所以我有点不知道该怎么做我已经尝试将其作为 int 读取,但我知道它不起作用。
这是我的代码:
public void methodName() {
File vData = new File("src/volunteer_data.txt"); // Create file object
try { // try catch to handle exception
Scanner readFile = new Scanner(vData); // Create scanner object
while (readFile.hasNextLine()) { //
int volunteerID = readFile.nextInt(); // Read volunteerID as an int
String vName = readFile.nextLine(); // Read volunteer name as string
// DATATYPE address = readFile.nextSOMETHING(); // Read address as ....
String contact = readFile.nextLine(); // Read contact number as a string
}
readFile.close(); // Close scanner
} catch(FileNotFoundException e) { // Throw exception and stop program if error found
e.printStackTrace();
}
这是我正在阅读的文本文件。它是制表符分隔的:
VolunteerID Name Address Contact
050 John 24 Willow Street 905-747-0876
042 Emily 362 Sunset Avenue 905-323-1234
013 Alice 16 Wonderland Street 905-678-0987
071 Arthur 36 York Road 905-242-5643
060 Daniel 125 Ottawa Street 905-666-3290
055 Peppa 64 Great Britain Blvd 905-212-4365
024 Sean 909 Green Avenue 905-232-5445
077 Kim 678 Grape Garden 905-080-7641
098 Patrick 126 Oxford Street 905-099-9535
092 Laura 45 Mill Street 905-244-0086
008 Gary 84 California Street 905-767-3456