java Flashcards
The program uses import java.util.Scanner;
for access to the Scanner class.
Scanner inSS = null;
declares a Scanner object.
The statement inSS = new Scanner(userInfo);
assigns the associated input stream with a copy of userInfo. Then, the program can extract data from the scanner inSS using the family of next() methods (e.g., next(), nextInt(), nextDouble(), etc.).
A common use of string streams is
to process user input line-by-line.
inSS = new Scanner(lineString);
uses the Scanner’s constructor to initialize the stream’s buffer to the String lineString.
output string stream
can be created that is associated with a string rather than with the screen (standard output)
An output string stream is created using both
1) the StringWriter
import java.io.StringWriter;
2) PrintWriter classes
import java.io.PrintWriter;
The StringWriter class provides
a character stream that allows a programmer to output characters
The PrintWriter class is
a wrapper class that augments(الزيادات) character streams, such as StringWriter, with print() and println() methods that allow a programmer to output various data types
To create a PrintWriter object,
the program must first create a StringWriter, passing the StringWriter object to the constructor for the PrintWriter.
PrintWriter object provides
the print() and println() methods for writing to the stream,
StringWriter object provides
the toString() method for getting the resulting String
userInput = scnr.nextLine();
inSS = new Scanner(userInput);
userMonth = inSS.next();
userDate =inSS.nextInt();
userYear = inSS.nextInt();
read
output formatting
programmer can adjust the way that a program’s output appears
the standard output stream System.out provides the methods
1- printf() and
2- format()
format string
specifies the format of the text to print along with any number of placeholders for printing numeric values.
The placeholders are known as
format specifiers
format specifier
specifies the type of value to print in its place
sub-specifier
provides formatting options for a format specifier and are included between the % and format specifier character.