Author |
Message |
krishon
|
Posted: 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

|
|
 |
Tony

|
Posted: 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");
}
}
}
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
krishon
|
Posted: 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
|
Posted: 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
|
Posted: Thu Sep 25, 2003 7:42 pm Post subject: (No subject) |
|
|
yah...he did thx tony  |
|
|
|
|
 |
rizzix
|
Posted: 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
|
Posted: 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
|
Posted: Thu Sep 25, 2003 7:55 pm Post subject: (No subject) |
|
|
Yes!! ... wow you reply fast. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Thu Sep 25, 2003 7:55 pm Post subject: (No subject) |
|
|
I've just posted substring way up there /\
but I like the new .split way even more 8)
oh man, JAVA pwnz  |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
krishon
|
Posted: 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  |
|
|
|
|
 |
krishon
|
Posted: 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  |
|
|
|
|
 |
krishon
|
Posted: 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  |
|
|
|
|
 |
Tony

|
Posted: 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  |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
krishon
|
Posted: 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 , wow that's gotta be under the cons of turing  |
|
|
|
|
 |
rizzix
|
Posted: 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!! ??  |
|
|
|
|
 |
|