Advertisement
476179

inputProblems-mitchell

Oct 9th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. package lessons;
  2.  
  3. import java.util.Scanner;
  4. //program asks name, age and annual income, then displays a greeting with name+age+income
  5.  
  6. public class InputProblem
  7. {
  8.  
  9. public static void main(String[] args)
  10. {
  11. Scanner keyboard = new Scanner(System.in);
  12. //'Scanner' is a class
  13. // 'keyboard' is an object from scanner class
  14. // 'system.in' monitors keyboard strokes
  15.  
  16. int age;
  17. double income;
  18. String name;
  19. //'double' is a prim data type
  20. //'string' is a class, 'name' is a reference veriable
  21.  
  22.  
  23.  
  24. System.out.print("whats your age?");
  25. age = keyboard.nextInt();
  26. keyboard.nextLine();
  27. System.out.print("hi whats your name?");
  28. //'system' is a class
  29. //'out' is an object
  30. //'print' is a method
  31.  
  32. //before you do the keyboard.nextLine() declare another before it
  33. //so it fixes keyboard buffer issue, this is whenever you type stuff it goes in a
  34. //spot in memory, then when you call a string one itll read the spaces/tabs/enters you
  35. //typed before as the input not the actual input u just typed, calling another
  36. //'keyboard.nextLine(); before it will eliminate this.
  37. name = keyboard.nextLine();
  38.  
  39. System.out.print("whats your annual income?");
  40. income = keyboard.nextDouble();
  41. // 'nextDouble()' is called a method
  42. // 'keyboard' is an OBJECT from the scanner class
  43.  
  44. System.out.print("Greetings "+name+" you are "+age+
  45. " years old and have an annual salary of $"+income);
  46. keyboard.close();
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement