Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-07-2012, 04:24 AM   PM User | #1
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
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!
sorlaker is offline   Reply With Quote
Old 07-07-2012, 04:41 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
Fou-Lu is offline   Reply With Quote
Old 07-07-2012, 05:19 AM   PM User | #3
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
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
sorlaker is offline   Reply With Quote
Old 07-07-2012, 06:34 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 07-07-2012, 02:16 PM   PM User | #5
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
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
sorlaker is offline   Reply With Quote
Old 07-07-2012, 05:33 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
sorlaker (07-07-2012)
Old 07-07-2012, 07:40 PM   PM User | #7
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
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!
sorlaker is offline   Reply With Quote
Old 07-07-2012, 07:49 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP Code:
int i 0;
try
{
    
Integer.parseInt(s.nextLine());
}
catch (
Exception ex)
{
    
System.out.println("That was not a number.");

Fou-Lu is offline   Reply With Quote
Old 07-07-2012, 10:35 PM   PM User | #9
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
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..
sorlaker is offline   Reply With Quote
Old 07-07-2012, 11:17 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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); 
?
Fou-Lu is offline   Reply With Quote
Old 07-08-2012, 05:59 PM   PM User | #11
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
It says arrayIndexOutOfBoundsException. o.O
sorlaker is offline   Reply With Quote
Old 07-08-2012, 06:50 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Old 07-08-2012, 07:55 PM   PM User | #13
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
This is way too hard.

There isnt a easier way to make this work?

Code:
e.nextInt();
e.nextLine(); //this line always receives ""
sorlaker is offline   Reply With Quote
Old 07-09-2012, 03:59 AM   PM User | #14
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:17 AM.


Advertisement
Log in to turn off these ads.