import java.util.Scanner;
Scanner console = new Scanner(System.in);
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 |
java Lab2 < myData.txt
Where in the above, myData.txt is the text file containing your input data for testing.
System.out.print("\nEnter a number of seconds as a whole number or enter 0 to exit: ");
int seconds = console.nextInt();
public static void foo()...
public static void bar( int num )...
public static void fubar( int num, String name )...
// Bad method
public static void upperCase( String str ) {
str = str.toUpperCase();
}
String str = "hello";
upperCase( str ); // Will not work - str remains "hello"
// Good method
public static String upperCase( String str ) {
return str.toUpperCase();
}
String str = "hello";
str = upperCase( str ); // This works.
if (currentScore > maxScore) {
System.out.println("A new high score!");
maxScore = currentScore;
}
if (<test>) {
<statement>;
<statement>;
...
<statement>;
}
if (<test>) {
<statement>;
<statement>;
...
<statement>;
} else {
<statement>;
<statement>;
...
<statement>;
}
<expression> <relational operator> <expression>
numChars < MAX_LENGTH
x >= 4
while (<test>) {
<statement>;
<statement>;
...
<statement>;
}
int number = 1;
int max = 10;
while (number <= max) {
System.out.println("Hi there");
number++;
}
int number = 1;
int max = 10;
while (true) {
System.out.println("Hi there");
number++;
if (number>max) {
break;
}
}
Lecture 4 sample is here (click on page, cut and paste)