我正在嘗試製作一個程序,該程序需要學生ID號,姓名和班級成績。我希望能夠讀取數據後。但是當我嘗試讀取數據(或打開文件)時出現錯誤。當我運行程序時,出現「Error opening file」錯誤。我有5個班,並將附加他們。有人可以試圖幫助我,並找出錯誤。我會附上類:我想打開我的文本文件,但我不知道問題是什麼
public class StudentRecords
{
private int IDnumber;
private String firstName;
private String lastName;
private double grade;
public StudentRecords()
{
this(0, "", "", 0.0);
}
public StudentRecords(int id, String first, String last, double gr)
{
setIDnumber(id);
setFirstName(first);
setLastName(last);
setGrade(gr);
}
public void setIDnumber(int id)
{
IDnumber = id;
}
public int getIDnumber()
{
return IDnumber;
}
public void setFirstName(String first)
{
firstName = first;
}
public String getFirstName()
}
return firstName;
}
public void setLastName(String last)
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setGrade(double gr)
{
grade = gr;
}
public double getGrade()
{
return grade;
}
}
這裏是第二類
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class StudentTextFile
{
private Formatter output;
public void openFile()
{
try
{
output = new Formatter("students.txt");
}
catch (SecurityException securityException)
{
System.err.println(
"You do not have write access to this file.");
System.exit(1);
}
catch (FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening or creating file.");
System.exit(1);
}
}
public void addStudentRecords()
{
StudentRecords record = new StudentRecords();
Scanner input = new Scanner(System.in);
System.out.printf("%s\n%s\n%s\n%s\n\n",
"To terminate input, type the end-of-file indicator",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <crtl> z then press enter");
System.out.printf("%s\n%s",
"Enter student ID number (> 0), first name, last name and grade. ",
"? ");
while (input.hasNext())
{
try
{
record.setIDnumber(input.nextInt());
record.setFirstName(input.next());
record.setLastName(input.next());
record.setGrade(input.nextDouble());
if (record.getIDnumber() > 0)
{
output.format("%d %s %s %.2f\n", record.getIDnumber(),
record.getFirstName(), record.getLastName(),
record.getGrade());
}
else
{
System.out.println(
"Student ID Number must be greater than 0.");
}
}
catch (FormatterClosedException formatterClosedException)
{
System.err.println("Error writing to file.");
return;
}
catch (NoSuchElementException elementException)
{
System.err.println("Invalid input. Please try again.");
input.nextLine();
}
System.out.printf("%s %s \n%s", "Enter student ID number (>0),",
"first name. last name and grade.", "? ");
}
}
public void closeFile()
{
if (output != null)
output.close();
}
}
這裏是第三類
public class StudentTextFileTest {
public static void main(String[] args)
{
StudentTextFile application = new StudentTextFile();
application.openFile();
application.addStudentRecords();
application.closeFile();
}
}
這裏是第四類
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ReadStudentTextFile
{
private Scanner input;
public void openfile()
{
try
{
input = new Scanner(new File("studentrecords.txt"));
}
catch (FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening file.");
System.exit(1);
}
}
public void readStudentRecords()
{
StudentRecords record = new StudentRecords();
System.out.printf("%-10s%-12s%-12s%10s\n", "Student ID Number",
"First Name", "Last Name", "Balance");
try
{
while (input.hasNext())
{
record.setIDnumber(input.nextInt());
record.setFirstName(input.next());
record.setLastName(input.next());
record.setGrade(input.nextDouble());
System.out.printf("%-10d%-12s%-12s%10.2f\n",
record.getIDnumber(), record.getFirstName(),
record.getLastName(), record.getGrade());
}
}
catch (NoSuchElementException elementException)
{
System.err.println("File improperly formed. ");
input.close();
System.exit(1);
}
catch (IllegalStateException stateException)
{
System.err.println(" Error reading from file.");
System.exit(1);
}
}
public void closeFile()
{
if (input!= null)
input.close();
}
}
這裏是第五類
public class ReadStudentTextFileTest
{
public static void main(String[] args)
{
ReadStudentTextFile application = new ReadStudentTextFile();
application.openfile();
application.readStudentRecords();
application.closeFile();
}
}
該文件是否存在,您確定您的文件路徑正確嗎? – PakkuDon
當發生'FileNotFoundException'時,您自己的代碼會打印該字符串。 – keyser
您已經發布了很多與您的問題無關的代碼。嘗試縮小問題的位置。本文應該幫助你:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – SimonC