Results 1 to 4 of 4
- 03-02-2011, 04:02 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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.
I have to use String Tokenizers. Where is my logic failing here?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++; } }
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.
- 03-02-2011, 04:06 PM #2
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!
- 03-02-2011, 04:48 PM #3
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
didn't find a "," which would return -1.Java Code:initialCourses[i][1].indexOf(",")
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.
- 03-02-2011, 05:04 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
Need help with school project
By camaro01 in forum New To JavaReplies: 16Last Post: 02-20-2011, 06:34 PM -
School project help!
By Addez in forum Advanced JavaReplies: 5Last Post: 09-17-2010, 10:37 PM -
Need help with school project!
By jaxxx in forum New To JavaReplies: 5Last Post: 03-25-2010, 08:55 PM -
calculating time of program runing
By sara12345 in forum New To JavaReplies: 3Last Post: 03-17-2010, 12:25 AM -
school project help
By justin8790 in forum New To JavaReplies: 5Last Post: 03-22-2009, 09:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks