I'm trying to set up a boolean flag so that the user must enter 'M' (Mark the student answers) before they are able to choose 'S' and 'Q'. If they choose these, there should be a message saying "Please mark the answers before inputting statistics". The rest of the options should always be available for the user but I'm not sure how to set up a boolean so that the user must choose 'M' before choosing 'S' and 'Q'. Anyone know how to do this? Here is my code so far:
public class Marker_Menu
{
public static void main(String args[])throws IOException
{
Quiz_Marker input2 = new Quiz_Marker();
char arg[]= null;
System.out.println ("Welcome to the Quiz Grading System \n");
char choice = menu();
while(choice != 'E')
{
switch (choice)
{
case 'C':
input2.corAnsPrint();
break;
case 'A':
input2.stuAnsPrint();
break;
case 'M':
input2.quizMarking();
break;
case 'S':
input2.stuStatsPrint();
break;
case 'Q':
input2.quesStatsPrint();
break;
default:
System.out.println("Your choice is invalid");
}
choice = menu();
}
System.out.println("Thank you for using the Quiz Marker System");
System.exit(0);
}
public static char menu() throws IOException
{
System.out.println ("Please enter your choice \n" +
" C - Print Correct Answers \n" +
" A - Print Student Answers \n" +
" M - Mark the Student Answers \n" +
" S - Produce the Quiz Statistics \n" +
" Q - Produce Question Statistics \n" +
" E - Exit the System");
Scanner input = new Scanner (System.in);
char choice = input.next().toUpperCase().charAt(0);
return choice;
}
}
Just create a boolean variable, set it when 'M' is selected, and test it if 'S' or 'Q' is selected:
public static void main(String args[])throws IOException
{
boolean mSelected = false;
...
while(choice != 'E')
{
switch (choice)
{
...
case 'M':
mSelected = true;
input2.quizMarking();
break;
case 'S':
if (mSelected) {
input2.stuStatsPrint();
} else {
System.out.println("Please mark the answers before inputting statistics");
}
break;
case 'Q':
if (mSelected) {
input2.quesStatsPrint();
} else {
System.out.println("Please mark the answers before inputting statistics");
}
break;
...
}
...
Related
I have a project in college to create a login menu type application.
I am very much a beginner so please bear with me.
Can I ask for someone to point me in the right direction as I've hit a bit of a blank on this one.
The application isn't finished so I understand there will be more that needs adding to it, just know that it is only a very barebones application I am looking to build.
After choosing option 1, the app tells the user to first change password and attempts. After changing the password and the attempts (I'm changing it to 5 when testing) and re-choosing option 1, this error comes up -
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at loginmenu.LoginMenu.loginAttempt(LoginMenu.java:74)
at loginmenu.LoginMenu.showMenu(LoginMenu.java:34)
at loginmenu.LoginMenu.loginAttempt(LoginMenu.java:77)
at loginmenu.LoginMenu.showMenu(LoginMenu.java:34)
at loginmenu.LoginMenu.main(LoginMenu.java:12)
Here is the code -
package loginmenu;
import java.util.Scanner;
public class LoginMenu {
private static String correctPassword;
public static String userPassword;
public static int attemptsCounter;
public static boolean loggedIn;
public static void main(String[] args) {
showMenu();
loginAttempt();
}
public static void showMenu()
// displays a menu and keeps displaying until user chooses the QUIT option
{
int userChoice;
Scanner myScan = new Scanner(System.in);
do {
System.out.println("1. Login");
System.out.println("2. Change Password");
System.out.println("3. Change Attempts");
System.out.println("4. Quit");
userChoice = myScan.nextInt();
switch (userChoice) {
case 1: {
System.out.println("You chose to login.");
loginAttempt();
break;
}
case 2: {
System.out.println("You chose to change password.");
Scanner myNewScan = new Scanner(System.in);
System.out.println("Please enter a password.");
userPassword = myNewScan.nextLine();
break;
}
case 3: {
System.out.println("You chose to change attempts.");
Scanner myNewScan = new Scanner(System.in);
System.out.println("Please enter amount of attempts.");
attemptsCounter = myNewScan.nextInt();
break;
}
case 4: {
System.out.println("You have quit the program.");
break;
}
default: {
System.out.println("Not a valid choice.");
}
}// closes switch
} while (userChoice != 4);
myScan.close();
System.out.println("Goodbye.");
}// closes showMenu1
public static void loginAttempt()
{
Scanner scan = new Scanner(System.in);
while (correctPassword != userPassword) && (attemptsCounter>=5);
{
System.out.println("Please change password and attempts first.");
showMenu();
}
if (userPassword == correctPassword) && (attemptsCounter<=5)
{
System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
}
if (attemptsCounter>=6)
{
System.out.println("You have been locked out.");
}
}
}
Fix your brackets and ditch the semicolon and don't use ==/!= for strings.
while (correctPassword != userPassword) && (attemptsCounter>=5);
should be
while (!correctPassword.equals(userPassword) && (attemptsCounter>=5))
same issue with brackets here:
if (userPassword == correctPassword) && (attemptsCounter<=5)
This is my approach of your question.
First, i set the my password at the first place.
private static String correctPassword = "tommybee";
And, i can call the showMenu method only in this case.
public static void main(String[] args) {
showMenu();
}
You don't have to call the showMenu method in your loginAttempt and the while statement is also removed.
public static void loginAttempt() {
if (!(userPassword.toLowerCase().equals(correctPassword)) && (attemptsCounter >= 5)) {
System.out.println("Please change password and attempts first.");
}
if ((correctPassword.toLowerCase().equals(userPassword)) && (attemptsCounter <= 5)) {
System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
}
if (attemptsCounter >= 6) {
System.out.println("You have been locked out.");
}
}
In the showMenu method, i use only one scanner class with system's input stream.
Check the scanner class.
Declare a Scanner class and initialize a userChoice variable to 4.
Scanner myScan = new Scanner(System.in);
int userChoice = 4;
Here is showMenu method.
public static void showMenu()
// displays a menu and keeps displaying until user chooses the QUIT option
{
Scanner myScan = new Scanner(System.in);
int userChoice = 4;
do {
System.out.println("Choose one of the list below");
System.out.println("1. Login");
System.out.println("2. Change Password");
System.out.println("3. Change Attempts");
System.out.println("4. Quit");
if(myScan.hasNextInt())
{
userChoice = myScan.nextInt();
switch (userChoice) {
case 1: {
System.out.println("You choose to login.");
System.out.println("Please enter a password.");
userPassword = myScan.next();
System.out.println("pass " + userPassword);
loginAttempt();
break;
}
case 2: {
System.out.println("You choose to change password.");
System.out.println("Please enter a new password.");
correctPassword = myScan.next();
break;
}
case 3: {
System.out.println("You choose to change attempts.");
System.out.println("Please enter amount of attempts.");
attemptsCounter = myScan.nextInt();
break;
}
case 4: {
System.out.println("You have quit the program.");
break;
}
default: {
System.out.println("Not a valid choice.");
}
}// closes switch
}
} while (myScan.hasNext() && userChoice != 4);
myScan.close();
System.out.println("Goodbye.");
}// closes showMenu1
I use a next method instead of a nextInt method.
See the next method of the api document.
This method may block while waiting for input to scan,
Here is what i have done.
import java.util.Scanner;
public class LoginMenu {
private static String correctPassword = "tommybee";
public static String userPassword;
public static int attemptsCounter;
public static boolean loggedIn;
public static void main(String[] args) {
showMenu();
//loginAttempt();
}
public static void showMenu()
// displays a menu and keeps displaying until user chooses the QUIT option
{
Scanner myScan = new Scanner(System.in);
int userChoice = 4;
do {
System.out.println("Choose one of the list below");
System.out.println("1. Login");
System.out.println("2. Change Password");
System.out.println("3. Change Attempts");
System.out.println("4. Quit");
if(myScan.hasNextInt())
{
userChoice = myScan.nextInt();
switch (userChoice) {
case 1: {
System.out.println("You choose to login.");
System.out.println("Please enter a password.");
//Scanner myNewScan = new Scanner(System.in);
userPassword = myScan.next();
//myNewScan.close();
System.out.println("pass " + userPassword);
loginAttempt();
break;
}
case 2: {
System.out.println("You choose to change password.");
//Scanner myNewScan = new Scanner(System.in);
System.out.println("Please enter a new password.");
correctPassword = myScan.next();
//myNewScan.close();
break;
}
case 3: {
System.out.println("You choose to change attempts.");
//Scanner myNewScan = new Scanner(System.in);
System.out.println("Please enter amount of attempts.");
attemptsCounter = myScan.nextInt();
//myNewScan.close();
break;
}
case 4: {
System.out.println("You have quit the program.");
break;
}
default: {
System.out.println("Not a valid choice.");
}
}// closes switch
}
} while (myScan.hasNext() && userChoice != 4);
myScan.close();
System.out.println("Goodbye.");
}// closes showMenu1
public static void loginAttempt() {
//while ((correctPassword != userPassword) && (attemptsCounter >= 5)) {
if (!(userPassword.toLowerCase().equals(correctPassword)) && (attemptsCounter >= 5)) {
System.out.println("Please change password and attempts first.");
//showMenu();
}
if ((correctPassword.toLowerCase().equals(userPassword)) && (attemptsCounter <= 5)) {
System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
}
if (attemptsCounter >= 6) {
System.out.println("You have been locked out.");
}
}
}
I am having some problem with my java assignment and currently am stuck at the nested if else statement.I am actually trying to get an input age from the user and store in as a variable.After executing my code i am getting error when i run the program.Am i doing this correctly or is there some other way to program this?
Error message that i got
Enter your selection:
1
You have selected No.1
Please enter your age**Exception in thread "main"
java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Assignment.main(Assignment.java:48)**
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println(
"1. Add player" + "\n" +
"2.Display transaction" + "\n" +
"3.Begin the ride");
System.out.println("");
System.out.println("Enter your selection: ");
char selection = sc.findInLine(".").charAt(0);
sc.close();
switch(selection) {
case '1' :
System.out.println("You have selected No.1");
break;
case '2' :
System.out.println("You have selected No.2" );
break;
case '3' :
System.out.println("You have selected No.3" );
break;
default:
System.out.println("Please make a selection");
break;
}
if (selection=='1') {
int age=0;
System.out.print("Please enter your age");
age = sc.nextInt();
while (age<100) {
age+=age;
}
}
else if (selection=='2') {
System.out.println("Display daily transaction");
}
else if (selection=='3') {
System.out.println("Begin the ride");
}
else {
System.out.println("Please enter a valid input");
}
}
Please read basic Java so that you can learn programs better.
you can reduce this code to a very good extent, i haven't removed nested-if statements, which you can by moving all execution under case statements.
This program will terminate if option entered is not a number, this can also be improved by reading it as a string and checking is its one of the valid options (map's key set).
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class SwitchCase {
public static Map<Integer, String> options = new LinkedHashMap<Integer, String>();
static {
options.put(1, "Add player");
options.put(2, "Display transaction");
options.put(3, "Begin the ride");
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
for (Integer option : options.keySet()) {
System.out.println(option + ". " + options.get(option));
}
System.out.println("Enter your selection: ");
int selection = sc.nextInt();
switch (selection) {
case 1:
case 2:
case 3:
System.out.println("You have selected No." + selection);
break;
default:
System.out.println("Please make a selection");
break;
}
if (selection == 1) {
int age = 0;
System.out.print("Please enter your age : ");
age = sc.nextInt();
while (age < 100) {
age += age;
}
System.out.println(age);
} else if (selection == 2) {
System.out.println(options.get(selection));
} else if (selection == 3) {
System.out.println(options.get(selection));
} else {
System.out.println("Please enter a valid input!");
}
}
}
switch case is a type of nested if else.we can use switch in place of nested if else.you need not to use both switch case and nested if else simultaneously.
you can write your code like this.
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("1. Add player" + "\n" +
"2.Display transaction" + "\n" +
"3.Begin the ride");
System.out.println("");
System.out.println("Enter your selection: ");
char selection = sc.findInLine(".").charAt(0);
switch(selection) {
case '1' :
System.out.println("You have selected No.1");
int age=0;
System.out.print("Please enter your age");
age = sc.nextInt();
while (age<100) {
age+=age;
}
System.out.print(age);
break;
case '2' :
System.out.println("You have selected No.2" );
System.out.println("Display daily transaction");
break;
case '3' :
System.out.println("You have selected No.3" );
System.out.println("Begin the ride");
break;
default:
System.out.println("Please make a selection");
System.out.println("Please enter a valid input");
break;
}
sc.close();
}
The exception is pretty clear on the problem: java.lang.IllegalStateException: Scanner closed
Before your code enters the switch, you're closing the scanner with sc.close();. But later on you're trying to read from it again:
if (selection=='1') {
int age=0;
System.out.print("Please enter your age");
age = sc.nextInt(); // <---- Here
while (age<100) {
age+=age;
}
}
But this will fail if the scanner is already closed. To solve this, you can simply put the line sc.close() to the end of your main.
As an alternative you could wrap everything into a try-with-resources block, so that you don't have to care about closing the scanner anymore because it will be closed automatically after leaving the block.
I want to make an array of integer values that represent counters so I initialized it like this in my main method:
int [] counters = new int [7];
counters [countListAll] = 0;
counters [countEmployeeReport] = 0;
counters [countDivisionReport] = ;
counters [countSalaryReport] = 0;
counters [countRetirementReport] = 0;
counters [countMain] = 0;
counters [countOthers] = 0;
And now I have to pass this array to my menu() method so that I can increment each counter each time any of the options is chosen.
public static void menu(int [] counters)
{
System.out.println("You have accessed Menu()");
System.out.println("Enter 'L' for list of the employee data available. \nEnter 'E' to dislpay information on a particular employee. \nEnter 'D' to display division information. \nEnter 'S' to display salary information. \nEnter 'R' to display retirement information. \nEnter 'Q' to quit Menu and return to Main.");
Scanner scan = new Scanner(System.in);
String first = scan.next();
char first1 = first.charAt(0);
if (first1 == 'L'|| first1 =='l'||first1 =='E'||first1 =='e'||first1 =='D'||first1 =='d'||first1 =='S'||first1 =='s'||first1 =='R'||first1 =='r'||first1 =='Q'||first1 =='q')
{
switch (first1)
{
case 'L':
case 'l':
listAll();
counters [countListAll] ++;
break;
case 'E':
case 'e':
employeeReport();
counters [countEmployeeReport] ++;
break;
case 'D':
case 'd':
divisionReport();
counter [countDivisionReport] ++;
break;
case 'S':
case 's':
salaryReport();
counters [countSalaryReport] ++;
break;
case 'R':
case 'r':
retirementReport();
counters [countRetirementReport] ++;
break;
case 'Q':
case 'q':
counters [countMain] ++;
break;
else
{
menu();
countOthers++;
}
}
Am I initializing the array correctly and passing it into menu() method correctly? And am I allowed to increment the objects like that?
EDIT: I changed the code, and this is the new code
Main method:
int [] counters = new int [7];
counters [L] = 0;//listAll
counters [E] = 0;//employeeReport
counters [D] = 0;//divisionReport
counters [S] = 0;//salaryReport
counters [R] = 0;//retirementReport
counters [Q] = 0;//quit
counters [O] = 0;//others
Menu Method:
public static void menu(int [] counters)
{
System.out.println("You have accessed Menu()");
System.out.println("Enter 'L' for list of the employee data available. \nEnter 'E' to dislpay information on a particular employee. \nEnter 'D' to display division information. \nEnter 'S' to display salary information. \nEnter 'R' to display retirement information. \nEnter 'Q' to quit Menu and return to Main.");
Scanner scan = new Scanner(System.in);
String first = scan.next();
char first1 = first.charAt(0);
if (first1 == 'L'|| first1 =='l'||first1 =='E'||first1 =='e'||first1 =='D'||first1 =='d'||first1 =='S'||first1 =='s'||first1 =='R'||first1 =='r'||first1 =='Q'||first1 =='q')
{
switch (first1)
{
case 'L':
case 'l':
listAll();
counters [L] ++;
break;
case 'E':
case 'e':
employeeReport();
counters[E]++;
break;
case 'D':
case 'd':
divisionReport();
counters [D]++;
break;
case 'S':
case 's':
salaryReport();
counters [S]++;
break;
case 'R':
case 'r':
retirementReport();
counters [R]++;
break;
case 'Q':
case 'q':
counters [Q]++;
break;
}
}
else
{
menu();
counters [O]++;
}
}
And FinalStats Method:
public static void finalStats(int [] counters)
{
System.out.println("Number of times listAll() was accessed from menu() is: " + counters[L]);
System.out.println("Number of times employeeReport() was accessed from menu() is: " + counters[E]);
System.out.println("Number of times divisionReport() was accessed from menu() is: " + counters[D]);
System.out.println("Number of times salaryReport() was accessed from menu() is: " + counters[S]);
System.out.println("Number of times retirementReport() was accessed from menu() is: " + counters[R]);
System.out.println("Number of times 'Quit' was chosen from menu() is: " + counters[Q]);
System.out.println("Number of times any other key was pressed in menu() is: " + counters[O]);
}
Yes, in general. There appears to be a bug with your countOthers logic. although nesting a switch with-in an if seems an odd choice, it would be much more readable with a simple if else-if chain (and you could use Character.toLowerCase(char) to handle mixed case. Something like,
if-else
public static void menu(int[] counters) {
System.out.println("You have accessed Menu()");
System.out.println("Enter 'L' for list of the employee data available.");
System.out.println("Enter 'E' to dislpay information on a particular employee.");
System.out.println("Enter 'D' to display division information.");
System.out.println("Enter 'S' to display salary information.");
System.out.println("Enter 'R' to display retirement information.");
System.out.println("Enter 'Q' to quit Menu and return to Main.");
Scanner scan = new Scanner(System.in);
String first = scan.next();
char first1 = Character.toLowerCase(first.charAt(0));
if (first1 == 'l') {
listAll();
counters[countListAll]++;
} else if (first1 == 'e') {
employeeReport();
counters[countEmployeeReport]++;
} else if (first1 == 'd') {
divisionReport();
counter[countDivisionReport]++;
} else if (first1 == 's') {
salaryReport();
counters[countSalaryReport]++;
} else if (first1 == 'r') {
retirementReport();
counters[countRetirementReport]++;
} else if (first1 == 'q') {
counters[countMain]++;
} else {
menu();
counters[countOthers]++; // <-- instead of countOthers++
}
}
switch-case
It is also possible to express the above if-else chain with switch-case and something like,
switch (Character.toLowerCase(first.charAt(0))) {
case 'l':
listAll();
counters[countListAll]++;
break;
case 'e':
employeeReport();
counters[countEmployeeReport]++;
break;
case 'd':
divisionReport();
counter[countDivisionReport]++;
break;
case 's':
salaryReport();
counters[countSalaryReport]++;
break;
case 'r':
retirementReport();
counters[countRetirementReport]++;
case 'q':
counters[countMain]++;
break;
default:
menu();
countOthers++;
}
I am just trying to call the methods to the main for each switch when it happens, but i just get the error message everytime i try to call any methods, not trying to return anything. ex. if the user enters a or A i want to call the add method to main
public static void main(String[] args)
{
char character;
Scanner keyboard = new Scanner(System.in);
while (character != 'E' || character != 'e')
{
System.out.println(" A:Addition \n S:Subtraction \n M:Multiplication \n D:Division \n R:Modulus \n E:exit");
switch (character)
{
case 'a':
case 'A':
System.out.println("your choice A");
add();
break;
case 's':
case 'S':
System.out.println("your choice S");
subtraction();
break;
case 'm':
case 'M':
System.out.println("your choice M");
multiplication();
break;
case 'd':
case 'D':
System.out.print("your choice D");
division();
break;
case 'r':
case 'R':
System.out.println("your choice R");
modulus();
break;
default:
System.out.println("Error: please enter a valid letter");
break;
}
}
}
public static void add(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a + b;
System.out.println(a + "plus" + b + "is" + total );
}
public static void subtraction(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a-b;
System.out.println(a + "minus" + b + "is " + total);
}
public static void multiplication(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a*b;
System.out.println(a + "times" + b + "is " + total);
}
public static void division(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a/b;
System.out.println(a + "divided" + b + "is " + total);
}
public static void modulus(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total= a%b;
System.out.println(a + "modulus" + b + "is " + total);
System.out.println("The program is terminating");
}
}
you're calling the method but you didn't include an argument
take a look at this.
public static void add(Scanner keyboard)
you have an argument, so you must include an argument when calling this method
so
you must call the method like this.
add(keyboard);
You have defined the method which takes Scanner as argument but you are calling the methods with no args.
All the method you are using are supposed to receive a Scanner object while you pass no argument.
For example you call add(); while it signature is
public static void add(Scanner keyboard)
Which is why you get the error.
Instead, use add(keyboard) and repeat the same for substraction, multiplication, division and modulus methods.
So that your switch would now look like
switch (character) {
case 'a':
case 'A':
System.out.println("your choice A");
add(keyboard);
break;
case 's':
case 'S':
System.out.println("your choice S");
subtraction(keyboard);
break;
case 'm':
case 'M':
System.out.println("your choice M");
multiplication(keyboard);
break;
case 'd':
case 'D':
System.out.print("your choice D");
division(keyboard);
break;
case 'r':
case 'R':
System.out.println("your choice R");
modulus(keyboard);
break;
default:
System.out.println("Error: please enter a valid letter");
break;
}
You are missing the arguments in the method call.
case 'a':
case 'A':
System.out.println("your choice A");
add(keyboard); // Add arguments.
break;
Here is the code:
package classes;
import java.util.*;
public class Introduction {
Scanner Input = new Scanner(System.in);
int classChoose;
boolean repeat = false;
public void Introduction() {
System.out.println("\t===THE QUEST FOR PERSEPOLIS===\tv 1.0\n");
System.out.println("Please choose a class: ");
System.out.print("(1)Elite Knight\t");
System.out.print("(2)Dawnguard\n");
System.out.print("(3)Archer\t\t\t");
System.out.print("(4)Barbarian\n");
System.out.print("(5)Mage\t\t\t");
System.out.print("(6)Swordsman\n");
System.out.println("(7)Crossbowman\t");
do {
try {
repeat = false;
classChoose = Input.nextInt();
while(classChoose < 1 || classChoose > 7) {
repeat = false;
System.out.println("Error. Enter a number between 1 and 7(inclusive).");
classChoose = Input.nextInt();
}
}
catch(InputMismatchException e) {
repeat = true;
System.err.println("Caught: "+e);
Input.nextLine();
}
}while(repeat = true);
switch(classChoose) {
case 1: chooseKnight();
break;
case 2: chooseGuard();
break;
case 3: chooseArcher();
break;
case 4: chooseBarbarian();
break;
case 5: chooseMage();
break;
case 6: chooseSwordsman();
break;
case 7: chooseCrossbowman();
break;
}
}
public static void chooseKnight() {
System.out.println("You have chosen the Elite Knight. You will be briefed and then you shall be set "
+"on your quest!");
}
static void chooseGuard() {
System.out.println("You have chosen the Dawnguard. You will be briefed and then you shall be set "
+"on your quest!");
}
static void chooseArcher() {
System.out.println("You have chosen the Archer. You will be briefed and then you shall be set "
+"on your quest!");
}
static void chooseBarbarian() {
System.out.println("You have chosen the Barbarian. You will be briefed and then you shall be set "
+"on your quest!");
}
static void chooseMage() {
System.out.println("You have chosen the Mage. You will be briefed and then you shall be set "
+"on your quest!");
}
static void chooseSwordsman() {
System.out.println("You have chosen the Swordsman. You will be briefed and then you shall be set "
+"on your quest!");
}
static void chooseCrossbowman() {
System.out.println("You have chosen the Crossbowman. You will be briefed and then you shall be set "
+"on your quest!");
}
}
Everytime I run it, the program prompts me to choose my class. After I enter my choice, the program does not go on to the switch statement below the do loop. Can someone help me fix this?
-Calvin
This is an assignment:
while(repeat = true); // Note single '=', not '=='
and the result of which will always be true, from section 15.26 Assignment Operators of the Java Language Specification:
At run time, the result of the assignment expression is the value of the variable
after the assignment has occurred.
Change to:
while(repeat);
while(repeat = true);
should be: -
while(repeat == true); // Or better: - while(repeat);
And in your catch, change Input.nextLine() to Input.next() : -
catch(InputMismatchException e) {
repeat = true;
System.err.println("Caught: "+e);
Input.nextLine(); // Change to Input.next()
}
And your instance variable should begin with a lowercase alphabet or an underscore.. So change Input to input.