Homework 7 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
| Method | Description |
|---|---|
| next() | Reads and returns the next token as a String |
| nextDouble() | reads and returns a double value |
| nextInt() | reads and returns an int value |
| nextLine() | reads and returns the next line of input as a String |
| Method | Description |
|---|---|
| hasNext() | Returns true if there is another token to be read |
| hasNextInt() | Returns true if there is another integer token to be read |
| hasNextDouble() | Returns true if there is another decimal token to be read |
| hasNextLine() | Returns true if there is another line of text to be read |
while loop
import java.io.*;
import java.util.Scanner;
public class ReadFile {
public static void main (String[] args)
throws FileNotFoundException {
File file = new File(args[0]);
Scanner input = new Scanner(file);
while (input.hasNext()){
System.out.println();
}
}
}
1 * 4 = 4
0 * 2 = 0
1 * 1 = 1
----
5
int can hold is 2147483647int can hold is -2147483648
public class MaxMinInt {
public static void main (String[] args){
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
$ java MaxMinInt
2147483647
-2147483648
$ cat temps.txt 2017-06-01 67 2017-06-02 71 2017-06-03 69 2017-06-04 88 2017-06-05 74 ...
while loop header
import java.io.*;
import java.util.Scanner;
public class ReadRecords {
public static void main (String[] args)
throws FileNotFoundException {
File file = new File(args[0]);
Scanner input = new Scanner(file);
while (input.hasNextLine()){
System.out.println(input.nextLine());
}
}
}
$ java ReadRecords temps.txt
2017-06-01 67
2017-06-02 71
2017-06-03 69
2017-06-04 88
2017-06-05 74
...
String []
import java.io.*;
import java.util.Scanner;
public class ReadFields {
public static void main (String[] args)
throws FileNotFoundException {
File file = new File(args[0]);
Scanner input = new Scanner(file);
String line;
String [] fields;
while (input.hasNextLine()){
line = input.nextLine();
fields = line.split(" ");
for (int i = 0; i < fields.length; i++)
System.out.print(fields[i] + " ");
System.out.println();
}
}
}
$ java ReadFields temps.txt
2017-06-01 67
2017-06-02 71
2017-06-03 69
2017-06-04 88
2017-06-05 74
...
for loop used to print the individual fields for each line2017-06-0167 2017-06-0271 2017-06-0369 2017-06-0488 2017-06-0574 ...
import java.io.*;
import java.util.Scanner;
public class AverageTemps {
public static void main (String[] args)
throws FileNotFoundException {
File file = new File(args[0]);
Scanner input = new Scanner(file);
String line;
String [] fields;
int total = 0;
int count = 0;
while (input.hasNextLine()){
count += 1;
line = input.nextLine();
fields = line.split(" ");
total += Integer.parseInt(fields[1]);
}
System.out.println(total/count);
}
}
$ java AverageTemps temps.txt
77
static int average(File file)
throws FileNotFoundException {
Scanner input = new Scanner(file);
String line;
String [] fields;
int total = 0;
int count = 0;
while (input.hasNextLine()){
count += 1;
line = input.nextLine();
fields = line.split(" ");
total += Integer.parseInt(fields[1]);
}
return total/count;
}
if statementthrows clause in the method header
static int days_above(File file, int average)
throws FileNotFoundException {
Scanner input = new Scanner(file);
String line;
String [] fields;
int days_above = 0;
int temp;
while (input.hasNextLine()){
line = input.nextLine();
fields = line.split(" ");
temp = Integer.parseInt(fields[1]);
if (temp > average) {
days_above += 1;
}
}
return days_above;
}
}
public static void main (String[] args)
throws FileNotFoundException {
File file = new File(args[0]);
System.out.println(days_above(file, average(file)));
}
$ java DaysAbove temps.txt 13
public class Cheer1 {
public static void main (String[] args){
cheer("Sox");
}
public static void cheer(String team){
System.out.println("Go " + team + "!");
}
}
$ java Cheer1
Go Sox!
public class Cheer2 {
public static void main (String[] args){
cheer();
}
public static void cheer(){
String team = "Sox";
System.out.println("Go " + team + "!");
}
}
$ java Cheer2
Go Sox!
public class Cheer3 {
public static void main (String[] args){
print_cheer("Sox");
}
public static void print_cheer(String team){
System.out.println(cheer(team));
}
public static String cheer(String team){
return "Go " + team + "!";
}
}
public class Cheer4 {
public static void main (String[] args){
String team = "Pats";
cheer(team);
System.out.println(team);
}
public static void cheer(String team){
System.out.println("Go " + team + "!");
}
}
$ java Cheer4
Go Pats!
Pats