文件花名册: roster.txt
Reagan rebradshaw835
Ryley rbarber894
Peyton pstott885
Tyrese tmayo945
Caius ccharlton329该程序必须处理异常处理。我目前正在努力克服的问题是能够获得适当的输出。我不知道是否有一种特定的方法可以找到包含名称的行,并返回行中除名称之外的所有内容。这样我们就可以返回分配的用户名。
例如,第一行显示:
Reagan rebradshaw835方法仅返回rebradshaw835
下面是代码:(更新代码) vhthanh真的帮助我理解了如何读取文件!我决定使用替换字符串命令来编辑代码。如果没有vhthanhI,可能不会学习如何与文件交互。
package chapter11;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
public class LabProgram {
public static String findID(String studentName, Scanner infoScnr) throws Exception {
while (infoScnr.hasNextLine()) {
String line = infoScnr.nextLine();
//String[] values = line.split(" ", 2);
if (line.contains(studentName)) {
line = line.replace(studentName + " ", "");
return line;
}
}
throw new Exception ("Student ID not found for " + studentName);
}
public static String findName(String studentID, Scanner infoScnr) throws Exception {
while (infoScnr.hasNextLine()) {
String line = infoScnr.nextLine();
//String[] values = line.split(" ", 2);
if (line.contains(studentID)) {
line = line.replace(studentID + " ", "");
return line;
}
}
throw new Exception ("Student name not found for " + studentID);
}
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
String studentName;
String studentID;
String studentInfoFileName;
FileInputStream studentInfoStream = null;
Scanner studentInfoScanner = null;
// Read the text file name from user
studentInfoFileName = scnr.next();
// Open the text file
studentInfoStream = new FileInputStream(studentInfoFileName);
studentInfoScanner = new Scanner(studentInfoStream);
// Read search option from user. 0: findID(), 1: findName()
int userChoice = scnr.nextInt();
// FIXME: findID() and findName() may throw an Exception.
// Insert a try/catch statement to catch the exception and output the exception message.
try {
if (userChoice == 0) {
studentName = scnr.next();
studentID = findID(studentName, studentInfoScanner);
System.out.println(studentID);
}
else {
studentID = scnr.next();
studentName = findName(studentID, studentInfoScanner);
System.out.println(studentName);
}
studentInfoStream.close();
}
catch (Exception exp) {
System.out.println(exp.getMessage());
}
}
}发布于 2021-11-21 04:07:27
您需要从Scanner中读取每一行,并将字符串split()到一个包含2个值的数组中,然后进行比较。尝试以下代码:
public static String findID(String studentName, Scanner infoScnr) throws Exception {
while (infoScnr.hasNextLine()) {
String line = infoScnr.nextLine();
String[] values = line.split(" ", 2);
if (studentName != null && studentName.equals(values[0])) {
return values[1];
}
}
throw new Exception ("Student name not found for " + studentName);
}
public static String findName(String studentID, Scanner infoScnr) throws Exception {
while (infoScnr.hasNextLine()) {
String line = infoScnr.nextLine();
String[] values = line.split(" ", 2);
if (studentID != null && values.length > 1 && studentID.equals(values[1])) {
return values[0];
}
}
throw new Exception ("Student id not found for " + studentID);
}发布于 2021-11-21 05:56:09
.split (" ")对于间距
.split ("/n")用于新行(nextLine)
https://stackoverflow.com/questions/70051669
复制相似问题