IT – Java programming – summary – Grade 11 – Scanner
Description
Scanner: Read a line from keyboard: (NEVER asked in an exam)
below package:
import [Link].*;
Instantiate an object of the scanner class and read from [Link]
Scanner scInput = new Scanner ([Link]);
Type prompt in System. out
[Link]("Type in a sentence");
Save the line entered into a string variable
String line = [Link]();
Processes / display line (see notes below) OR you can replace the line above with the reading of individual components
using nextInt(), nextDouble() and next(), but keep in mind that any white space is considered a seperator between
components, unless you use useDelimiter.
[Link]("Enter an integer number");
int iNum = [Link]();
[Link]("Enter a real number");
Double rNum = [Link]();
[Link]("Enter a word");
String sWord = [Link]();
May give problem, especially if more than one word is entered!! Might have to use an if ([Link]()) with it or a
blank [Link]()
Close the scanner class
[Link]();
Read a line from text file:
To link an existing text file to your program: copy the text file into the same folder as your program (in the case of
Netbeans and Eclipse, copy it into the project (not the package))
below package:
import [Link].*; import [Link].*;
throw / catch a FileNotFound Exception
If you catch, then everything below must be in the try statement.
Instantiate an object of the scanner class, that reads from the text file
Scanner scFile = new Scanner ( new File( "[Link]" ));
OR can combine with BufferedReader
Scanner scFile = new Scanner ( new BufferedReader( new FileReader( "[Link]" )));
Read items from the line until there is no more
while ([Link]()){
Store line into a string variable
String line = [Link]();
Processes / display line (see notes below)
}//Close bracket
Close the scanner class
[Link]();
Break a line (a string) into various components:
Instantiate an object of the scanner class that reads the string variable that holds the line.
State which delimiter is used.
Scanner scLine = new Scanner (line).useDelimiter("#");
Read items from the line until there is no more
while ([Link]()){
Get the individual components, in the sequence they are stored in the line, and save them in variables of the correct type,
e.g.
String firstName = [Link]();
int mark = [Link]();
double price = [Link]();
String message = [Link]();
Other types: page 41 of grade 11 textbook
Can also use an array if you make use of a counter
Processes / display components
}//Close bracket
Processes / display components
Close the scanner class
[Link]();
Complete method to read from text file:
try
{
Scanner scFile = new Scanner ( new File( "[Link]" ));
while ([Link]())
{
String line = [Link]();
try
{//optional – to catch any potential problems, e.g. reading real number into integer variable
Scanner scLine = new Scanner (line).useDelimiter("#");
String firstName = [Link]();
int mark = [Link]();
double price = [Link]();
String message = [Link]();
[Link](firstName+"\t"+mark+"\t"+price+"\t"+message);
[Link]();
}
catch (Exception e){[Link]("ERROR: "+e);}
}
[Link]();
}
catch (FileNotFoundException x) {[Link]("FILE NOT FOUND. ERROR: "+x);}
To write to a text file:
char ans;
try {
FileWriter out = new FileWriter("[Link]");
//to append to end of file change to: FileWriter out = new FileWriter("[Link]",true);
do
{
String sName = [Link]("Enter name");
String sMark = [Link]("Enter mark");
String sPrice = [Link]("Enter price");
String sMessage = [Link]("Enter message");
String line = sName+"#"+sMark+"#"+sPrice+"#"+sMessage+"\n";
[Link](line);
ans = [Link]([Link]
("Do you want to enter more on this line? (Y/N").charAt(0));
} while (ans == 'Y');
[Link]();
} catch (IOException e) { [Link]("ERROR: "+e);
}
Resources:
Introduction:
o [Link]
o [Link]
o [Link]
o
Summary of Scanner methods:
o [Link]
Using BufferedReader: [Link]
List of exceptions: [Link]