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.basejavautil.Scanner.throwForScannerjava:
at java.basejavautil.Scanner.nextScannerjava:
at java.basejavautil.Scanner.nextIntScannerjava:
at java.basejavautil.Scanner.nextIntScannerjava:
at com.app.CavazosExample.mainCavazosExamplejava:
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 UsersenvyiDocumentsGitHubGeneralCavazosAppcavazosappsrcmainjavacomappcommandsjson";
private static String commandArray;
private static int lastIssuedCommandIndex ;
private static int redoCommandIndex ;
public static void mainString args
JSONArray commandJSONArray JSONFile.readArrayfileName;
commandArray getCommandArraycommandJSONArray;
initialize Scanner
Scanner scanner new ScannerSystemin;
Menu Commands
boolean running true;
while running
System.out.println
General Cavazos Commander App";
System.out.println;
System.out.printlni Issue a command";
System.out.printlnl List all of the commands";
System.out.printlnu Undo the last command that was issued";
System.out.printlnr Redo the last command that was issued";
System.out.printlnq Quit";
System.out.println;
System.out.printEnter 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.printlnQuitting the app...";
running false;
default System.out.printlnInvalid option. Please try again.";
scanner.close;
print list of all commands
private static void listCommands
System.out.println List of all commands ;
printcommandArray;
private static void issueRandomCommand
Random rand new Random;
lastIssuedCommandIndex rand.nextIntcommandArraylength;
System.out.printf
Issuing Random Cammands
s
commandArraylastIssuedCommandIndex;
redoCommandIndex ;
Undo last command
private static void undoCommand
if lastIssuedCommandIndex
System.out.printlnNo command to undo.";
else
System.out.printf
Undo Command
s
commandArraylastIssuedCommandIndex;
redoCommandIndex lastIssuedCommandIndex;
lastIssuedCommandIndex ;
Redo last command
private static void redoCommand
if redoCommandIndex
System.out.printlnNo command to redo.";
else
System.out.printf
Redo Command
s
commandArrayredoCommandIndex;
lastIssuedCommandIndex redoCommandIndex;
redoCommandIndex ; Reset redo index
print command array
public static void printString commandArray
System.out.printfNumbertCommand
;
System.out.printft
;
for int i ; i commandArray.length; i
System.out.printfdts
i commandArrayi;
get names from json object
public static String getCommandArrayJSONArray commandArray
String arr new StringcommandArraysize;
for int i ; i commandArray.size; i
arri commandArray.getitoString;
return arr;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
