Can you help me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Can you help me?

An online electronics store has 5 product categories - PCs, Notebooks, Tablets, Phones, and Аccessories. The program you are given declares an array and stores that types into it. Write a program to take N number as input and output the element of the array with N index. If the index is out of range, program should output "Wrong Option".

4th Jul 2021, 5:31 PM
ShadowRise
7 Answers
+ 10
import java.util.Scanner; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); String[] categories = {"PCs", "Notebooks", "Tablets", "Phones", "Аccessories"}; try { System.out.println(categories[choice]); } catch(Exception e) { System.out.println("Wrong Option"); } } }
4th Sep 2021, 3:58 PM
Fazal Haroon
Fazal Haroon - avatar
+ 4
You missed else statement. BTW, try this https://code.sololearn.com/c4CfF5T9lqV0/?ref=app
4th Jul 2021, 5:50 PM
Simba
Simba - avatar
0
Thank you
4th Jul 2021, 5:57 PM
ShadowRise
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); String[] categories = {"PCs", "Notebooks", "Tablets", "Phones", "Аccessories"}; //complete the code try { System.out.println(categories[choice]); } catch(Exception e) { System.out.println("Wrong Option"); } } }
22nd Jul 2021, 4:26 PM
Yosua Philip Sirait
Yosua Philip Sirait - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); String[] categories = {"PCs", "Notebooks", "Tablets", "Phones", "Аccessories"}; try { System.out.println(categories[choice]); } catch(Exception e) { System.out.println("Wrong Option"); } } }
11th Nov 2022, 3:25 PM
Pooja Patel
Pooja Patel - avatar
0
A local electronics shop has a great deal for notebooks. You want to buy as many notebooks as you can afford. For that, you need to calculate how many notebooks you can buy with the amount in your bank account and how much money will be left over. The given code includes comments as instructions for two tasks you need to complete. Follow the instructions to solve this code coach!
23rd Sep 2023, 3:35 AM
MUTHU M
MUTHU M - avatar
- 1
https://code.sololearn.com/c53a3a3a94A1 i know i cheated a little, but idk how to use that catch block
4th Jul 2021, 5:44 PM
ShadowRise