Results 1 to 4 of 4
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default School Project - runing out of time D:

    I need to take a string that has 3 words in it and split each word into a new string. New string will be an array. So for example I read a file and stored it in a two dimensional string array. then I take array[i][2] i being a number that will change since i'm doing this for each line and 2 is the part that contains the three words. Now i want to separate each word on its own and store it in a new two dimensional array but i just can't get it to work.

    Here is sample code thats not working for me.
    Java Code:
    for(int i=0;i<3;i++)
    		{
    			initialArray[i][1].substring(0, initialCourses[i][1].indexOf(",")); //<----Line 19
    			StringTokenizer str = new StringTokenizer(initialArray[i][2], " ");
    			isolatedArray[i] = new String[str.countTokens()];
    			int ctr = 0;
    			while(str.hasMoreTokens())
    			{
    				String temp = str.nextToken().trim();
    				System.out.println(temp);
    				isolatedArrayi][ctr] = temp; 
    				ctr++;
    			}
    		}
    I have to use String Tokenizers. Where is my logic failing here?

    Here is the error:
    Java Code:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    	at java.lang.String.substring(Unknown Source)
    	at courseChanger.CourseChanger.main(CourseChanger.java:19)

    I also should probably mention each line has a different number of words. and that the first part where i store the lines into a two D array works fine so you don't have to worry about any of that.
    Last edited by Bagzli; 03-02-2011 at 04:08 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    You want to split a String, eh? Why not use the split method then?

    I'd help more, but I'm afraid that it would take too long and your time would run out by then.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    I agree with Kevin, but you might want to make sure initialArray[i][1] contains what you think. I assume you're getting the error because
    Java Code:
    initialCourses[i][1].indexOf(",")
    didn't find a "," which would return -1.

    Edit:
    Also, line 19 seems unnecessary. The substring method returns a string which you're not storing anywhere.
    Last edited by Shoss; 03-02-2011 at 04:51 PM.

  4. #4
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Solution was:
    Quote Originally Posted by Shoss View Post
    Edit:
    Also, line 19 seems unnecessary. The substring method returns a string which you're not storing anywhere.
    Thank you

Similar Threads

  1. Need help with school project
    By camaro01 in forum New To Java
    Replies: 16
    Last Post: 02-20-2011, 06:34 PM
  2. School project help!
    By Addez in forum Advanced Java
    Replies: 5
    Last Post: 09-17-2010, 10:37 PM
  3. Need help with school project!
    By jaxxx in forum New To Java
    Replies: 5
    Last Post: 03-25-2010, 08:55 PM
  4. calculating time of program runing
    By sara12345 in forum New To Java
    Replies: 3
    Last Post: 03-17-2010, 12:25 AM
  5. school project help
    By justin8790 in forum New To Java
    Replies: 5
    Last Post: 03-22-2009, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •