Results 1 to 4 of 4
Thread: delimiter breaking the array
- 03-12-2012, 11:54 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
delimiter breaking the array
Hi java programmers!
I have a problem here using a delimiter. I have the following input:
2
4
1 2 3 (1 2 4 2 4 5)*100 4 5 1
My goal is to get everything inside the brackets and put it into a separate array. I wrote this code:
but it produces the error referring to the print statement right above.Java Code:public static void main(String[] args) throws FileNotFoundException { Scanner s = new Scanner(new File("src/input.txt")); while (s.hasNext()){ s.useDelimiter("//()"); list.add(s.next()); } System.out.println(s.delimiter()); System.out.println(list); System.out.println(list.get(5));
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 1
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at PagingSystem.main(PagingSystem.java:44)
//()
[2
4
1 2 3 (1 2 4 2 4 5)*100 4 5 1]
It seems to me that it breaks the ArrayList into 3 other arrays. However if I don't use any delimiter the output is this:
[2, 4, 1, 2, 3, (1, 2, 4, 2, 4, 5)*100, 4, 5, 1]
which is what i want it to look like after using the delimiter with the brackets being separate tokens. I don't particularly care about commas. I suppose I'm not using the delimiter properly or I'm not even supposed to use delimiter but I am kind of clueless. Can you help me out?
Many thanks :)Last edited by truant420; 03-12-2012 at 11:58 AM.
-
Re: delimiter breaking the array
I don't think this is a good time to use a delimiter, because in this case it makes your life more complicated.
Here is another way you could do this:
And here's how you can switch between both arrays when adding the result:Java Code:while (s.hasNext()) { if (s.hasNextInt()) { //add result of s.nextInt() to first array or second array } if (s == '(') { //use the second array } if (s == ')') { //use the first array } }
Note that this is too basic to work with multiple bracket-pairs e.g. 1 2 3 (4 5 (6 7) 8 9)Java Code://set to false when you want to use the second array boolean useFirstArray = true; String s = "A string"; if (useFirstArray) { firstArray.add(s); } else { secondArray.add(s); }
- 03-13-2012, 05:33 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
Re: delimiter breaking the array
The reason I was trying to use delimiter in the first place was because the scanner reads the bracket and the integer after it as one token.
1, 2, 3, (1, 2 ...
I tried using your code but it still behaves in the same manner. Any other ideas? How do I make it read the bracket and the number after it separately?Last edited by truant420; 03-13-2012 at 09:11 AM.
-
Re: delimiter breaking the array
Oh right, then use String.contains("(") instead of if s=="(".
Java Code:private static final String openBracket = "("; private static final String closedBracket = ")"; private boolean useFirstArray = true;Java Code:while (s.hasNext()) { if (s.hasNextInt()) { //store the number storeNumber(s.nextInt()); continue; //continue if we get the number } //only come here if we don't get the number String str = s.next(); if (str.contains(openBracket)) { //we found an open-bracket, lets get the number attached with it String nextIntStr = str.replace(openBracket, ""); Integer nextInt = Integer.valueOf(nextIntStr); //set boolean to use second array useFirstArray = false; //store the number storeNumber(nextInt); } if (str.contains(closedBracket)) { //we found a closed-bracket //do something similar to the above, but set useFirstArray to false } }Java Code:void storeNumber(int number) { if (useFirstArray) { //store number in first array } else { //store number in second array } }Last edited by ozzyman; 03-14-2012 at 12:03 AM.
Similar Threads
-
Breaking the thread.
By JDaniel in forum Threads and SynchronizationReplies: 4Last Post: 12-21-2010, 03:22 PM -
breaking up
By smray7 in forum New To JavaReplies: 2Last Post: 10-31-2010, 07:58 AM -
breaking out of while loop
By mac in forum New To JavaReplies: 5Last Post: 05-18-2010, 03:21 PM -
Breaking up of array
By agarwal_srushti in forum New To JavaReplies: 3Last Post: 09-27-2009, 07:03 PM -
delimiter
By satin in forum New To JavaReplies: 2Last Post: 11-17-2008, 10:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks