Java Reference
In-Depth Information
try
{
System.out.printf(
"
Input int (e.g. %4d):
"
,3501);
int int
-
val
=
scanner.nextInt ();
System.out.println (
"
You entered
"
+ int
-
val +
"\
n
"
);
System.out.printf ("Input float (e.g. %5.2f): ", 2.43);
float float
-
val = scanner.nextFloat ();
System.out.println ("You entered" + float
-
val +"\n");
System.out.printf ("Input double (e.g. %6.3e): ",
4.943e15);
double double
-
val = scanner.nextDouble ();
System.out.println (
"
You entered
"
+ double
-
val +
"\
n
"
);
}
catch (InputMismatchException e)
{
System.out.println (
"
Mismatch exception:
"
+ e);
}
}
// main
}
// class ScanConsoleApp
A session with the program
ScanConsoleApp
is shown here:
Input int (e.g. 3501): 23431
You entered 23431
Input float (e.g. 2.43): 1.2343
You entered 1.2343
Input double (e.g. 4.943e+15): -2.34e4
You entered -23400.0
There are a number of other useful methods in the
Scanner
class such as
skip()
to skip over some input,
useDelimiter()
to set the delimiter, and
findInLine()
to search for substrings. The
Scanner
class uses tools from
the
java.util.regex
package for pattern matching with regular expressions.
We don't have space here to describe these very powerful text-matching tools but
you can find more info in the Java 2 API Specifications.
9.5 The
File
class
Files and directories are accessed and manipulated via the
java.io.File
class.
The
File
class does not actually provide for input and output to files. It simply
provides an
identifier
of files and directories. Remember that just because a
File
object is created, it does not mean there actually exists on the disk a file with the
identifier held by that
File
object.
Search WWH ::
Custom Search