I need help because I don’t know why my code terminates after I enter a value using the Scanner method. Is there a problem with the syntax?. Please help.
here is the code:
import java.util.Scanner;
public class Objects {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int[] num1 = {3, 2, 7};
int[][] num2 = {
{2, 9, 0},
{3, 1, 5},
};
String[][] thing = {
{"Phone", "Pencil"},
{"Eraser", "Mouse"},
{"Paper", "tissue"},
};
int[] newNum1 = new int[2];
int[][] newNum2 = new int[2][2];
newNum1[0] = 5;
newNum1[1] = 2;
newNum2[0][0] = 21;
newNum2[0][1] = 43;
newNum2[1][0] = 27;
newNum2[1][1] = 98;
System.out.println("Which of the following do you want to access?");
System.out.println("string or number?");
System.out.print("Answer: ");
String choice = console.next();
if(choice == "string") {
System.out.println("Which row do you want to access?");
System.out.print("Choice: ");
int a = console.nextInt();
System.out.println("Which item do you want to access?");
System.out.print("Choice: ");
int b = console.nextInt();
String result = thing[a][b];
System.out.println("You have chosen the item: " + result);
} else if(choice == "number") {
System.out.println("What specific number do you want to access?");
System.out.print("num1, num2, newNum1, or newNum2");
String pick = console.next();
if(pick == "num1") {
System.out.println("This numbers has only 1 array");
System.out.println("Which position do you want to pick?");
int a = console.nextInt();
int result = num1[a];
System.out.println("The number chosen is: " + result);
} else if(pick == "num2") {
System.out.println("Row 1 or Row 2?");
int a = console.nextInt();
System.out.println("Which position?");
int b = console.nextInt();
int result = num2[a][b];
System.out.println("The chosen number is: " + result);
} else if(pick == "newNum1") {
System.out.println("This numbers has only 1 array");
System.out.println("Which position do you want to pick?");
int a = console.nextInt();
int result = num1[a];
System.out.println("The number chosen is: " + result);
} else if(pick == "newNum2") {
System.out.println("Row 1 or Row 2?");
int a = console.nextInt();
System.out.println("Which position?");
int b = console.nextInt();
int result = num2[a][b];
System.out.println("The chosen number is: " + result);
} else {
System.out.println("Invalid choice");
}
}
}
}