Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lessons;
- import java.util.Scanner;
- //program asks name, age and annual income, then displays a greeting with name+age+income
- public class InputProblem
- {
- public static void main(String[] args)
- {
- Scanner keyboard = new Scanner(System.in);
- //'Scanner' is a class
- // 'keyboard' is an object from scanner class
- // 'system.in' monitors keyboard strokes
- int age;
- double income;
- String name;
- //'double' is a prim data type
- //'string' is a class, 'name' is a reference veriable
- System.out.print("whats your age?");
- age = keyboard.nextInt();
- keyboard.nextLine();
- System.out.print("hi whats your name?");
- //'system' is a class
- //'out' is an object
- //'print' is a method
- //before you do the keyboard.nextLine() declare another before it
- //so it fixes keyboard buffer issue, this is whenever you type stuff it goes in a
- //spot in memory, then when you call a string one itll read the spaces/tabs/enters you
- //typed before as the input not the actual input u just typed, calling another
- //'keyboard.nextLine(); before it will eliminate this.
- name = keyboard.nextLine();
- System.out.print("whats your annual income?");
- income = keyboard.nextDouble();
- // 'nextDouble()' is called a method
- // 'keyboard' is an OBJECT from the scanner class
- System.out.print("Greetings "+name+" you are "+age+
- " years old and have an annual salary of $"+income);
- keyboard.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement