Home arrow static arrow Java Programming [Archive] - how to split input into multiple variables?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how to split input into multiple variables?
This topic has 8 replies on 1 page.

Posts:6
Registered: 8/9/04
how to split input into multiple variables?  
Aug 9, 2004 5:34 PM



 
I am very new to Java, apologies if this is a newbie question.

I want to ask the user to enter 3 numbers (console input), all at once, space delimited, and insert them into three different variables. I am wanting to know how to accomplish this with SDK 1.4.2. With SDK 1.5 you can do:

Scanner scan = Scanner.create (System.in)
System.out.print("Enter three numbers: ");
num1 = scan.nextInt();
num2 = scan.nextInt();
num3 = scan.nextInt();

Thanks for any help, appreciate it.

Brian
 

Posts:37,103
Registered: 3/30/99
Re: how to split input into multiple variables?  
Aug 9, 2004 5:38 PM (reply 1 of 8)



 
[url http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)]String.split[/url]
[url http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html]StringTokenizer[/url]

Either of the above will do.
 

Posts:6
Registered: 8/9/04
Re: how to split input into multiple variables?  
Aug 9, 2004 5:53 PM (reply 2 of 8)



 
Thanks,

I did look into String.split and implemented it using that like so:

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try {
System.out.print("Enter three integers: ");
result = stdin.readLine().split("
s");
stra = result[0];
num1 = Integer.parseInt(stra);
strb = result[1];
num2 = Integer.parseInt(strb);
strc = result[2];
num3 = Integer.parseInt(strc);
}
 

Posts:16
Registered: 7/26/04
Re: how to split input into multiple variables?  
Aug 9, 2004 5:56 PM (reply 3 of 8)



 
Thanks,

I did look into String.split and implemented it using
that like so:

BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));

try {
System.out.print("Enter three integers: ");
result = stdin.readLine().split("
s");
stra = result[0];
num1 = Integer.parseInt(stra);
strb = result[1];
num2 = Integer.parseInt(strb);
strc = result[2];
num3 = Integer.parseInt(strc);
}

what was the end result when you tried to do it this way?
 

Posts:6
Registered: 8/9/04
Re: how to split input into multiple variables?  
Aug 9, 2004 6:02 PM (reply 4 of 8)



 
The end result was that it worked fine. Complete program is as follows (an adaptation of a lesson from Java Software Solutions, Lewis and Loftus, ISBN 0-321-24583-0):

import java.io.*;

public class MinOfThree {
// read three integers from the user and determines the smallest value.
public static void main (String args[]) {
int num1 = 0, num2 = 0, num3 = 0, min = 0;
String stra, strb, strc;
String[] result;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try {
System.out.print("Enter three integers: ");
result = stdin.readLine().split("
s");
stra = result[0];
num1 = Integer.parseInt(stra);
strb = result[1];
num2 = Integer.parseInt(strb);
strc = result[2];
num3 = Integer.parseInt(strc);
}
catch (Exception exc) {
System.out.println("Invalid Input");
System.exit(1);
}

if (num1 < num2)
if (num1 < num3)
min = num1;
else
min = num3;
else
if (num2 < num3)
min = num2;
else
min = num3;

System.out.println("Minimum value: " + min);
}
}
 

Posts:16
Registered: 7/26/04
Re: how to split input into multiple variables?  
Aug 9, 2004 6:07 PM (reply 5 of 8)



 
Then I am confused as to what the question is. Can you clarify please?
 

Posts:6
Registered: 8/9/04
Re: how to split input into multiple variables?  
Aug 9, 2004 6:13 PM (reply 6 of 8)



 
lucky,

When jverd told me about the String.split, I used that, and it worked. I think we just have a miscommunication. Originally, when I posted the question, I was not aware of how to accomplish this. After I posted, I was able to accomplish it using String.split, and now all is well.

Brian
 

Posts:16
Registered: 7/26/04
Re: how to split input into multiple variables?  
Aug 9, 2004 6:22 PM (reply 7 of 8)



 
Oh dear me! I am sorry, I thought you wanted to do it a different way or something! LOL
 

Posts:37,103
Registered: 3/30/99
Re: how to split input into multiple variables?  
Aug 9, 2004 6:56 PM (reply 8 of 8)



 
lucky,

When jverd told me about the String.split, I used
that, and it worked. I think we just have a
miscommunication. Originally, when I posted the
question, I was not aware of how to accomplish this.
After I posted, I was able to accomplish it using
String.split, and now all is well.

Brian

Cool. Glad you got it working.
 
This topic has 8 replies on 1 page.