Source Code
ScannerExample.java
// import all the classes and interfaces from the java.util package into your program.
import java.util.*;
public class ScannerExample {
public static void main(String[] args) {
System.out.println("Scanner Example Code");
// 1. Print a message to the user
// 2. Initialize the Scanner. Declare a variable named in
// that reads from System.in
// 3. Create a variable and assign it to the value returned by nextLine
// The Scanner class provides a method called nextLine that reads a
// line of input from the keyboard and returns a String.
// 4. Print the value of the variable
// 5. Close the Scanner
}
}
ScannerExample2.java
import java.util.*;
public class ScannerExample2 {
public static void main(String[] args) {
System.out.println("Scanner Example 2 Code");
Scanner in = new Scanner(System.in);
System.out.print("What's your age? ");
int age = in.nextInt();
// add code to fix the "Scanner bug"
System.out.print("What's your name? ");
String line = in.nextLine();
System.out.println("Name: " + line);
System.out.println("Age: " + age);
in.close();
}
}
M1Assignment.java
import java.util.*;
public class M1Assignment {
public static void main(String[] args) {
System.out.println("Module 1 Assignment");
Scanner in = new Scanner(System.in);
in.close();
}
}
Last updated