When I run this code it gives me the same output for each Record. there are 390 different lines in the text file that is being read. each line is then parsed and put into the record. I have tested when the object is first created and everything is entered the data is fine. On the second call (in another for loop) it returns all the same data for r.returnSSN(); . I have gone over this to many times I can't seem to find the problem.
*******************************************************
Record.java
*******************************************************
import java.io.*;
import java.util.*;
public class Record {
private static StringTokenizer st;
static readFile s = new readFile();
static readFile t = new readFile();
static readFile q = new readFile();
static readFile a = new readFile();
private static String SSN = "";
private static String Username = "";
private static int Test1 = -1;
private static int Test2 = -1;
private static int Test3 = -1;
private static int Quiz1 = -1;
private static int Quiz2 = -1;
private static int Quiz3 = -1;
private static int Ass1 = -1;
private static int Ass2 = -1;
private static int Ass3 = -1;
private static int Abs = 0;
private static int level = 1;
public static int linesRead = 0;
public Record(int lines) {
setStudent();
setTest("test.1",1);
setTest("test.2",2);
setTest("test.3",3);
setQuiz("quiz.1",1);
setQuiz("quiz.2",2);
setQuiz("quiz.3",3);
setAssign("assign.1",1);
setAssign("assign.2",2);
setAssign("assign.3",3);
linesRead = lines;
//linesRead++;
}
public Record(){}
private static void setStudent(){
if(s.exists("student.dat"
){
String temp = s.returnLine();
//temp = s.returnLine();
for(int i = 0; i < linesRead;i++){
temp = s.returnLine();
}
st = new StringTokenizer(temp);
SSN = st.nextToken();
Username = st.nextToken();
s.close();
}
}
private static void setTest(String name, int testNumber) {
int tempInt = 0;
if (t.exists(name)) {
String temp = t.returnLine();
temp = t.returnLine();
for(int i = 1; i < linesRead;i++){
temp = t.returnLine();
}
st = new StringTokenizer(temp);
st.nextToken();
temp = st.nextToken();
tempInt = Integer.parseInt(temp);
switch (testNumber) {
case 1:
Test1 = tempInt;
break;
case 2:
Test2 = tempInt;
break;
case 3:
Test3 = tempInt;
break;
default:
break;
}
t.close();
}
}
private static void setQuiz(String name, int testNumber) {
int tempInt = 0;
if (q.exists(name)) {
String temp = q.returnLine();
temp = q.returnLine();
for (int i = 1; i < linesRead; i++) {
temp = q.returnLine();
}
st = new StringTokenizer(temp);
st.nextToken();
temp = st.nextToken();
tempInt = Integer.parseInt(temp);
switch (testNumber) {
case 1:
Quiz1 = tempInt;
break;
case 2:
Quiz2 = tempInt;
break;
case 3:
Quiz3 = tempInt;
break;
default:
break;
}
q.close();
}
}
private static void setAssign(String name, int testNumber) {
int tempInt = 0;
if (a.exists(name)) {
String temp = a.returnLine();
temp = a.returnLine();
for (int i = 1; i < linesRead; i++) {
temp = a.returnLine();
}
st = new StringTokenizer(temp);
st.nextToken();
temp = st.nextToken();
tempInt = Integer.parseInt(temp);
switch (testNumber) {
case 1:
Ass1 = tempInt;
break;
case 2:
Ass2 = tempInt;
break;
case 3:
Ass3 = tempInt;
break;
default:
break;
}
a.close();
}
}
public void Setup(){
setStudent();
setTest("test.1",1);
setTest("test.2",2);
setTest("test.3",3);
setQuiz("quiz.1",1);
setQuiz("quiz.2",2);
setQuiz("quiz.3",3);
setAssign("assign.1",1);
setAssign("assign.2",2);
setAssign("assign.3",3);
}
public int returnTest(int testNumber){
switch (testNumber) {
case 1:
return Test1;
case 2:
return Test2;
case 3:
return Test3;
default:
return -1;
}
}
public int returnQuiz(int quizNumber) {
switch (quizNumber) {
case 1:
return Quiz1;
case 2:
return Quiz2;
case 3:
return Quiz3;
default:
return -1;
}
}
public int returnAssign(int assNumber) {
switch (assNumber) {
case 1:
return Ass1;
case 2:
return Ass2;
case 3:
return Ass3;
default:
return -1;
}
}
public String printOutAssign(int assNumber) {
switch (assNumber) {
case 1:
if (Ass1 == -1){
return "N/A";
}
else{
return Integer.toString(Ass1);
}
case 2:
if (Ass2 == -1){
return "N/A";
}
else{
return Integer.toString(Ass2);
}
case 3:
if (Ass3 == -1){
return "N/A";
}
else{
return Integer.toString(Ass3);
}
default:
return "N/A";
}
}
public String printOutTest(int testNumber) {
switch (testNumber) {
case 1:
if (Test1 == -1){
return "N/A";
}
else{
return Integer.toString(Test1);
}
case 2:
if (Test2 == -1){
return "N/A";
}
else{
return Integer.toString(Test2);
}
case 3:
if (Test3 == -1){
return "N/A";
}
else{
return Integer.toString(Test3);
}
default:
return "N/A";
}
}
public String printOutQuiz(int quizNumber) {
switch (quizNumber) {
case 1:
if (Quiz1 == -1){
return "N/A";
}
else{
return Integer.toString(Quiz1);
}
case 2:
if (Quiz2 == -1){
return "N/A";
}
else{
return Integer.toString(Quiz2);
}
case 3:
if (Quiz3 == -1){
return "N/A";
}
else{
return Integer.toString(Quiz3);
}
default:
return "N/A";
}
}
public String returnSSN(){
return SSN;
}
public String returnUserName(){
return Username;
}
public void setAbs(int i){
Abs = i;
}
public void setLevel(int i){
level = 1;
}
public static void main(String[] args){
Record[] r = new Record[392];
for(int i = 0;i < 392;i++){
r = new Record(i);
}
for(int i = 0;i < 391;i++){
System.out.println(r.returnSSN() + " " +
r.returnUserName() + " " +
r.returnAssign(1) + " " +
r.returnQuiz(1) + " " +
r.returnTest(1)
);
}
}
}
********************************************************
readFile.java
********************************************************
import java.io.*;
/**
*Opens, Reads and Closes a file that a program wants to open
*
*/
public class readFile {
private FileReader inFile;
private BufferedReader reader;
private static final int EOF = -1;//the end of file mark
private int location = 0;//the location in the file
/**
*The readFile constructor.
*
*/
public readFile() {/*blank*/}
public void loadinFile(String name) {
try {
inFile = new FileReader(name);
reader = new BufferedReader(inFile);
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
public boolean exists(String name){
File f = new File(name);
if(f.exists()){
loadinFile(name);
return true;
}
else
return false;
}
/**
* the method Read reads one line from the file once it is open. It returns a
* String so that you can use the Read method in readAllOfFile()
*
*/
public String returnLine(){
String read = new String();
try{
read = reader.readLine();
}
catch(IOException e){
System.out.println("Error -- " + e.toString());
}
return read;
}
/**
* Reads oneline then outputs it to the screen.
*/
public void ReadOneLine(){
try{
System.out.println(reader.readLine());
}
catch(IOException e){
System.out.println("Error -- " + e.toString());
}
}
/**
* Reads the whole file by calling the Read() method. the location is then
* set to the int value of the bufferedreader(reader). At the end of file
* reader will return a -1 which is the end of file.
*/
public void readAllOfFile() {
while (EOF != location) {
System.out.println(returnLine());
try {
location = reader.read();
}
catch (IOException e) {
System.out.println("Error -- your out"
;
break;
}
}
}
public boolean EOF(){
try{
if (reader.read() == EOF)
return true;
else
return false;
}
catch(IOException e){
System.out.println("Error -- Can't look for EOF"
;
return true;
}
}
/**
* close just closes the FileReader.
*/
public void close(){
try{
inFile.close();
}
catch(IOException e){
System.out.println("Error -- " + e);
}
}
}
*******************************************************
Record.java
*******************************************************
import java.io.*;
import java.util.*;
public class Record {
private static StringTokenizer st;
static readFile s = new readFile();
static readFile t = new readFile();
static readFile q = new readFile();
static readFile a = new readFile();
private static String SSN = "";
private static String Username = "";
private static int Test1 = -1;
private static int Test2 = -1;
private static int Test3 = -1;
private static int Quiz1 = -1;
private static int Quiz2 = -1;
private static int Quiz3 = -1;
private static int Ass1 = -1;
private static int Ass2 = -1;
private static int Ass3 = -1;
private static int Abs = 0;
private static int level = 1;
public static int linesRead = 0;
public Record(int lines) {
setStudent();
setTest("test.1",1);
setTest("test.2",2);
setTest("test.3",3);
setQuiz("quiz.1",1);
setQuiz("quiz.2",2);
setQuiz("quiz.3",3);
setAssign("assign.1",1);
setAssign("assign.2",2);
setAssign("assign.3",3);
linesRead = lines;
//linesRead++;
}
public Record(){}
private static void setStudent(){
if(s.exists("student.dat"
String temp = s.returnLine();
//temp = s.returnLine();
for(int i = 0; i < linesRead;i++){
temp = s.returnLine();
}
st = new StringTokenizer(temp);
SSN = st.nextToken();
Username = st.nextToken();
s.close();
}
}
private static void setTest(String name, int testNumber) {
int tempInt = 0;
if (t.exists(name)) {
String temp = t.returnLine();
temp = t.returnLine();
for(int i = 1; i < linesRead;i++){
temp = t.returnLine();
}
st = new StringTokenizer(temp);
st.nextToken();
temp = st.nextToken();
tempInt = Integer.parseInt(temp);
switch (testNumber) {
case 1:
Test1 = tempInt;
break;
case 2:
Test2 = tempInt;
break;
case 3:
Test3 = tempInt;
break;
default:
break;
}
t.close();
}
}
private static void setQuiz(String name, int testNumber) {
int tempInt = 0;
if (q.exists(name)) {
String temp = q.returnLine();
temp = q.returnLine();
for (int i = 1; i < linesRead; i++) {
temp = q.returnLine();
}
st = new StringTokenizer(temp);
st.nextToken();
temp = st.nextToken();
tempInt = Integer.parseInt(temp);
switch (testNumber) {
case 1:
Quiz1 = tempInt;
break;
case 2:
Quiz2 = tempInt;
break;
case 3:
Quiz3 = tempInt;
break;
default:
break;
}
q.close();
}
}
private static void setAssign(String name, int testNumber) {
int tempInt = 0;
if (a.exists(name)) {
String temp = a.returnLine();
temp = a.returnLine();
for (int i = 1; i < linesRead; i++) {
temp = a.returnLine();
}
st = new StringTokenizer(temp);
st.nextToken();
temp = st.nextToken();
tempInt = Integer.parseInt(temp);
switch (testNumber) {
case 1:
Ass1 = tempInt;
break;
case 2:
Ass2 = tempInt;
break;
case 3:
Ass3 = tempInt;
break;
default:
break;
}
a.close();
}
}
public void Setup(){
setStudent();
setTest("test.1",1);
setTest("test.2",2);
setTest("test.3",3);
setQuiz("quiz.1",1);
setQuiz("quiz.2",2);
setQuiz("quiz.3",3);
setAssign("assign.1",1);
setAssign("assign.2",2);
setAssign("assign.3",3);
}
public int returnTest(int testNumber){
switch (testNumber) {
case 1:
return Test1;
case 2:
return Test2;
case 3:
return Test3;
default:
return -1;
}
}
public int returnQuiz(int quizNumber) {
switch (quizNumber) {
case 1:
return Quiz1;
case 2:
return Quiz2;
case 3:
return Quiz3;
default:
return -1;
}
}
public int returnAssign(int assNumber) {
switch (assNumber) {
case 1:
return Ass1;
case 2:
return Ass2;
case 3:
return Ass3;
default:
return -1;
}
}
public String printOutAssign(int assNumber) {
switch (assNumber) {
case 1:
if (Ass1 == -1){
return "N/A";
}
else{
return Integer.toString(Ass1);
}
case 2:
if (Ass2 == -1){
return "N/A";
}
else{
return Integer.toString(Ass2);
}
case 3:
if (Ass3 == -1){
return "N/A";
}
else{
return Integer.toString(Ass3);
}
default:
return "N/A";
}
}
public String printOutTest(int testNumber) {
switch (testNumber) {
case 1:
if (Test1 == -1){
return "N/A";
}
else{
return Integer.toString(Test1);
}
case 2:
if (Test2 == -1){
return "N/A";
}
else{
return Integer.toString(Test2);
}
case 3:
if (Test3 == -1){
return "N/A";
}
else{
return Integer.toString(Test3);
}
default:
return "N/A";
}
}
public String printOutQuiz(int quizNumber) {
switch (quizNumber) {
case 1:
if (Quiz1 == -1){
return "N/A";
}
else{
return Integer.toString(Quiz1);
}
case 2:
if (Quiz2 == -1){
return "N/A";
}
else{
return Integer.toString(Quiz2);
}
case 3:
if (Quiz3 == -1){
return "N/A";
}
else{
return Integer.toString(Quiz3);
}
default:
return "N/A";
}
}
public String returnSSN(){
return SSN;
}
public String returnUserName(){
return Username;
}
public void setAbs(int i){
Abs = i;
}
public void setLevel(int i){
level = 1;
}
public static void main(String[] args){
Record[] r = new Record[392];
for(int i = 0;i < 392;i++){
r = new Record(i);
}
for(int i = 0;i < 391;i++){
System.out.println(r.returnSSN() + " " +
r.returnUserName() + " " +
r.returnAssign(1) + " " +
r.returnQuiz(1) + " " +
r.returnTest(1)
);
}
}
}
********************************************************
readFile.java
********************************************************
import java.io.*;
/**
*Opens, Reads and Closes a file that a program wants to open
*
*/
public class readFile {
private FileReader inFile;
private BufferedReader reader;
private static final int EOF = -1;//the end of file mark
private int location = 0;//the location in the file
/**
*The readFile constructor.
*
*/
public readFile() {/*blank*/}
public void loadinFile(String name) {
try {
inFile = new FileReader(name);
reader = new BufferedReader(inFile);
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
public boolean exists(String name){
File f = new File(name);
if(f.exists()){
loadinFile(name);
return true;
}
else
return false;
}
/**
* the method Read reads one line from the file once it is open. It returns a
* String so that you can use the Read method in readAllOfFile()
*
*/
public String returnLine(){
String read = new String();
try{
read = reader.readLine();
}
catch(IOException e){
System.out.println("Error -- " + e.toString());
}
return read;
}
/**
* Reads oneline then outputs it to the screen.
*/
public void ReadOneLine(){
try{
System.out.println(reader.readLine());
}
catch(IOException e){
System.out.println("Error -- " + e.toString());
}
}
/**
* Reads the whole file by calling the Read() method. the location is then
* set to the int value of the bufferedreader(reader). At the end of file
* reader will return a -1 which is the end of file.
*/
public void readAllOfFile() {
while (EOF != location) {
System.out.println(returnLine());
try {
location = reader.read();
}
catch (IOException e) {
System.out.println("Error -- your out"
break;
}
}
}
public boolean EOF(){
try{
if (reader.read() == EOF)
return true;
else
return false;
}
catch(IOException e){
System.out.println("Error -- Can't look for EOF"
return true;
}
}
/**
* close just closes the FileReader.
*/
public void close(){
try{
inFile.close();
}
catch(IOException e){
System.out.println("Error -- " + e);
}
}
}