我无法让我的程序从文件中读取并填充我的数组。
这是我的StudentAccount
类,其中包含我的数组。
数组需要能够从文本文件中填充
并使用Student
类添加Scanner
。
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Student {
// attributes
private StudentAccount[] list; // to hold the accounts
private int numStudents;
// to keep track of the number of accounts in the list
// methods
// the constructor
public Student() {
// size array with parameter
list = new StudentAccount[20];
numStudents = 0;
}
// helper method to find the index of a specified account
private int search(String studentNameIn) {
for (int i = 0; i < numStudents; i++) {
StudentAccount tempAccount = list[i];
// find the account at index i
String tempName = tempAccount.getStudentName();
// get account number
if (tempName.equals(studentNameIn))
// if this is the account we are looking for
{
return i; // return the index
}
}
return -999;
}
// return the total number of accounts in the list
public int getTotal() {
return numStudents;
}
// check if the list is empty
public boolean isEmpty() {
if (numStudents == 0) {
return true; // list is empty
} else {
return false; // list is not empty
}
}
// check if the list is full
public boolean isFull() {
if (numStudents == list.length) {
return true; // list is full
} else {
return false; // list is empty
}
}
// add an item to the array
public boolean add(StudentAccount accountIn) {
if (!isFull()) // check if list is full
{
list[numStudents] = accountIn; // add item
numStudents++; // increment total
return true; // indicate success
} else {
return false; // indicate failure
}
}
// return an account at a particular place in the list
public StudentAccount getItem(int positionIn) {
if (positionIn < 1 || positionIn > numStudents) {
return null; // indicate invalid index
} else {
return list[positionIn - 1];
}
}
// return an account with a particular account number
public StudentAccount getItem(String studentNameIn) {
int index;
index = search(studentNameIn);
if (index == -999) {
return null; // indicate invalid index
} else {
return list[index];
}
}
// remove an account
public boolean remove(String numberIn) {
int index = search(numberIn); // find index of account
if (index == -999) // if no such account
{
return false; // remove was unsuccessful
} else { // overwrite items by shifting other items along
for (int i = index; i <= numStudents - 2; i++) {
list[i] = list[i + 1];
}
numStudents--; // decrement total number of accounts
return true; // remove was successful
}
}
static void writeToFile() {
}
static void readFromFile() {
try {
FileInputStream fstream = new FileInputStream("Students.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
}
in.close();
} catch (Exception e) {
System.out.println("File Not Found");
}
}
}
这是我的Student
课程:
public class StudentAccount {
private String studentName;
private String studentDOB;
private String studentAddress;
private String studentGender;
public final static String MALE = "m";
public final static String FEMALE = "f";
public StudentAccount(String nameIn, String dobIn,
String addressIn, String genderIn) {
this.studentName = nameIn;
this.studentDOB = dobIn;
this.studentAddress = addressIn;
this.studentGender = genderIn;
}
public String getStudentName() {
return studentName;
}
public String getStudentDOB() {
return studentDOB;
}
public String getStudentAddress() {
return studentAddress;
}
public String getStudentGender() {
return studentGender;
}
public static boolean validateGender(String genderIn) {
return (genderIn.toLowerCase().compareTo(MALE) == 0)
|| (genderIn.toLowerCase().compareTo(FEMALE) == 0);
}
}
最后这是我运行程序的主要类。
readFromFile
的方法
我需要它一次读取一行文件并将每行保存为数组中的一个学生记录。它从文件读入,但不会在数组中放入任何数据。
public class EnrolmentRegister {
private static String lecturer = "John Smyth";
public static void main(String[] args) {
char choice;
Student newStudent = new Student();
Student.readFromFile();
do {
System.out.println();
System.out.println("1. Add A New Student Account");
System.out.println("2. Remove A Student Account");
System.out.println("3. Get Course Details ");
System.out.println("4. Quit");
System.out.println();
System.out.println("Please Choose One Of The Options Above");
choice = InputScanner.nextChar();
System.out.println();
switch (choice) {
case '1':
option1(newStudent);
break;
case '2':
option2(newStudent);
break;
case '3':
option3(newStudent);
break;
case '4':
break;
default:
System.out.println("Invalid entry");
}
} while (choice != '4');
}
static void option1(Student StudentIn) {
String gender;
System.out.print("Enter Student Name: ");
String studentName = InputScanner.nextString();
System.out.print("Enter Student Date Of Birth: ");
String studentDOB = InputScanner.nextString();
System.out.print("Enter Student Address: ");
String studentAddress = InputScanner.nextString();
do {
System.out.print("Enter Student Gender m/f: ");
gender = InputScanner.nextString();
} while (!StudentAccount.validateGender(gender));
// create new account
StudentAccount account =
new StudentAccount(studentName, studentDOB,
studentAddress, gender);
// add account to list
boolean ok = StudentIn.add(account);
if (!ok) {
System.out.println("The list is full");
} else {
System.out.println("Account created");
}
}
static void option2(Student StudentIn) {
// get account number of account to remove
System.out.print("Enter Student Name: ");
String studentName = InputScanner.nextString();
// delete item if it exists
boolean ok = StudentIn.remove(studentName);
if (!ok) {
System.out.println("No such Student Name");
} else {
System.out.println("Account removed");
}
}
static void option3(Student StudentIn) {
System.out.println("COM 180, " + lecturer);
}
static void option4(Student StudentIn) {
}
static void option5(Student StudentIn) {
}
}
/*When I call the readFromFile method and print it I get each
line of text from the text file but don't know how
to put each line from the file into the array. This is
homework and been at this for days. I know its probably
something simple but I cant work it out. Any help would
be greatly appreciated. Thanks*/
答案 0 :(得分:1)
我没有看到您将InputScanner
声明为扫描仪:
Scanner InputScanner = new Scanner(new File("fileNameGoesHere"));
然而,您在main
中多次调用它:
choice = InputScanner.nextChar();
String studentName = InputScanner.nextString();
注意:不要大写变量名称,因为它可能与对象类型混淆。