CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   split string into array (http://www.codingforums.com/showthread.php?t=267294)

sorlaker 07-07-2012 04:24 AM

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!

Fou-Lu 07-07-2012 04:41 AM

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?

sorlaker 07-07-2012 05:19 AM

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

Fou-Lu 07-07-2012 06:34 AM

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.

sorlaker 07-07-2012 02:16 PM

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

Fou-Lu 07-07-2012 05:33 PM

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


sorlaker 07-07-2012 07:40 PM

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!

Fou-Lu 07-07-2012 07:49 PM

PHP Code:

int i 0;
try
{
    
Integer.parseInt(s.nextLine());
}
catch (
Exception ex)
{
    
System.out.println("That was not a number.");



sorlaker 07-07-2012 10:35 PM

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?

Fou-Lu 07-07-2012 11:17 PM

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); 

?

sorlaker 07-08-2012 05:59 PM

It says arrayIndexOutOfBoundsException. o.O

Fou-Lu 07-08-2012 06:50 PM

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

sorlaker 07-08-2012 07:55 PM

This is way too hard.

There isnt a easier way to make this work?

Code:

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


Fou-Lu 07-09-2012 03:59 AM

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.


All times are GMT +1. The time now is 02:38 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.