fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. //Initialize variables for product names, prices, and quantities and assign intial value
  13. String productName1;
  14. double productPrice1 = 0.0;
  15. double productQuantity1 = 0.0;
  16. String productName2;
  17. double productPrice2 = 0.0;
  18. double productQuantity2 = 0.0;
  19. String productName3;
  20. double productPrice3 = 0.0;
  21. double productQuantity3 = 0.0;
  22.  
  23. //Initialize Scanner
  24. Scanner input = new Scanner(System.in);
  25.  
  26. //Get input values for all three products (9 total values)
  27. System.out.print("Enter product name: ");
  28. productName1 = input.nextLine();
  29. System.out.println();
  30. System.out.printf("Enter %s price: ", productName1);
  31. System.out.printf("Got %s as input", productName1);
  32. productPrice1 = input.nextDouble();
  33. System.out.println();
  34. System.out.printf("Enter %s weight/quantity: ", productName1);
  35. productQuantity1 = input.nextDouble();
  36. System.out.println();
  37.  
  38. System.out.println();
  39.  
  40. System.out.print("Enter product name: ");
  41. productName2 = input.nextLine();
  42. System.out.println();
  43. System.out.printf("Enter %s price: ", productName2);
  44. System.out.printf("Got %s as input", productName2);
  45. productPrice2 = input.nextDouble();
  46. System.out.println();
  47. System.out.printf("Enter %s weight/quantity: ", productName2);
  48. productQuantity2 = input.nextDouble();
  49. System.out.println();
  50.  
  51. System.out.println();
  52.  
  53. System.out.print("Enter product name: ");
  54. productName3 = input.nextLine();
  55. System.out.println();
  56. System.out.printf("Enter %s price: ", productName3);
  57. productPrice3 = input.nextDouble();
  58. System.out.println();
  59. System.out.printf("Enter %s weight/quantity: ", productName3);
  60. productQuantity3 = input.nextDouble();
  61. System.out.println();
  62.  
  63. System.out.println();
  64.  
  65. //Print out the 36 dashes
  66. System.out.println("------------------------------------");
  67.  
  68. //Enter first three lines after the dashes required for output and then add a blank lines
  69. System.out.println("SaveMart Receipt");
  70. System.out.println("123 Main Street");
  71. System.out.println("San Jose CA 95135");
  72. System.out.println(" ");
  73. }
  74. }
Runtime error #stdin #stdout #stderr 0.07s 4386816KB
stdin
Honey Crisp Apples
1.30
2.0
Captain Crunch
3.30
2
Wonder Bread
1.50
3
stdout
Enter product name: 
Enter Honey Crisp Apples price: Got Honey Crisp Apples as input
Enter Honey Crisp Apples weight/quantity: 

Enter product name: 
Enter  price: Got  as input
stderr
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:864)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextDouble(Scanner.java:2413)
	at Ideone.main(Main.java:45)