Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Error on myList.get(2) - When myList.size() is 8

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
     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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


    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 
    	}

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Error on myList.get(2) - When myList.size() is 8

    What is the error?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Error on myList.get(2) - When myList.size() is 8

    wordList.get(j)
    What value is returned by the get method? Can it be: "Error: 1"
    Change your println to:
    System.out.println(">"+wordList.get(j) +"<");

  7. #7
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default 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 siteI think I used .size() method in LinkedList today and it worked greatJust keep tweaking around if you still need to find the solution

Similar Threads

  1. HI can some one tell me the size of the below structure.
    By sucheth13 in forum Java Native Interface
    Replies: 1
    Last Post: March 11th, 2011, 02:08 AM
  2. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  4. Font Size
    By nasi in forum AWT / Java Swing
    Replies: 5
    Last Post: May 13th, 2010, 06:22 AM
  5. [SOLVED] Hashtable and Key Size
    By sapzero in forum Collections and Generics
    Replies: 0
    Last Post: January 28th, 2010, 01:31 PM