Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object keeps containing the same data on output

Status
Not open for further replies.

shaggy56

Programmer
Mar 16, 2003
12
US
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(&quot;test.1&quot;,1);
setTest(&quot;test.2&quot;,2);
setTest(&quot;test.3&quot;,3);

setQuiz(&quot;quiz.1&quot;,1);
setQuiz(&quot;quiz.2&quot;,2);
setQuiz(&quot;quiz.3&quot;,3);

setAssign(&quot;assign.1&quot;,1);
setAssign(&quot;assign.2&quot;,2);
setAssign(&quot;assign.3&quot;,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 &quot;N/A&quot;;
}
else{
return Integer.toString(Ass1);
}
case 2:
if (Ass2 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Ass2);
}
case 3:
if (Ass3 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Ass3);
}

default:
return &quot;N/A&quot;;

}
}

public String printOutTest(int testNumber) {

switch (testNumber) {
case 1:
if (Test1 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Test1);
}
case 2:
if (Test2 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Test2);
}
case 3:
if (Test3 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Test3);
}

default:
return &quot;N/A&quot;;

}
}

public String printOutQuiz(int quizNumber) {

switch (quizNumber) {
case 1:
if (Quiz1 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Quiz1);
}
case 2:
if (Quiz2 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Quiz2);
}
case 3:
if (Quiz3 == -1){
return &quot;N/A&quot;;
}
else{
return Integer.toString(Quiz3);
}

default:
return &quot;N/A&quot;;

}
}




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() + &quot; &quot; +
r.returnUserName() + &quot; &quot; +
r.returnAssign(1) + &quot; &quot; +
r.returnQuiz(1) + &quot; &quot; +
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(&quot;Error -- &quot; + 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(&quot;Error -- &quot; + 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(&quot;Error -- &quot; + 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(&quot;Error -- your out&quot;);
break;
}
}
}

public boolean EOF(){
try{

if (reader.read() == EOF)
return true;
else
return false;
}
catch(IOException e){
System.out.println(&quot;Error -- Can't look for EOF&quot;);
return true;
}
}

/**
* close just closes the FileReader.
*/
public void close(){
try{
inFile.close();
}
catch(IOException e){
System.out.println(&quot;Error -- &quot; + e);
}
}

}
 
Your code does not compile. Maybe you wanted this:

Code:
  public static void main(String[] args){
    Record[] r = new Record[392];

    for(int i = 0;i < 392;i++){
      r[i] = new Record(i);
    }

    for(int i = 0;i < 391;i++){
      System.out.println(r[i].returnSSN() + &quot; &quot; +
                         r[i].returnUserName() + &quot; &quot; +
                         r[i].returnAssign(1) + &quot; &quot; +
                         r[i].returnQuiz(1) + &quot; &quot; +
                         r[i].returnTest(1)
                         );
    }
or this:
Code:
  public static void main(String[] args){

    for(int i = 0;i < 391;i++){
			Record r = new Record(i);
      System.out.println(r.returnSSN() + &quot; &quot; +
                         r.returnUserName() + &quot; &quot; +
                         r.returnAssign(1) + &quot; &quot; +
                         r.returnQuiz(1) + &quot; &quot; +
                         r.returnTest(1)
                         );
    }

Sean McEligot
 
Yeah I just had to trow that man func in so that it could run. I didn't want to put the whole program on this. Any way the first main you have won't output the right data inside the Record, but the second one will.
 
the problem I am having is when the second forloop runs instead of giving me say

SSN USERNAME A1 Q1 T1
-------------------------------
600-22-1040 ZV102 64 24 35
600-22-1060 ZV103 35 50 13

it gives

SSN USERNAME A1 Q1 T1
-------------------------------
600-22-1040 ZV102 64 24 35
600-22-1040 ZV102 64 24 35

only 391 times instead of 2 ;)
 
String temp = s.returnLine();
for(int i = 0; i < linesRead;i++) {
temp = s.returnLine();
}


Every time you assign temp to s.returnLine() you are overwriting the previous value before using it. You have a for loop in the main so you only want to get one line at a time in each set method.

You should also open your files during Setup and only call readLine in the set methods.

You may also want to consider renaming your variables and methods. Methods should do only what they say. For example exists, should be openIfExists or else be split into two methods. Let your variable and method names be self-documenting. You can always find a more descriptive name than temp. This will make your code easier you read for yourself and others.

Sean McEligot
 
The reason for the

String temp = s.returnLine();
for(int i = 0; i < linesRead;i++) {
temp = s.returnLine();
}

is so it doesn't assign the first line which is
SSN username Assigmnet1 Quiz1 Test1
if I didn't have that line it would put that in the array.

but on the variable naming I see what you mean. Thank you for your help
 
I figured it out......it had nothing to do with the main function. it was all those &quot;static&quot; vars I have in the Record class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top