Error on myList.get(2) - When myList.size() is 8
Hey everyone,
I have been programming in java for a while now but have never once come across this...
I am opening a text file and reading it into a string variable using
myVar = myVar + inputLine;
I am then splitting it
Code :
String[] tData;
for(int i =0; i < information.length ; i++){
System.out.println(information[i]); // OUTPUTS: 100-83-42-11-56-38-62-98
tData = information[i].split("-");
List<String> wordList = Arrays.asList(tData);
System.out.println(wordList.size()); //This is outputting 8
System.out.println(wordList.get(4)); //This gives, Error: 4 when running program (compiles fine)
}
I was hoping some one could help me figure out where I am going wrong?
As far as I can see this should work fine?
Any ideas would be appreciated
Thanks,
Kriogenic.
Re: Error on myList.get(2) - When myList.size() is 8
What is the actual error?
What happens when you loop through your List, printing out each element?
Re: Error on myList.get(2) - When myList.size() is 8
What kind of error is it giving you? Its hard to determine what context that piece of code is running in, and when exactly it is failing.
Re: Error on myList.get(2) - When myList.size() is 8
It compiles fine but when I run instead of the output i want I get
Error: 1
Code :
for(int j =0; j < wordList.size() ; j++){
System.out.println(wordList.size()); //Gives me 8
System.out.println(j); //gets to 1
System.out.println(wordList.get(j)); //Error: 1
}
Re: Error on myList.get(2) - When myList.size() is 8
Re: Error on myList.get(2) - When myList.size() is 8
What value is returned by the get method? Can it be: "Error: 1"
Change your println to:
System.out.println(">"+wordList.get(j) +"<");
Re: Error on myList.get(2) - When myList.size() is 8
PHP Code:
You can probably find more information by visiting the Java API documentation site. I think I used .size() method in LinkedList today and it worked great. Just keep tweaking around if you still need to find the solution.