how to split input into multiple variables?

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

[542 byte] By [signal@shreve.net] at [2007-9-30 15:00:25]
# 1
[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.
jverd at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...
# 2

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

}

signal@shreve.net at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...
# 3

> 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?

lucky48390 at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...
# 4

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

}

}

signal@shreve.net at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...
# 5
Then I am confused as to what the question is. Can you clarify please?
lucky48390 at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...
# 6

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

signal@shreve.net at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...
# 7
Oh dear me! I am sorry, I thought you wanted to do it a different way or something! LOL
lucky48390 at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...
# 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.

jverd at 2007-7-5 21:33:07 > top of Java-index,Archived Forums,Java Programming...