+ 1

Introductory Java Array Vending Machine Question

Hello, I am working on the introductory java course, I have progressed to the vending machine array question and I am having trouble passing it. I wrote my own code which I thought should have worked , but It was not giving any output, so I checked the solution and changed my code accordingly. Still didn't pass the questions with no outputs. At this point I tried writing the code in a multitude of ways and checked my notes to make sure I wasnt making any small errors like a missing ; or a misspelled command but I could not get the code to give any outputs. At this point I copied the solution word for word, character for character because I understood the underlying concept which was good enough for me. And even when I copied the solution the code didnt pass. Can somebody please give me something I can copy and paste that will pass this question? I understand the underlying concept and I would like to continue with my course. The question is the following: You are making a program for a vending machine that provides drinks. The menu of the drinks is stored in an array called menu: String[] menu = {"Tea", "Espresso", "Americano", "Water", "Hot Chocolate"}; JAVA Take the choice of the customer as an integer from input and output the corresponding menu item. Also, check for errors: in case the input is out of the range of the array, output "Invalid". Any help is greatly appreciated, Thank you!

28th Feb 2023, 7:41 AM
Aries Kouklakis
Aries Kouklakis - avatar
9 Réponses
+ 4
if( choice < 0 || choice > 4 ) { // correct way
28th Feb 2023, 7:47 PM
Jayakrishna 🇮🇳
+ 2
Share your tried code here so that it helps to find where is going wrong..?
28th Feb 2023, 7:56 AM
Jayakrishna 🇮🇳
+ 2
import java.util.Scanner; public class Program { public static void main(String[] args) { String[] menu = {"Tea", "Espresso", "Americano", "Water", "Hot Chocolate"}; Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if (choice < 0 || choice > 4) { System.out.println("Invalid"); } else { System.out.println(menu[choice]); } } }
25th Oct 2023, 5:46 PM
Rasheedha Hanif
Rasheedha Hanif - avatar
0
Hello Jaya thank you for the reply, here is the code I have import java.util.Scanner; public class Program { public static void main(String[] args) { String[] menu = {"Tea", "Espresso", "Americano", "Water", "Hot Chocolate"}; Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if (choice < 0 || > 4){ System.out.println("Invalid"); } else{ System.out.println(menu[choice]); } } }
28th Feb 2023, 7:36 PM
Aries Kouklakis
Aries Kouklakis - avatar
0
here's the answer import java.util.Scanner; public class Program { public static void main(String[] args) { String[] menu = {"Tea", "Espresso", "Americano", "Water", "Hot Chocolate"}; Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if (choice < 0 || choice > 4) { System.out.println("Invalid"); } else { System.out.println(menu[choice]); } } }
2nd Oct 2023, 3:00 PM
Cord Ian Moraleta
Cord Ian Moraleta - avatar
0
How to Attempt? AAT ch the dough) Color Sandwich 13509 Sam is a cook, and he has colored breads and stuffing with which he had made some sandwiches. A sandwich can be made by keeping multiple or no stuffing between t same-colored breads like qaboq (ywhere abc represents stulling and a repromes coloured bread) The sandwiches are placed one over the other represented by S where each character depicts either bread or the stalling Yarkond and return a string value representing the colour of the broads wed in all samdeches Input Specification: input1: A string Srepresenting the sandwiches Output Specification: Return a string value representing the colour of the bread and off Example 1: input1qczcqu Output: qu Explanation Here the given onde
19th Sep 2024, 1:35 PM
Pavan Sharma
Pavan Sharma - avatar
0
How to Attempt? AAT ch the dough) Color Sandwich 13509 Sam is a cook, and he has colored breads and stuffing with which he had made some sandwiches. A sandwich can be made by keeping multiple or no stuffing between t same-colored breads like qaboq (ywhere abc represents stulling and a repromes coloured bread) The sandwiches are placed one over the other represented by S where each character depicts either bread or the stalling Yarkond and return a string value representing the colour of the broads wed in all samdeches Input Specification: input1: A string Srepresenting the sandwiches Output Specification: Return a string value representing the colour of the bread and off Example 1: input1qczcqu Output: qu Explanation Here the given onde
19th Sep 2024, 1:36 PM
Pavan Sharma
Pavan Sharma - avatar