Mr. Truncale - Robert E. Lee High School
  • Home
  • Digital Media
    • HTML
  • Video Game Design
  • Comp. Programming I
  • Comp. Programming II
  • AP Computer Science
  • 3D Printing
  • Teacher Resources
  • Contact Me

Java - Input Notes


// Must include the import statement at top - import java.util.Scanner;
// Scanner object - Scanner keyboard = new Scanner(System.in);

Scanner keyboard =new Scanner(System.in);

// Input an int
System.out.print("Enter an int: ");
int i = keyboard.nextInt();
System.out.println(i);

// Input a double
System.out.print("Enter a double: ");
double d = keyboard.nextDouble();
System.out.println(d);

// Input a String
System.out.print("Enter a String: ");
String s = keyboard.next();
System.out.println(s);

// NOTE: you should use print instead of println for input prompt
DEMO PROGRAM

// Demo: Have user enter their first name, their favorite number and 
//       the calculation of their birthYear/birthMonth

Scanner keyboard = new Scanner(System.in);
String name;
double favNum;
double birthYear;
double birthMonth;
double calculation;

System.out.print("What is your name? ");
name = keyboard.next();
System.out.print("What is your favorite number? ");
favNum = keyboard.nextDouble();
System.out.print("What year were you born? ");
birthYear = keyboard.nextDouble();
System.out.print("What is your birth month? (number) ");
birthMonth = keyboard.nextDouble();

calculation = birthYear/birthMonth;

System.out.println("Hello "+name+"! Your favorite num is "+favNum);
System.out.println("Your special number is "+calculation);

Videos


lesson7_part1.swf
File Size: 4968 kb
File Type: swf
Download File

lesson7_part2.swf
File Size: 3647 kb
File Type: swf
Download File

lesson7_part3.swf
File Size: 2894 kb
File Type: swf
Download File

lesson7_part4.swf
File Size: 2634 kb
File Type: swf
Download File

Robert E. Lee High School
1809 Market
Baytown, TX 77520
281-420-4535
digital media | computer programming I and II | BIM
ap computer science | architectural cad | video game design
national technical honor society | contact me