Introductory Java Array Vending Machine Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
6 Answers
+ 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
And I decided to buy a coffee machine, it would be much easier than trying to program something. I was lucky enough to find the https://connectvending.co.uk/coffee-machines/office-coffee-machines/rex-royal-s2/ Rex Royal S2 here and learn as much as I could about it. It really is a very high quality machine. So I can safely recommend it for you as well. I hope it was useful and was able to help.
11th Apr 2024, 2:18 AM
Garold Rafa
Garold Rafa - avatar