+ 1

Can anyone tell me the correct code

PYTHON intermediate course >> file handling >> this code showing error it can't satisfy the test caseimport 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","Accessories"}; try { // Check if the input choice is within the valid range if (choice >= 0 && choice < categories.length) { System.out.println(categories[choice]); } else { // Out of bounds index System.out.println("Wrong Option\n"); } } catch (Exception e) { System.out.println("An error occurred"); // Capital 'E' in Exception } } } Tell me plz

24th Sep 2024, 1:04 PM
BHANUTEJA
BHANUTEJA - avatar
8 Answers
+ 6
BHANUTEJA the Python interpreter probably hasn't studied Java language. I am sure it is very confused by this code.
24th Sep 2024, 2:09 PM
Brian
Brian - avatar
+ 2
python: import java.util.Scanner; ?? wow *.* this is really new to me....
24th Sep 2024, 1:24 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 1
anyways, you don't need the if-else statement: delete the if line delete the } else { line but keep the else's output (without \n) and replace the existing output in the catch block.... if you are asking ChatGPT or similar AI tool to solve your problem, ask it to fix own bugs as well with explaining the code :-)
24th Sep 2024, 1:36 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 1
BHANUTEJA there is a known problem in the Sololearn editor that occurs when you paste from another source. Sometimes it ends up inserting non-printable extra characters in the leading spaces of indented lines. I found that it happened to your code. Your code passed after I reset the code back to the starting sample by using the menu option, and manually entered your logic. But note that the task says: "Use the Exception type to catch all possible exceptions." Your code is preventing the exception from happening and catching only one type of exception.
2nd Oct 2024, 5:20 AM
Brian
Brian - avatar
0
Brian šŸ‘šŸ˜‚
25th Sep 2024, 10:26 AM
Š•Š²Š³ŠµŠ½ŠøŠ¹
Š•Š²Š³ŠµŠ½ŠøŠ¹ - avatar
0
Java code in python?
25th Sep 2024, 5:08 PM
AĶ¢J
AĶ¢J - avatar
0
@Brain python intermediate āŒ Java intermediate āœ… Help me with the code Java intermediate -> exceptions handling problem
2nd Oct 2024, 3:05 AM
BHANUTEJA
BHANUTEJA - avatar
0
AĶ¢J No, In Java intermediate -> exceptions handling problem There is a bug in the test case that doesn't satisfy the test case, help me with the code
2nd Oct 2024, 3:09 AM
BHANUTEJA
BHANUTEJA - avatar