Results 1 to 20 of 24
- 09-04-2010, 12:25 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
Variable of an object in an array compared to an element of another array?
I have an array of objects with four fields and another array of ints. I'm having trouble trying to see if each of the elements of the int array are equal to a certain (the same) field of each of the objects in the object array but I keep getting errors.
if (studentIDNumbers[index] != student[index2].getStudentID())
doesn't seem to work..
studentIDNumbers is the array of ints and student[] is the object array. getStudentID is the accessor method for the int field of the object.
Anyone know what I'm doing wrong?
- 09-04-2010, 02:25 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
hard to tell without more of your code especially on the class student
- 09-04-2010, 02:49 AM #3
Try debugging your code by writing simple loops that goes thru the student array and prints out the values returned by the getStudentID() method and the values in the studentIDNumbers array. Then you can see if there should be matches.
Further debugging add this:
System.out.println("studentIDNumbers[index] = " + studentIDNumbers[index] + ", student[index2].getStudentID() =" + student[index2].getStudentID());
just before the if() test where they are compared.
- 09-04-2010, 05:07 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
if (studentIDNumbers[index] != student[index2].getStudentID())
That should be comparing the two int variables though, right? I mean the syntax isn't wrong is it? studentIDNumbers[] is just an array of ints so its calling the int in 'index' and 'getStundentID()' is the accessor method for the int field (it returns the value held in the studentID field) of the student object and the name of the array is 'student[]' so shouldn't that be the correct syntax for calling the student ID variable held by the student object in the 'index2' element position?
I trying debugging with loops before I posted here and it prints out the id number but when I run the whole thing it throws an exception at the if statement above.
-
As mentioned already, it's hard to know what's going on without code.
- 09-04-2010, 01:07 PM #6
Please copy and paste here the full text of the error message.it throws an exception at the if statement above.
- 09-04-2010, 07:09 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
- 09-04-2010, 07:13 PM #8
What variable at line 72 is null?
Add a println statement before line 72 to show the values of all the variables used in line 72 to find out which one is null.
- 09-06-2010, 08:54 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
import java.util.*;
import java.io.*;
public class StudentDemo
{
public static void main(String[] args)throws IOException
{
String filename;
String filename2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file to open.");
filename = keyboard.nextLine();
File openFile = new File(filename);
Scanner inputFile = new Scanner(openFile);
String line = inputFile.nextLine();
Student[] student = new Student[10];
for(int index = 0; index < 10; index++)
{
while (inputFile.hasNext())
{
String[] tokens = line.split(",");
student[index] = new Student(tokens[0], tokens[1], tokens[2], tokens[3]);
line = inputFile.nextLine();
}
}
inputFile.close();
System.out.println("Enter the name of the file of student IDs to open.");
filename2 = keyboard.nextLine();
File openFile2 = new File(filename2);
Scanner inputFile2 = new Scanner(openFile2);
int[] studentIDNumbers = new int[10];
for (int index = 0; index < 10; index++)
{
studentIDNumbers[index] = inputFile2.nextInt();
}
inputFile2.close();
boolean found = false;
for(int index = 0; index < 10; index++)
{
for(int index2 = 0; index2 < 10; index2++)
{
if (studentIDNumbers[index] == student[index2].getStudentID())
{
found = true;
System.out.println(studentIDNumbers[index] + "is " +student[index2].getFirstName()+ " " +student[index2].getLastName());
}
else
{
found = false;
}
}
if (!found)
System.out.println(studentIDNumbers[index] + "was not found.");
}
}
private static class Student
{
private String firstName;
private String lastName;
private int studentID;
private String studentMajor;
public Student(String fName, String lName, String id, String major)
{
firstName = fName;
lastName = lName;
studentID = Integer.parseInt(id);
studentMajor = major;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getStudentID()
{
return studentID;
}
public String getMajor()
{
return studentMajor;
}
}
}
thats the whole code.
first txt file is a list of students with their first and last names, id number and major. I just made them up to test the program. The second one is a list of nothing but id numbers.
first one:
Bob,johnson,123,Math
john,paul,456,Computer Science
paul,john,789,Biology
mark,anderson,012,Physics
jane,hughs,034,Computer Science
megan,jones,056,English
lanie,mcintire,078,Math
robert,robertson,090,History
jimmy,Johnson,091,Math
joan,bigguns,001,Computer Science
second one:
123
456
001
765
543
999
012
000
333
777
I did have a loop inside the loop that read the lines from the first file that would print out the individual tokens and that works but for some reason its not saving the tokens array to any of the student array objects. Also the second array seems to get the last number of the file saved to the first element and then zeros to the rest.Last edited by asmodean; 09-06-2010 at 08:58 PM.
- 09-06-2010, 08:56 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
import java.util.*;
import java.io.*;
public class StudentDemo
{
public static void main(String[] args)throws IOException
{
String filename;
String filename2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file to open.");
filename = keyboard.nextLine();
File openFile = new File(filename);
Scanner inputFile = new Scanner(openFile);
String line = inputFile.nextLine();
Student[] student = new Student[10];
for(int index = 0; index < 10; index++)
{
while (inputFile.hasNext())
{
String[] tokens = line.split(",");
student[index] = new Student(tokens[0], tokens[1], tokens[2], tokens[3]);
line = inputFile.nextLine();
}
}
inputFile.close();
System.out.println("Enter the name of the file of student IDs to open.");
filename2 = keyboard.nextLine();
File openFile2 = new File(filename2);
Scanner inputFile2 = new Scanner(openFile2);
int[] studentIDNumbers = new int[10];
for (int index = 0; index < 10; index++)
{
studentIDNumbers[index] = inputFile2.nextInt();
}
inputFile2.close();
boolean found = false;
for(int index = 0; index < 10; index++)
{
for(int index2 = 0; index2 < 10; index2++)
{
if (studentIDNumbers[index] == student[index2].getStudentID())
{
found = true;
System.out.println(studentIDNumbers[index] + "is " +student[index2].getFirstName()+ " " +student[index2].getLastName());
}
else
{
found = false;
}
}
if (!found)
System.out.println(studentIDNumbers[index] + "was not found.");
}
}
private static class Student
{
private String firstName;
private String lastName;
private int studentID;
private String studentMajor;
public Student(String fName, String lName, String id, String major)
{
firstName = fName;
lastName = lName;
studentID = Integer.parseInt(id);
studentMajor = major;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getStudentID()
{
return studentID;
}
public String getMajor()
{
return studentMajor;
}
}
}
thats the whole code.
first txt file is a list of students with their first and last names, id number and major. I just made them up to test the program. The second one is a list of nothing but id numbers.
first one:
Bob,johnson,123,Math
john,paul,456,Computer Science
paul,john,789,Biology
mark,anderson,012,Physics
jane,hughs,034,Computer Science
megan,jones,056,English
lanie,mcintire,078,Math
robert,robertson,090,History
jimmy,Johnson,091,Math
joan,bigguns,001,Computer Science
second one:
123
456
001
765
543
999
012
000
333
777
I did have a loop inside the loop that read the lines from the first file that would print out the individual tokens and that works but for some reason its not saving the tokens array to any of the student array objects. Also the second array seems to get the second to last number of the file saved to the first element and then zeros to the rest.
- 09-06-2010, 09:06 PM #11
Is your problem solved now? I didn't see your question(s) in the last post.
If you are going to post code, please use code tags to preserve formatting.
- 09-06-2010, 09:09 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
- 09-06-2010, 09:18 PM #13
Each element in an array of objects must be assigned a value, otherwise it is null.
Does your program do that?
- 09-07-2010, 01:43 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
Yeah, I have a loop that assigns values to an object's fields and then it assigns that object to the array. At least I'm pretty sure its supposed to be doing that. But for some reason the objects aren't getting assigned to the array, or the values aren't getting assigned to the objects' fields.
- 09-07-2010, 02:29 PM #15
You need to do some debugging of your program to find out why.for some reason the objects aren't getting assigned to the array, or the values aren't getting assigned to the objects' fields.
Print out the contents of the array after you add a item to it by using the Arrays.toString() method.
Add the following method to the Student class:
This will be what the Arrays.toString() method will show.Java Code:public String toString() { return "fName=" + fName + ", id=" + studendID; }
- 09-07-2010, 02:42 PM #16
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
I did its at the bottom of the wall of text I posted yesterday, sorry.
"I did have a loop inside the loop that read the lines from the first file that would print out the individual tokens and that works but for some reason its not saving the tokens array to any of the student array objects. Also the second array seems to get the last number of the file saved to the first element and then zeros to the rest."
- 09-07-2010, 02:47 PM #17
Does the array have a variable name?second array seems to get the last number of the file saved to the first element and then zeros to the rest
What does "zeros to the rest" mean?
Do some more debugging by adding some println()s to see why the values are zeros.
Print out each line as you read it in.
Print out each time a new Student object is created.
Print out the value of index as each Student object is added to the array.
- 09-07-2010, 07:13 PM #18
Member
- Join Date
- Jul 2010
- Posts
- 18
- Rep Power
- 0
Okay, I put this line inside the loop that assigns variables to the field of a student object.
System.out.println(student[index].getFirstName());
I put it in the loop but after the object is supposed to be added to the array, (line 32) and it spits out the correct names then, but I also added it to the code just after the loop executes (in another for loop on line 38,39) and it gives me a null pointer exception. . . I'm so frustrated. . . It does make any sense. . .
Edit: I was wrong. Actually its starting the println loop that I added below the assignment loop but the first element of the array is the student object filled with the last line of the file. . . All the other elements are null. I know what all that means. What I don't is why its doing it. . .Last edited by asmodean; 09-07-2010 at 07:24 PM.
- 09-07-2010, 07:22 PM #19
Why didn't you put the 3+ lines in your code as I suggested? If you put only one in at a time, the process will take a long time.
Your print out could be enhanced a bit to show where you are in the loop and which variable is null by using three lines:
System.out.println("index=" + index);
System.out.println("student[index]=" + student[index]);
System.out.println("getFirstName()=" + student[index].getFirstName());
When you get a NPE, you must look at the code and determine what variable is null.it gives me a null pointer exception
Then back track in the code to see why it is null and then change it so it is not null.
- 09-07-2010, 07:40 PM #20
What does the print out of the value of index show?All the other elements are null.
You need to work on one part of the program at a time.
First concentrate on filling the array with data.
After you think its full, use the Arrays.toString() method to print out the contents of the array.
If there are any holes/null values or the values are all the same go back over your code that fills the array and find the problem.Last edited by Norm; 09-07-2010 at 07:45 PM.
Similar Threads
-
deleting an element from an array
By moamen in forum New To JavaReplies: 11Last Post: 01-03-2010, 05:38 PM -
[SOLVED] indexing an element in an array help!
By anthonym2121 in forum New To JavaReplies: 1Last Post: 04-03-2009, 06:21 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
Unique element in an array
By revathi17 in forum New To JavaReplies: 2Last Post: 12-31-2007, 08:44 AM -
Max element in an Array
By mew in forum New To JavaReplies: 5Last Post: 12-03-2007, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks