Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post

    split string into array

    The split function doesnt return the result as a String[].

    Example :

    Code:
    String a = "some phrase";
    String[] b = a.split(" ");
    U can see this by running both codes.

    Code:
    System.out.println(b);
    System.out.println(b[0]);
    I want it to return into an array like :

    b[0] = "some";
    b[1] = "phrase";

    can someone help me figure out this pls? thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Works fine for me:
    PHP Code:
            String a "some phrase";
            
    String[] a.split(" ");
            
            for (
    String s b)
            {
                
    System.out.println("String part: " s);
            } 
    So I'm not quite sure I follow you here?

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    yeah i know it works that way. but it not returns as an array.

    try printing b[1]

    if u print b[0] the output will be :
    some
    phrase

    and i need to assign b[0] to some and b[1] to phrase. o.O

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Um, no it doesn't. It works as expected, even the loop shows that.
    PHP Code:
            String a "some phrase";
            
    String[] a.split(" ");
            
            for (
    String s b)
            {
                
    System.out.println("String part: " s);
            }  
            
            
    System.out.println("Position 0: " b[0]);
            
    System.out.println("Position 1: " b[1]); 
    Code:
    String part: some
    String part: phrase
    Position 0: some
    Position 1: phrase
    BTW, if you are returning results of b[0] as:
    Code:
    some
    phrase
    That indicates your string is not separated by a space, rather its separated by a linefeed.

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    Hm....

    is
    String a = "some phrase";

    equal to

    e.nextLine();
    when u write "some phrase" at the terminal?

    because for me it shows
    some
    phrase

    when printing b[0].

    o.O

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Doesn't matter if they come from the scanner or from hard coded input. The split works as anticipated:
    PHP Code:
            Scanner s = new Scanner(System.in);
            
    String a s.nextLine();
            
    String[] a.split(" ");

            
    System.out.println("Chars in a:");
            
    char[] ca a.toCharArray();
            for (
    Character c ca)
            {
                
    System.out.println((int)c);
            }
            
            
    System.out.println("Chars in position 0:");
            
    char[] c0 b[0].toCharArray();
            for (
    Character c c0)
            {
                
    System.out.println((int)c);
            }
            
            
    System.out.println("Chars in position 1:");
            
    char[] c1 b[1].toCharArray();
            for (
    Character c c1)
            {
                
    System.out.println((int)c);
            } 
    Terminal:
    Code:
    some phrase
    Chars in a:
    115
    111
    109
    101
    32
    112
    104
    114
    97
    115
    101
    Chars in position 0:
    115
    111
    109
    101
    Chars in position 1:
    112
    104
    114
    97
    115
    101

  • Users who have thanked Fou-Lu for this post:

    sorlaker (07-07-2012)

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    OMG thanks a lot!
    im new to java so could u display some code example of the try/catch using e.nextLine()?

    if its a number to something. if its a line...

    thanks!

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    int i 0;
    try
    {
        
    Integer.parseInt(s.nextLine());
    }
    catch (
    Exception ex)
    {
        
    System.out.println("That was not a number.");


  • #9
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    How can i use it with lots of lines?

    like :

    Code:
    while(e.hasNext()){
        int i = 0;
        try{
            i = Integer.parseInt(e.nextLine());
            System.out.println("Your input is a number : "+i);
        }catch (Exception ex){
            String a = e.nextLine();
            System.out.println("Your input is a text : "+a);
        }
    }
    Terminal :
    OBVIOUS OUTPUT I KNOW XP
    Code:
    90
    Your input is a number : 90
    omg
    omgogmgomgomgomg
    Your input is a text : omgogmgomgomgomg
    I want something like this :

    Code:
    90
    Your input is a number : 90
    omg
    Your input is a text : omg
    ....

    how can i do that?
    Last edited by sorlaker; 07-07-2012 at 10:45 PM.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You wouldn't put an input request in the catch. That's to display a message of something being incorrect. Perhaps what you mean is something more along the lines of:
    PHP Code:
            int i 0;
            
    boolean bValid false;
            do
            {
                try
                {
                    
    System.out.print("Enter a number: ");
                    
    Integer.parseInt(s.nextLine());
                    
    bValid true;
                }
                catch (
    NumberFormatException ex)
                {
                    
    System.out.println("That is not a number.  Try again.");
                }
            }
            while (!
    bValid);
            
    System.out.println("You have entered: " i); 
    ?

  • #11
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    It says arrayIndexOutOfBoundsException. o.O

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    And the code you are using is?
    IndexOutOfBoundsException indicates you are trying to access an array outside of the valid range declared. That hasn't a thing to do with casting, for which it tosses a NumberFormatException. The scanner can also throw, but you'll likely only see the IllegalStateException off of it, and the NoSuchElementException is also possible if you try and force information out of it (from say a file).

  • #13
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    This is way too hard.

    There isnt a easier way to make this work?

    Code:
    e.nextInt();
    e.nextLine(); //this line always receives ""

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Using nextInt() will leave the linefeed on the buffer. So when you call nextline it will only pull the linefeed. It can lead to a lot of trouble.
    Simply call e.nextLine() after any call to a next* method other than nextLine. It'll clear the linefeed off to let the next line of entry through.


  •  

    Posting Permissions

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