Computer Science 15-110, Spring 2010
Class Notes: Scanner methods and Console UI
Scanner Methods and Console UI
// This programs reads an int value and prints it. // For details, see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html import java.util.Scanner; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); System.out.println(x); } }
Another Example:
// This programs reads two int values and prints their sum. import java.util.Scanner; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println(x+y); } }
// This programs reads two double values and prints their sum.
import java.util.Scanner;
class MyCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
double y = scanner.nextDouble();
System.out.println(x+y);
}
}
// This programs reads two String values and prints their concatenation. import java.util.Scanner; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String x = scanner.next(); // next(), not nextString()! String y = scanner.next(); System.out.println(x+y); } }
// This program reads int values until the sentinel -1,
// and then prints out the sum (not including the sentinel!).
import java.util.Scanner;
class MyCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sentinel = -1;
int sum = 0;
int x = scanner.nextInt();
while (x != sentinel) {
sum += x;
x = scanner.nextInt();
}
System.out.println(sum);
}
}
Or, more concisely (and without duplicate code):
// This program reads int values until the sentinel -1,
// and then prints out the sum (not including the sentinel!).
import java.util.Scanner;
class MyCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sentinel = -1;
int sum = 0;
int x;
while ((x = scanner.nextInt()) != sentinel)
sum += x;
System.out.println(sum);
}
}
// This programs reads two int values and prints their sum. import java.util.Scanner; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int x = scanner.nextInt(); System.out.print("Enter another integer: "); int y = scanner.nextInt(); System.out.println(x+y); } }
// This programs reads two int values and prints their sum. import java.util.Scanner; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("This program reads two values and prints their sum."); System.out.print("Enter an integer: "); int x = scanner.nextInt(); System.out.print("Enter another integer: "); int y = scanner.nextInt(); System.out.println(x + " + " + y + " = " + (x+y)); } }
// This programs reads two int values and prints their quotient. import java.util.Scanner; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("This program reads two values and prints their quotient."); System.out.print("Enter the integer numerator: "); int x = scanner.nextInt(); System.out.print("Enter the integer denominator: "); int y = scanner.nextInt(); if (y == 0) System.out.println("Cannot divide by zero!"); else System.out.println(x + " / " + y + " = " + (x/y)); } }
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem