The variable disappears at the end of the case [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I have a problem because I do not know how to save the variable. The variable newname and the newpassword deafult are zero. But in case 1 they are changed to the given values, but after case 1 the variables return to the basic values 0. and i cant log in (in case 2) becasue login and password always are 0. How i can globally set this variable on case 1?
String newname = null;
String newpassword = null;
System.out.println("Hello!");
System.out.println();
System.out.println(" ****************************************");
System.out.println(" * MENU *");
System.out.println(" ****************************************");
System.out.println(" 1. Create new account");
System.out.println(" 2. Log in");
System.out.println(" 3. Help");
System.out.println(" 0. End");
Scanner opcje = new Scanner(System.in);
int choose = opcje.nextInt();
switch (choose) {
case 1:
System.out.println("You choose create new acount\n Enter the login");
Scanner nlogin = new Scanner(System.in);
newname = nlogin.nextLine();
System.out.println("Please enter the password ");
Scanner npassword = new Scanner(System.in);
newpassword = npassword.nextLine();
System.out.println("the account has been created\n");
case 2:
Scanner login = new Scanner(System.in);
System.out.println("Login:");
String pass1 = login.nextLine();
System.out.println("Password:");
Scanner password = new Scanner(System.in);
String pass2 = password.nextLine();
if (pass1 == newname & pass2 == newpassword){
System.out.println("you are logged in");
}else{
System.out.println("incorrect passoword or login");
}
break;
case 3:
System.out.println("Help is off");
break;
case 0:
System.out.println("ending");
break;
default:
System.out.println("Select the option by pressing 1,2,3 or 0");
break;
}
}
}

One of the problems is with this line:
if (pass1 == newname & pass2 == newpassword){
System.out.println("you are logged in");
}else{
System.out.println("incorrect passoword or login");
}
If you will debug this code, You will notice that this if statement, doesn't
compare the values of the String. For more details you may visit:
https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
Try this code instead:
if(pass1.equals(newname) && pass2.equals(newpassword))
{
System.out.println("you are logged in");
}else{
System.out.println("incorrect passoword or login");
}
second problem:
you need to put your switch statement in a while loop:
1.This why the program won't end (and this way the variable) will be saved.
2.IF the user input is invalid instead of going to default, the program will also ask again for the user to write a number.

There are so many issues here.
You are missing a break; after the first case
You potentially have a scope issue for your variables if they are simply local variables in the method and you call the method repeatedly then their contents are simply forgotten in between calls.
You are comparing Strings using == , Read More here => Java String Equals
Correct Code:
public class MainClass {
String newname = null;
String newpassword = null;
//continue your code here
//call menu() when required
menu();
}
public void menu()
{
System.out.println("Hello!");
System.out.println();
System.out.println(" ****************************************");
System.out.println(" * MENU *");
System.out.println(" ****************************************");
System.out.println(" 1. Create new account");
System.out.println(" 2. Log in");
System.out.println(" 3. Help");
System.out.println(" 0. End");
Scanner opcje = new Scanner(System.in);
int choose = opcje.nextInt();
switch (choose)
{
case 1:
System.out.println("You choose create new acount\n Enter the login");
Scanner nlogin = new Scanner(System.in);
newname = nlogin.nextLine();
System.out.println("Please enter the password ");
Scanner npassword = new Scanner(System.in);
newpassword = npassword.nextLine();
System.out.println("the account has been created\n");
break;
case 2:
Scanner login = new Scanner(System.in);
System.out.println("Login:");
String pass1 = login.nextLine();
System.out.println("Password:");
Scanner password = new Scanner(System.in);
String pass2 = password.nextLine();
if (pass1.equals(newname) & pass2.equals(newpassword)){
System.out.println("you are logged in");
}else{
System.out.println("incorrect password or login");
}
break;
case 3:
System.out.println("Help is off");
break;
case 0:
System.out.println("ending");
break;
default:
System.out.println("Select the option by pressing 1,2,3 or 0");
break;
}
}

Try changing the way you compare Strings, also use java logical operators instead of Bitwise &.
static String newname = null;
static String newpassword = null;
public static void main(String[] args) {
System.out.println("Hello!");
System.out.println();
System.out.println(" ****************************************");
System.out.println(" * MENU *");
System.out.println(" ****************************************");
System.out.println(" 1. Create new account");
System.out.println(" 2. Log in");
System.out.println(" 3. Help");
System.out.println(" 0. End");
Scanner opcje = new Scanner(System.in);
int choose = opcje.nextInt();
switch (choose) {
case 1:
System.out.println("You choose create new acount\n Enter the login");
Scanner nlogin = new Scanner(System.in);
newname = nlogin.nextLine();
System.out.println("Please enter the password ");
Scanner npassword = new Scanner(System.in);
newpassword = npassword.nextLine();
System.out.println("the account has been created\n");
case 2:
Scanner login = new Scanner(System.in);
System.out.println("Login:");
String pass1 = login.nextLine();
System.out.println("Password:");
Scanner password = new Scanner(System.in);
String pass2 = password.nextLine();
//Java uses equals method to compare Strings
//Java also uses && as the logical operator for "and"
if (pass1.equals(newname) && pass2.equals(newpassword)) {
System.out.println("you are logged in");
} else {
System.out.println("incorrect password or login");
}
break;
case 3:
System.out.println("Help is off");
break;
case 0:
System.out.println("ending");
break;
default:
System.out.println("Select the option by pressing 1,2,3 or 0");
break;
}
}

Related

I'm having trouble with a java login menu app

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.");
}
}
}

Java if else with nested loop

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.

Restrict Switch Statement order

I am writing a menu in java using a switch statement and while loop. I am looking to find a way of ensuring the user completes menu one before proceeding to menu two.
Here is an example piece of code:
(Please note I normally pass data using setters and getters, this is just a quick program I wrote for this question)
package menuOrder;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int option = 0;
String Fname = null;
String Sname = null;
int Age = 0;
while(option !=5){
System.out.println("1. Enter Firstname");
System.out.println("2. Enter Surname");
System.out.println("3. Enter Age");
System.out.println("4. Display Details");
System.out.println("5. System Exit");
option = sc.nextInt();
switch(option){
case 1:
System.out.println("Please enter your Firstname >");
Fname = sc.next();
break;
case 2:
System.out.println("Please enter your Surname >");
Sname = sc.next();
break;
case 3:
System.out.println("Please enter your Age >");
Age = sc.nextInt();
break;
case 4:
System.out.println("Firstname = " + Fname + "\nSurname = " + Sname + "\nAge = " + Age);
break;
case 5:
System.out.println("You have chosen to System Exit!!!");
System.exit(0);
break;
default:
System.out.println("Invalid Entry!! \nPlease try again");
}
}
}
}
I am trying to prevent the use from entering their Surname before their Firstname.
Can someone please help?
Thanks
First show only the first name. Then when the user enters first name call surname method from that method.Use method calls instead of switch case.

Java: How to use System.in Several times in one program?

On my previous question I've learned that I shouldn't close the System.in, and I also learned that using multiple Scanner together can cause problems.
The problem is that I need some methods recursively to use the Scanner to scan some inputs from the user.
Example
public void usersChoice(){
int choice = 0;
while(true){
try{
Scanner scan = new Scanner(System.in);
System.out.print("Choose an option: ");
choice = scan.nextInt();
break;
}
catch(Exception e){
System.out.println("Invalid Input!\n");
}
}
switch (choice){
case 1:
choiceNewAccount();
break;
case 2:
choiceLogin();
break;
case 3:
choiceRemoveAccount();
break;
case 4:
exit();
break;
default:
System.out.println("Invalid Input!\n");
break;
}
Case 1:
public void createAccount(){
try{
Scanner scan = new Scanner(System.in);
System.out.print("Enter Your Username: ");
String username = scan.next();
if(!isValidUsername(username)) System.out.println("Username Already Exists!");
else{
while(true){
System.out.print("Enter Your Password: ");
String pass1 = scan.next();
System.out.print("Re-Enter Your Passowrd: ");
String pass2 = scan.next();
if(!arePasswordsMatch(pass1,pass2)) System.out.println("Passowrds Don't Match!");
else {
addToAccountsList(username,pass1);
createTheUsersFile(username);
System.out.println("The Account Has Been Successfully Created!");
break;
}
}
}
} catch(Exception e){
System.out.println("Could Not Create Account!");
}
Do I need to create one System.in Object and use it on my whole program? Or is there an other way to work with the System.in Object?

Using Scanner inside a for loop for system input

I have been struggling with this for a while. I essentially want to loop through and read in as many strings as determined by num_choices. The following code only executes the else condition.
Scanner s2 = new Scanner(System.in);
for(int i=0; i < this.num_choices; i++)
{
if(s2.hasNext())
{
System.out.println("Enter choice " + (i+1) +":");
String ch = s2.next();
//this.choices.addElement(ch);
}
else
{
System.out.println("Lets end this");
}
}
`
I am getting this: Exception in thread "main" java.util.NoSuchElementException. In the main class, this is where the error points to
choice2 = Integer.parseInt(read_choice2.next());
which is inside a while loop as well. Here is the code for that:
public class Main
{
public static void main(String args[]) throws IOException
{
Vector<Survey> mysurveys = new Vector<Survey>();
boolean carry_on = true;
int choice = 0;
Scanner read_choice = new Scanner(System.in);
System.out.println("Let's begin the Survey/Test application!");
while(carry_on)
{
System.out.println("What would you like to do?");
System.out.println("1. Create a new Survey");
System.out.println("2. Create a new Test");
System.out.println("3. Display a Survey");
System.out.println("4. Display a Test");
System.out.println("5. Save a Survey");
System.out.println("6. Save a Test");
System.out.println("7. Load a Survey");
System.out.println("8. Load a Test");
System.out.println("9. Quit");
System.out.println();
System.out.println("Please enter a number for the operation you want to perform: ");
choice = Integer.parseInt(read_choice.next());
/*try
{
choice = Integer.parseInt(buffer.readLine());
}
catch(InputMismatchException e)
{
System.out.println("Invalid input. Please Enter again.");
System.out.println();
//read_choice.nextInt();
}*/
switch(choice)
{
case 1:
System.out.println("Please Enter a Name for your Survey");
String in = buffer.readLine();
Survey s1 = new Survey();
s1.CreateNew(in);
mysurveys.add(s1);
////
add_question(s1.type);
break;
case 2:
System.out.println("Please Enter a Name for your Test");
//String in = buffer.readLine();
Test t1 = new Test();
//t1.CreateNew(in);
mysurveys.add(t1);
break;
////
//add_question(t1.type);
case 3:
break;
// call Survey.display()
case 4:
break;
case 5:
Survey s = new Survey();
ReadWriteFiles x = new ReadWriteFiles();
x.SaveSurvey(s);
break;
case 6:
Test t = new Test();
//ReadWriteFiles x = new ReadWriteFiles();
//x.SaveSurvey(t);
break;
case 7:
carry_on = false;
break;
default:
System.out.println("Incorrect Input. Try Again");
System.out.println();
break;
}
}
read_choice.close();
}
public static void add_question(String type) throws IOException, NullPointerException
{
Questions q = null;
boolean carry_on2 = true;
int choice2 = 0;
Scanner read_choice2 = new Scanner(System.in);
//BufferedReader buffer2=new BufferedReader(new InputStreamReader(System.in));
while (carry_on2)
{
//
System.out.println("1. Add a new T/F Question");
System.out.println("2. Add a new Multiple Choice Question");
System.out.println("3. Add a new Short Answer Question");
System.out.println("4. Add a new Essay Question");
System.out.println("5. Add a new Ranking Question");
System.out.println("6. Add a new Matching Question");
System.out.println("7. If you want to stop adding more questions, and go back to the main menu.");
System.out.println("Please enter a number for the operation you want to perform: ");
choice2 = Integer.parseInt(read_choice2.next());
/*try
{
choice2 = Integer.parseInt(buffer2.readLine());
}
catch(InputMismatchException e)
{
System.out.println("Invalid input. Please Enter again.");
System.out.println();
//read_choice2.nextInt();
}*/
switch(choice2)
{
case 1:
q = new TrueFalse();
break;
case 2:
q = new MultipleChoice();
break;
case 3:
q = new ShortAnswer();
break;
case 4:
q = new Essay();
break;
case 5:
q = new Ranking();
break;
case 6:
q = new Matching();
break;
case 7:
carry_on2 = false;
break;
default:
System.out.println("Incorrect Input.");
break;
}
q.createQuestion(type);
}
}
}
I realize there is a lot of messy code, and I apologize for that. I just wanted to show the entire thing, so it's easier to spot the problem. Help would be appreciated.
In general way, you should add if(read_choice.hasNext()) before invoking read_choice.next(); You have the exception java.util.NoSuchElementException because no elements found to be read. this is a good habit.
About your problem, you are getting error because you has closed scanner before finish reading. Put read_choice.close() outside of loop.
Moreover, for simplify, if you want to read integer, just simple : scanner.nextInt().
read_choice.close();
Don't close the scanner as long as you are not done reading all the inputs. Doing also closes the underlying input stream (System.in), check the documention;
You don't need to initialize the Scanner multiple times. Just create one instance and pass it around (keep using it).
Also,
for(int i=0; i < this.num_choices; i++)
{
//if(s2.hasNext())
//{
System.out.println("Enter choice " + (i+1) +":");
String ch = s2.next();
//this.choices.addElement(ch);
you don't need that condition check. The next() will block until the input is entered.

Categories

Resources