Reputation: 1
I can't get my program to read from a file and populate my array.
This is my StudentAccount
class which contains my array.
The array needs to be able to populate from a text file
and also add a Student
by using the Scanner
class.
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");
}
}
}
This is my Student
class:
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);
}
}
Finally this is my main class which runs my program.
The method for readFromFile
I need it to read the file one line at a time and save each line as one student record in the array. It reads in from file but doesn't put any data in the array.
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*/
Upvotes: 0
Views: 79
Reputation: 1041
I do not see you declare InputScanner
anywhere as scanner:
Scanner InputScanner = new Scanner(new File("fileNameGoesHere"));
Yet you call it multiple times in main
:
choice = InputScanner.nextChar();
String studentName = InputScanner.nextString();
Note: do not capitalize variable names since it may be confused with Object types.
Upvotes: 1