Skip to main content

Question about objects

No replies
Joined: 2006-08-03

I had a problem with a code I was writing for class and I couldn't figure out why it was acting the way it was. I finally got the program to work but I had a problem with a block of code and I would appreciate if someone could tell me what it acted this way. First let me tell ou how I solved it, I moved the

Student student2 = new Student();

line into the while loop. Before what was happening was after the first go around, the compareTo was pointing at the same object. After I placed the previous line of code into the while loop it worked fine. Why is that?

Here is the code:

import java.util.*;

public class Project1 {

public static void main(String[] args) throws IOException {

// counter for while loop.
int control = -1;

//open file
Scanner inputStream = openFile();

//declaration of student objects
Student student1 = new Student();
Student student2 = new Student(); //from here!!!

//input of student object from file

while (inputStream.hasNext() && control <= 0) {
//to here --->//Student student2 = new Student();
control = student1.compareTo(student2);
student1 = student2;

Message was edited by: zfivgas