Question: In Java, how can I change the user input to accept characters for the commands I need the code to accept the characters ( i

In Java, how can I change the user input to accept characters for the commands
I need the code to accept the characters (i, l, u, r, q) for the user input commands, but the console is giving the following error:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.app.CavazosExample.main(CavazosExample.java:37)
The code I have is this:
package com.app;
import java.util.Random;
import java.util.Scanner;
import org.json.simple.JSONArray;
public class CavazosExample {
private static final String fileName ="/Users/envyi/Documents/GitHub/General_Cavazos_App/cavazosapp/src/main/java/com/app/commands.json";
private static String[] commandArray;
private static int lastIssuedCommandIndex =-1;
private static int redoCommandIndex =-1;
public static void main(String[] args){
JSONArray commandJSONArray = JSONFile.readArray(fileName);
commandArray = getCommandArray(commandJSONArray);
// initialize Scanner
Scanner scanner = new Scanner(System.in);
// Menu Commands
boolean running = true;
while (running){
System.out.println("
General Cavazos Commander App");
System.out.println("-----------------------------------------");
System.out.println("i Issue a command");
System.out.println("l List all of the commands");
System.out.println("u Undo the last command that was issued");
System.out.println("r Redo the last command that was issued");
System.out.println("q Quit");
System.out.println("-----------------------------------------");
System.out.print("Enter a command: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice){
case 'l'-> listCommands();
case 'i'-> issueRandomCommand();
case 'u'-> undoCommand();
case 'r'-> redoCommand();
case 'q'->{
System.out.println("Quitting the app...");
running = false;
}
default -> System.out.println("Invalid option. Please try again.");
}
}
scanner.close();
}
// print list of all commands
private static void listCommands(){
System.out.println("----- List of all commands -----");
print(commandArray);
}
private static void issueRandomCommand(){
Random rand = new Random();
lastIssuedCommandIndex = rand.nextInt(commandArray.length);
System.out.printf("
--- Issuing Random Cammands ---
%s
", commandArray[lastIssuedCommandIndex]);
redoCommandIndex =-1;
}
// Undo last command
private static void undoCommand(){
if (lastIssuedCommandIndex ==-1){
System.out.println("No command to undo.");
}
else {
System.out.printf("
--- Undo Command ---
%s
", commandArray[lastIssuedCommandIndex]);
redoCommandIndex = lastIssuedCommandIndex;
lastIssuedCommandIndex =-1;
}
}
// Redo last command
private static void redoCommand(){
if (redoCommandIndex ==-1){
System.out.println("No command to redo.");
}
else {
System.out.printf("
--- Redo Command ---
%s
", commandArray[redoCommandIndex]);
lastIssuedCommandIndex = redoCommandIndex;
redoCommandIndex =-1; // Reset redo index
}
}
// print command array
public static void print(String[] commandArray){
System.out.printf("Number\tCommand
");
System.out.printf("------\t---------------
");
for (int i =0; i < commandArray.length; i++){
System.out.printf("%04d\t%s
", i, commandArray[i]);
}
}
// get names from json object
public static String[] getCommandArray(JSONArray commandArray){
String[] arr = new String[commandArray.size()];
for (int i =0; i < commandArray.size(); i++){
arr[i]= commandArray.get(i).toString();
}
return arr;
}
}

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 Programming Questions!