http://pastebin.com/aefXKD9w

This is my code.

If the command line csv file (for example, input.csv) has the following:

Fred 22
Stella 11
Nellie 33
George 11
Violet 33
Rose 11
Bob 33
Lena 11
Billy 22

1) It should first print Fred is 22 years old.. Stella is 11 years old.. up to Billy is 22 years old (This works)
2) Then it should ask user to input the age (This works too)
3) Then for example, if user enters 11, it should only print people with 11 years old
Stella is 11 years old

Example output: http://img573.imageshack.us/img573/2415/examplea.jpg
George is 11 years old
Rose is 11 years old
Lena is 11 years old

But I'm getting blank on this part.. the method for this is displayPeopleWithSameAge().. could anyone help me out here?

Thank you!

Recommended Answers

All 12 Replies

I am getting "ERROR: null" when trying to run your program with a file identical to the one you have posted here - are you sure that the Scanner usage is correct?

Can you post some code? The best way to do this is not just print the messages, but save the results of the file in a structure. An array or a list. Then loop the list for the display. When the user enters the age then loop again and print only those with that age.

So, where is your code?

commented: Great help!!!! +1

After looking at the code:
In the displayPeopleWithSameAge method, you are comparing the input with the current. But one is a String and the other a PersonNode.
Also you haven't implemented the equals method in the PersonNode class. Try this:

public String displayPeopleWithSameAge(String input) {

for(PersonNode current = head; current != null; current = current.getNext()) {
     if (input.equals(current.getAge())) {
         output += current.toString() + "\n";
     }
   }
   return output;
}

Ok, found your problem. First of all the way that you have parsed your file returned "ERROR: null" when I have tried to run it. I have changed it a little bit so it will work for me:

scan = new Scanner(new File(fileLoc));
if(scan != null) 
{
   String[] line;
   while(scan.hasNext()) 
   {
	   line = scan.nextLine().split("\\s+");
	   name = line[0];
	   age = line[1];
	   list.add(name, age); // add to list
   }
}

Second, in the displayPeopleWithSameAge(String input) method you have the following line:

if (input.equals(current))

Which basically compares the String input with the PersonNode current - if I am not mistaken this is actually comparing the input with PersonNode's toString() method - no way that those two will be equal right?

I have changed it to be

if (input.equals(current.getAge()))

Where getAge() is a getter I have added in order to retrieve the age member from the PersonNode instance.

public String getAge()
{
   return age;
}

EDIT: javaAddict beat me to it by 3 minutes - using the same getter - ++javaAddict :)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

I was out for awhile. Sorry about that. I tried implementing your code and I get ArrayIndexOutOfBounds on the line that has

age = line[1];

Can this be done w/o using array? I don't think we're supposed to use them for this assignment.

The split method returns an array. If you are not allowed to use them - use your previous methods, just make sure that they work. Your main problem was what javaAddict and I have stated - your comparison was wrong.

commented: Thank you very much! Great Help! +1

Thank you! I figured out how to print without using array. But I have some question. On CSV file, it's something like

Name Age
Billy 22
Stella 11
...

So it prints Name is Age years old on the first line for the first JOptionPane pop-up.. this did not happen last time though.

So from my original code, I just added getAge() method and fixed my comparison for displayPeople().

The regex which you are using in the useDelimiter method is "\\s*" - * means 0 or more, when in fact you want 1 or more (since you want at least one space to separate between the name and the age) - try using "\\s+", which means one or more whitespace character.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

I was out for awhile. Sorry about that. I tried implementing your code and I get ArrayIndexOutOfBounds on the line that has

age = line[1];

Can this be done w/o using array? I don't think we're supposed to use them for this assignment.

For the ArrayIndexOutOfBoundsException I think you should have used this method:
scan.hasNextLine() instead of scan.hasNext()
But you should wait for apines comment, since it's his code.

For the other bug, try to add some System.out.println where you read the file and add elements to the list in order to see what is read. If the error still remains, just ignore the first line.

For the ArrayIndexOutOfBoundsException I think you should have used this method:
scan.hasNextLine() instead of scan.hasNext()
But you should wait for apines comment, since it's his code.

For the other bug, try to add some System.out.println where you read the file and add elements to the list in order to see what is read. If the error still remains, just ignore the first line.

minimi stated that he is not allowed to use arrays, so my code is out - hasNextLine() seems like a good solution though :)

Yup, it returns null when it's hasNext(). So I used hasNextLine(). I can't seem to figure out removing name is years old.

So as of now, my output is like

name is years old
Fred is 22 years old..
...

I'm trying something like

if(list.equals("name is age years old")) {
  break;
}
else {
  JOptionPane.showMessageDialog(null, list.toString()); // print all persons
}

After you read the first line of the file, don't add it to the list. It's that simple. Or add that if statement where you add elements to the list.

if (!line.equals("Name Age")) {
  // add to the list
}

And don't compare the list with a String: list.equals("name is age years old") list is not a String. Don't do that. Also the coding is wrong because by the time you reach that point the list has all the elements of the file, so it will never have only this value:
"name is age years old"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.