Results 1 to 4 of 4
  1. #1
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

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

    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));
    but it produces the error referring to the print statement right above.
    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.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

    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
        }
    }
    And here's how you can switch between both arrays when adding the result:
    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);
    }
    Note that this is too basic to work with multiple bracket-pairs e.g. 1 2 3 (4 5 (6 7) 8 9)

  3. #3
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

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

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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

  1. Breaking the thread.
    By JDaniel in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-21-2010, 03:22 PM
  2. breaking up
    By smray7 in forum New To Java
    Replies: 2
    Last Post: 10-31-2010, 07:58 AM
  3. breaking out of while loop
    By mac in forum New To Java
    Replies: 5
    Last Post: 05-18-2010, 03:21 PM
  4. Breaking up of array
    By agarwal_srushti in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 07:03 PM
  5. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 10:50 PM

Posting Permissions

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