Any body fix code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Any body fix code

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"}; if(choice<=4){ if(choice == 0){ System.out.println("PCs"); }else if (choice == 1){ System .out.println("Notebooks"); }else if (choice == 2){ System .out.println("Tablets"); }else if (choice == 3){ System .out.println("Phones"); }else if(choice ==4) { System .out.println("Accessories"); } } //complete the code try { if(choice >= 5) { throw new Exception("Invalid choice"); } } catch(Exception e) { System.out.println("Wrong Option"); }

24th Oct 2021, 1:06 AM
Amala Yakin
3 Answers
+ 3
Save your code in code playground and share it's link here. Don't paste your code in the question, or you might get less answers. In your code, it just missed two closing braces '}' at the end. Here is your code fixed. Happy learning =) https://code.sololearn.com/c3Z9Zd6qnPVw/?ref=app
24th Oct 2021, 1:39 AM
Rishi
Rishi - avatar
+ 1
Amala Yakin No need to write if else to print value because if there are thousands record in the array then it is not possible to write if else for all cases. Since categories is an array so you can directly pass choice which is an index of the array You can just do this: try { System.out.println(categories [choice]); } catch (Exception e) { System.out.println("Wrong Option"); }
24th Oct 2021, 3:19 AM
A͢J
A͢J - avatar
0
This wrong bro
31st Oct 2021, 12:26 AM
Amala Yakin