Beginner's question re: using the readLine() method

D

Dave

The program below takes an argument entered at the command line and
simply returns a float variable and then an integer.

public class Changer {
public static void main(String[] arguments) {
if (arguments.length > 0) {
System.out.println("The original value: "
+ arguments[0]);
Float num1 = new Float(arguments[0]);
float num2 = num1.floatValue();
int num3 = (int)num2;
System.out.println("The final value: " + num3);
}
}
}

Now let's say I want to input the argument at the command line, but
rather than doing, say:

C:>java Changer 7 (i.e. entering the argument on the command line
itself) ...

I want the program to request, and then process, the input using the
readLine() method, as follows:

C:> java Changer
> Type a number, please.
> ... and so on ...

My questions are:
(1) What's the syntax I need in order to implement the permutation of
readLine() that I want?
(2) What lines of code must I add to the program and where?


Thanks,
Dave
 
A

Arne Vajhøj

Dave said:
Now let's say I want to input the argument at the command line, but
rather than doing, say:

C:>java Changer 7 (i.e. entering the argument on the command line
itself) ...

I want the program to request, and then process, the input using the
readLine() method, as follows:

C:> java Changer
> Type a number, please.
> ... and so on ...

My questions are:
(1) What's the syntax I need in order to implement the permutation of
readLine() that I want?
(2) What lines of code must I add to the program and where?

Look at the scanner class.

http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top