PrintWriter
for output, Scanner
for inputFile
parameter
new PrintWriter(new File("output.txt"))
new Scanner(new File("input.txt"), "UTF-8")
throws FileNotFoundException
to methodin.nextInt()/in.nextDouble()
in.nextLine()
in.next()
in.useDelimiter("[^A-Za-z]+")
, then in.next()
in.useDelimiter("")
, then in.next()
nextLine
, next
return String
substring
returns String
char
?char
do it wrongchar
values
char
is useful in the rare case that you need to classify characters
isDigit
, isLetter
, isUpperCase
, isLowerCase
, isWhiteSpace
char ch = in.next().charAt(0); if (Character.isLetter(ch)) ...
nextLine
and then analyze string
Scanner in = new Scanner(new File("input.txt")); String line = in.nextLine(); Scanner lineScanner = new Scanner(line); String countryName = lineScanner.next(); // Read first word // Add more words to countryName until number encountered while (!lineScanner.hasNextInt()) { countryName = countryName + " " + lineScanner.next(); } int populationValue = lineScanner.nextInt();
Integer.parseInt
, Double.parseDouble
trim
Integer.parseInt(" 13")
throws an exceptionDouble.parseDouble("$10.95")
throws an exceptionInteger.parseInt("1,000,000")
throws an exceptionAn input file contains lines such as
Fred 40 Wilma 28 Mary Ann 30
Consider this code to process a line:
String line = in.nextLine(); int n = line.lastIndexOf(" "); String name = line.substring(0, n); int age = Integer.parseInt(line.substring(n));
What is the problem with this code?
Integer.parseInt
throws an exceptionname
char
valuesstr.split(regex)
splits the string into an arrayline.split(" ")
splits along spaces
line
is "Mary Ann 30"
, get an array ["Mary", "Ann", "30"]
line.split("\\s+")
\s
matches any whitespace+
means one or more"[^A-Za-z0-9]"
Scanner.useDelimiter
[A-Z]
means all letters from A to Z^
means “not”Complete this program to find the sum of the numbers in the second to last column. Use split
.
The input file looks like this:
Abraham Lincoln 6 ft 4 in 193 cm Lyndon B. Johnson 6 ft 3 1⁄2 in 192 cm Thomas Jefferson 6 ft 2 1⁄2 in 189 cm ...
What result do you get?
1729 Mary Ann 1730 Wilma
int studentID = in.nextInt();
Harry
reads an empty string! int studentID = in.nextInt();
in.nextLine(); // Consume the \n
String name = in.nextLine();
Suppose the input contains the characters 6,995.00 12. What is the value of price and quantity after these statements?
double price = in.nextDouble(); int quantity = in.nextInt();
out.printf
and String.format
are your friendsout.printf("%-10s%10.2f", items[i] + ":", prices[i]);
Clams: 19.95 Lobsters: 109.95
x
prints hexadecimal: String.format("%4x", s.charAt(0));
%,.2f
prints decimal separators 100,000.00
%04d
prints leading zeroes 0001
Have another look at the example with the clams. Why can't you use the simpler form
out.printf("%-10s:%10.2f", items[i], prices[i]);
items[i]
as a string with %s
Total.java
from Section 11.1for f in *.txt ; do java Total $f ${f/.txt/.out} ; done
args
arraypublic static void main(String[] args)
-
-d
for decoding in CaesarCipher.java