Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 splitting up input
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
krishon




PostPosted: Thu Sep 25, 2003 6:10 pm   Post subject: splitting up input

i need help with a particular exercise. In the question, i need to read in a users name with a space separating the first and last name (which i can do). However, i'm to split up the first and last name and store them into separate variables. That's the part i have no clue how to do.......ne help will be appreciated
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Sep 25, 2003 7:08 pm   Post subject: (No subject)

cant you just use substring?

Edit: new code

code:

import java.io.*;

public class substrings {

        public static void main(String[] args) {
                               
                InputStreamReader istream = new InputStreamReader(System.in);
                BufferedReader bufRead = new BufferedReader(istream);
               
                String text;
                String name1;
                String name2;
               
                System.out.println("enter your full name");
                try
                {
                text = bufRead.readLine();
                name1 = text.substring(0,text.indexOf(" "));
                name2 = text.substring(text.indexOf(" ")+1);
               
                System.out.println(name1);
                System.out.println(name2);
                }
                catch(Exception e)
                {
                        System.out.println("error occured");
                }
       
        }
}
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
krishon




PostPosted: Thu Sep 25, 2003 7:21 pm   Post subject: (No subject)

ye that's wut i thought tooo, i know to use the substring, i jsut dun't know how to see where the space is....ur idea wuz wut i thought at the start...but the only problem like i said wuz to detect the space
rizzix




PostPosted: Thu Sep 25, 2003 7:25 pm   Post subject: (No subject)

so problem solved right. i mean tony solved it for you. he detected the space and split the string.
krishon




PostPosted: Thu Sep 25, 2003 7:42 pm   Post subject: (No subject)

yah...he did thx tony Very Happy
rizzix




PostPosted: Thu Sep 25, 2003 7:53 pm   Post subject: (No subject)

here's an alternate way (lesser code) [wow you've got to love this language]:

code:

String input = "John Doe"; // lets suppose this is the input you get
String[] names = input.split(" ");

System.out.println(names[0]);  // First name
System.out.println(names[1]);  // Last name




works only with 1.4.x^ ( i haven't tested it )
krishon




PostPosted: Thu Sep 25, 2003 7:54 pm   Post subject: (No subject)

its the string names [] an array (i haven't learned it yet, lol, we're on strings)
rizzix




PostPosted: Thu Sep 25, 2003 7:55 pm   Post subject: (No subject)

Yes!! ... wow you reply fast.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Sep 25, 2003 7:55 pm   Post subject: (No subject)

Very Happy I've just posted substring way up there /\

but I like the new .split way even more 8)

oh man, JAVA pwnz Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
krishon




PostPosted: Thu Sep 25, 2003 7:56 pm   Post subject: (No subject)

lol, yah...bout teh fast replyin...having msn on and having it notify can help Laughing
krishon




PostPosted: Thu Sep 25, 2003 7:59 pm   Post subject: (No subject)

lol, tony if i used ur code, my teacher would know someone's helping, cuz i dunno try and catch Very Happy
krishon




PostPosted: Thu Sep 25, 2003 8:02 pm   Post subject: (No subject)

hmm....while we're on the topic of java.....can someone write a tutorial about graphics...i dun't expect this if you are too busy, but it'll be great if u can write one Very Happy
Tony




PostPosted: Thu Sep 25, 2003 8:03 pm   Post subject: (No subject)

how do you get terminal input then? cuz my IDE doesnt let me use input stream without try/catch block Confused
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
krishon




PostPosted: Thu Sep 25, 2003 8:05 pm   Post subject: (No subject)

well our teacher taught us the GUI input....nice and ez, plus unlike turing I CAN GET THE VALUES FROM THE GUI BOX Very Happy, wow that's gotta be under the cons of turing Laughing
rizzix




PostPosted: Thu Sep 25, 2003 11:33 pm   Post subject: (No subject)

so krishon, what do mean graphics... as in displaying images.. manipulating images... or GUI!! ?? Confused
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 30 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: