You are on page 1of 3

7-1

We will consider how to input from the keyboard the three data types.

, and

Use the
method to input an
from the keyboard:
import java.io.*; //
import java.util.*;
public class Tester
{
public static void main( String args[] )
{
Scanner kbReader = new Scanner(System.in); //
System.out.print(Enter your integer here. ); //enter
int i = kbReader.nextInt( );
System.out.println(3 * i); //prints
}
}
Use the
method to input a
from the keyboard:
import java.io.*;
import java.util.*;
public class Tester
{
public static void main( String args[] )
{
Scanner kbReader = new Scanner(System.in);
System.out.print(Enter your decimal number here. ); //
double d = kbReader.nextDouble( );
System.out.println( 3 * d ); //prints
}
}
Use the
method to input a
from the keyboard:
import java.io.*;
import java.util.*;
public class Tester{
public static void main( String args[] )
{
Scanner kbReader = new Scanner(System.in);
System.out.print(Enter your String here. ); //Enter
String s = kbReader.next( ); //inputs up to first white space
System.out.println( This is the first part of the String, + s);
s = kbReader.next( );
System.out.println( This is the next part of the String, + s);
}
}

7-2
Output would be as shown below:
Enter your String here. One Two
This is first part of the String,... One
This is next part of the String,... Two
In a similar way
and
can be used multiple times to parse data
input from the keyboard. For example, if
is input from the keyboard, then
can be applied four times to access these four integers separated by white space.
Inputting a
(it could contain spaces) from the keyboard using
import java.io.*;
import java.util.*;
public class Tester
{
public static void main( String args[] )
{
Scanner kbReader = new Scanner(System.in);
System.out.print(Enter your String here. ); //Enter
String s= kbReader.nextLine( );
System.out.println( This is my string, + s);
}
}

Output would be as shown below:


Enter your String here. One Two
This is my string,... One Two
We must
two classes,.
inputting integers,
s, and
importing.

that provide methods for


s. See Appendix I for more on the meaning of

In the above three examples we used the following code:


Scanner kbReader = new Scanner(System.in);
It simply creates the keyboard reader
(we arbitrarily named it
)
that provides access to the
,
,
, and
methods. For now just accept the necessity of all thisit will all be explained
later.
The
class used here to create our keyboard reader object only applies
to1.5.0_xx or higher versions of Java. For older versions, see Appendix M for an
alternate way to obtain keyboard input.

An anomaly
single Scanner
nextLine( )

nextInt( ) nextDouble( ) next( )


any sequence
nextInt( ) nextDouble( )
Scanner

nextLine( )

RadiusOfCircle

sqrt( )

nextLine( )

Project Going in Circles

Math

What is the area?

Radius of your circle is 139.4.

Project Whats My Name?


String
FullName
What is your first name? Cosmo
What is your last name? Kramer
Your full name is Cosmo Kramer.

PI

You might also like