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 4 of 4

Thread: String.split"char"; error

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default String.split"char"; error

    i am scanning in lines from a document. i want to store each section of the line into seperate strings variables. each section of the line is seperated by ",". the problem is if there is a space in the variable i gett an arrayOutOfBounds exception for example
    ...,hello world,yousuck,
    here ,hello world, will throw exception where as ,yousuck, would be fine.
    so i am assuming it is spliting at space allso as System.out.println prints them on seperate lines im not really sure why it is causing error any help would be great thanks.

    here is code
    String in=x.nextLine();
    String[] tokens = in.split("\\,");
    String one=tokens[0];
    String two=tokens[1];


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: String.split"char"; error

    What is the exception being thrown? And what is the code to which it refers?

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: String.split"char"; error

    sorry the scanner loads the linies of a text doc into an arraylist
    each line is two string seperated by","
    code uses selinium
    example.. htp://www.google.com,gofish
    htp://www.google.com,go fish
    for(int u=0;u<sitesL.size();u++){
                        String in=sitesL.get(u);
                        String[] tokens = in.split("\\,");
                        String one=tokens[0];
                        String two=tokens[1];//line throws error
                        WebDriver targetSite=new FirefoxDriver();
                        targetSite.manage().window().setPosition(posg);
                        targetSite.manage().window().setSize(sizeg);
                        targetSite.get(one);//gos to google
                        WebElement target = targetSite.findElement(By.className(classname));//finds search
                        target.sendKeys( two);enters text
    here the first line will work fine but the second will throw excption
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at chat.Chat.disher(Chat.java:81)//this is line with              String two=tokens[1];
    	at chat.Chat.main(Chat.java:37)//this is call from main
    BUILD STOPPED (total time: 1 minute 18 seconds)
    however it still enters text to search correctly google will search go fish correclty the programe then just throws error

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: String.split"char"; error

    What is the string u that is being split? Print it, along with the length and the contents of the array. Perhaps u does not have the value you think ("htp://www.google.com,gofish" and "htp://www.google.com,go fish")

    ---

    split("\\,") does split the string on commas as can be seen by removing the other logic and looking merely at what split() does.

    public class SplitEg {
        public static void main(String[] args) {
            test("htp://www.google.com,gofish");
            test("htp://www.google.com,go fish");
        }
     
        private static void test(String str) {
            System.out.printf("Looking at -->%s<--%n", str);
            String[] arr = str.split("\\,");
            System.out.printf("Found %d elements:%n", arr.length);
            for(int i = 0; i < arr.length; i++) {
                System.out.printf("    -->%s<--%n", arr[i]);
            }
        }
    }

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 10th, 2023, 03:11 AM
  2. Replies: 4
    Last Post: October 3rd, 2012, 07:40 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM