Question: Hello! I just need some help debugging my scanning method / my get choice method. Every time I run this program I keep getting the

Hello! I just need some help debugging my scanning method / my get choice method. Every time I run this program I keep getting the same error of "Exception in thread "main" java.util.NoSuchElementException" . Please let me know what I am doing wrong! All help is appreciated TIA!
public static void main(String[] args){
final int MAX_DEPS =5;
double[] DEPOSIT ={MAX_DEPS};
double userDep =0;
int userChoice =0;
System.out.println("Hello, please make a selection!");
do {
userChoice = getChoice(userChoice);
switch (userChoice){
case 1:
addNewDep(DEPOSIT, MAX_DEPS, userDep);
break;
case 2:
depHistory(DEPOSIT, userDep);
break;
case 3:
System.out.println("You are now exiting the program, have a great day!");
break;
default:
System.out.println("Invalid selection. Please make a valid selection");
break;
}
}while(userChoice!=3);
}
public static int getChoice(int userChoice){
System.out.println("");
System.out.println("**** MAIN MENU ****");
System.out.println("1. Add New Deposit");
System.out.println("2. View Deposit History");
System.out.println("3. Exit");
System.out.println("");
Scanner choice = new Scanner(System.in);
userChoice = choice.nextInt();
choice.close();
return userChoice;
}
public static void addNewDep(double[] DEPOSIT, int MAX_DEPS, double userDep){
Scanner sc = new Scanner(System.in);
for (int i =0; i < MAX_DEPS; i++){
System.out.println("Please enter in the amount to deposit (or enter -1 to stop): ");
userDep = sc.nextDouble();
if (userDep ==-1){
break;
}
DEPOSIT[i]= userDep;
}
sc.close();
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!