printList problem

Hi

I'm having problems getting java to print out the list below using printList. I just get the numbers of the list printed out like this:

0

1

2

3

4

5

=============

process completed

Any ideas on what i've done wrong?

Thanks

import java.util.*;

class pracB3

{

publicstaticvoid printList(List L)

{

String nextName;

for (int i=0; i<L.size(); i++)

{

nextName = (String) L.get(i);

System.out.println(i);

}

System.out.println("=============");

}

publicstaticvoid main(String[] args)

{

List names =new LinkedList();

names.add("Jones, M");

names.add("Smith, J");

names.add("Bloggs, F");

names.add("Thompson, A ");

names.add("McCullough, G");

names.add("Ling, Y ");

printList(names);

}

}

>

[1673 byte] By [jclegsa] at [2007-11-26 18:54:43]
# 1

for (int i=0; i<L.size(); i++)

{

nextName = (String) L.get(i);

System.out.println(nextName );// ><-- nextName !!!

}

PhHeina at 2007-7-9 20:32:15 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks for your help! I'm an idiot, knew it was something stupid like that!
jclegsa at 2007-7-9 20:32:15 > top of Java-index,Java Essentials,New To Java...
# 3

I'm also having trouble with the searching of the List. I have written the following method mySearchList and tried to return it but it says:

I:\Java practicals\pracB3\pracB3a.java:31: variable position might not have been initialized

return position;

I have been trying to just return the position of the String and not the String itself.

Thanks

import java.util.*;

class pracB3a

{

public static void printList(List L)

{

String nextName;

for (int i=0; i<L.size(); i++)

{

nextName = (String) L.get(i);

System.out.println(nextName);

}

System.out.println("=============");

}

public static int mySearchList (List L, String item)

{

int position;

String nextN;

for (int i=0; i><L.size(); i++)

{

nextN = (String) L.get(i);

position = mySearchList(L, item);

}

return position;

}

public static void main(String[] args)

{

List names = new LinkedList();

names.add("Jones, M");

names.add("Smith, J");

names.add("Bloggs, F");

names.add("Thompson, A ");

names.add("McCullough, G");

names.add("Ling, Y ");

names.add(2, "Young, M");

printList(names);

}

}

>

jclegsa at 2007-7-9 20:32:15 > top of Java-index,Java Essentials,New To Java...
# 4

There is no need for recursion in your search method. Just loop thru the items in the list, and return i when you find a match. Also, it is possible that the item passed in to the method does not exist in the list, and in that case position will no be have a value assigned, and java does not initialize local variables to a default, so you need to do that yourself. A safe default value would be -1, since list indexes start at 0.

public static int mySearchList (List L, String item)

{

int position = -1;

String nextN;

for (int i=0; i<L.size(); i++)

{

nextN = (String) L.get(i);

if(nextN.trim().equalsIgnoreCase(item.trim()))

return i;

}

return position;

}

Also, note that to compare strings, you need to use .equals() not ==, and that it is also a good idea to trim off whitespace when comparing (unless it is significant) and to ignore case.

~Tim

Message was edited by:

SomeoneElse>

SomeoneElsea at 2007-7-9 20:32:15 > top of Java-index,Java Essentials,New To Java...
# 5

You don't need recursion.

public static int mySearchList (List L, String item)

{

int position = -1;// nothing found

for (int i=0; i<L.size(); i++)

{

position = L.indexOf(item);

if (position >= 0) {

break; // found something (first occurance of item)

}

}

return position;

}

EDIT: Tim was quicker. The difference is that Tims method returns the last match, mine the first. No other differences really

Message was edited by:

PhHein

PhHeina at 2007-7-9 20:32:15 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks for all your help!!Much appreciated
jclegsa at 2007-7-9 20:32:15 > top of Java-index,Java Essentials,New To Java...