Source Code
Session2.java
import java.util.*;
public class Session2 {
public static void main(String args[]) {
String firstName = ""; // add your name here
System.out.println("Session 2 - " + firstName);
// Example 1
// Example 2
// Example 3
// Example 4
// Example 5
// Example 6
// Example 7
// Example 8
// Example 9
// Example 10
// Example 11
// Example 12
// Example 13
// Example 14
// Example 15
}
}
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
}
}
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? ");
in = new Scanner(System.in);
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);
}
}
Last updated