Error handling with strings java - java

I'm trying to add error handling to my java program if anything but the options and String/char are entered. I mainly need it for if a String is entered. I've tried to do the while(true) but I don't really understand that. I also added !(kb.hasNextInt()) to my line while (choice < 1 && choice > 4 ) but that didn't work either. So I just need help adding error handling to my program. Thanks!
here's my code
import java.util.*;
public class HeroesVersusMonsters
{
private static Hero hero;
private static Monster monster;
private static Random rand = new Random();
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
do
{
System.out.println("---------------------------------------");
System.out.println("\tChoose your type of hero");
System.out.println("---------------------------------------");
System.out.println("\t1. Warrior");
System.out.println("\t2. Sorceress");
System.out.println("\t3. Thief");
System.out.println("\t4. Snake");
System.out.println();
System.out.print("Choice --> ");
int choice = kb.nextInt();
kb.nextLine();
while (choice < 1 && choice > 4 )
{
System.out.println("\n" + choice + " is not an option. Please try again.");
System.out.print("Choice --> ");
choice = kb.nextInt();
kb.nextLine();
System.out.println();
}
switch (choice)
{
case 1:
hero = new Warrior();
break;
case 2:
hero = new Sorceress();
break;
case 3:
hero = new Thief();
break;
case 4:
hero = new Snake();
break;
}
switch (rand.nextInt(3))
{
case 0:
monster = new Ogre("Shrek the Ogre");
break;
case 1:
monster = new Skeleton("Bones the Skeleton");
break;
case 2:
monster = new Gremlin("Dobby the Gremlin");
break;
}
System.out.println();
System.out.println(hero.name + ", you will be fighting against " + monster.getName() + "!!!");
System.out.println();
while (hero.getHits() > 0 && monster.getHits() > 0)
{
hero.attack(monster);
monster.attack(hero);
}
System.out.print("Would you like to play again? (yes / no) ");
String play = kb.nextLine().toLowerCase();
play = play.trim();
if (play.equals("no"))
break;
else
System.out.println();
}
while (true);
}
}

Please look closly to your condition of inner while loop.
while (choice < 1 && choice > 4 )
Means loop will work until choice<1 and choice>4 remains true.
Is it exactly what you want?
I think No because what if input is 5 it is true for >4 but false for <1 what you want is you need to loop things until user enters correct input.
Am I right?
So what you need to do is just change condition like this
while(choice<1 || choice>4)
As Jared stated.
One more thing I want to suggest you don't you think you should break; external loop while user enters wrong input.(No problem)
You can do one this also.
ArrayList<Integer> ar=new ArrayList<Integer>(4);
ar.add(1);
ar.add(2);
ar.add(3);
ar.add(4);
while(true)
{
if(ar.contains(choice))
{
//Go On
}
else
{
//Print old stuff
}
}

Here is what your main method should look like:
public static void main(String ...args){
final Scanner scanner = new Scanner(System.in);
while(true){
final Hero hero = promptHero(scanner);
final Monster monster = getRandomMonster();
fight(hero, monster);
if(!playAgain(scanner))
break;
}
}
Now write the static methods promptHero, getRandomMonster, fight, and playAgain (which should return true if you want to play again).
Here is what your promptHero method should look like (to properly handle bad input):
private static Hero promptHero(final Scanner scanner){
while(true){
System.out.println("---------------------------------------");
System.out.println("\tChoose your type of hero");
System.out.println("---------------------------------------");
System.out.println("\t1. Warrior");
System.out.println("\t2. Sorceress");
System.out.println("\t3. Thief");
System.out.println("\t4. Snake");
System.out.println();
System.out.print("Choice --> ");
try{
final int choice = scanner.nextInt();
if(choice < 1 || choice > 4)
System.out.println("\n" + choice +
" is not an option. Please try again.");
else
return getHero(choice); //return the hero
} catch(InputMismatchException ime){
final String line = scanner.nextLine();// need to advance token
System.out.println("\n" + line +
" is not an option. Please try again.");
}
}
}
private static Hero getHero(final int choice){
switch (choice){
case 1:
return new Warrior();
case 2:
return new Sorceress();
case 3:
return new Thief();
case 4:
return new Snake();
}
return null;
}

You should check out the Java regex:
if(choice.toString().matches("[0-9]+"))
{
//continue
}
else
{
//error message
}

Related

How to make a simulation of a shop queue with switch statement and scan utility?

I have to make a Java program using scan, switch and cases in which I can add one customer with a command "add" and remove one customer with a command "remove".
The default number of customers in queue is 5. If the count of customers gets larger than 8 it prints out "This queue is too big." If there is less than 1 customer it prints out "There's nobody in the queue."
I tried to do some of the code, but I have no idea what to do next.
import java.util.Scanner;
public class fronta {
public static void main(String[] args) {
System.out.println ("This queue has 5 people in it at the moment.");
Scanner scan = new Scanner(System.in);
boolean x = true;
String b = "ADD";
int a = 5;
b = scan.nextLine();
while(x){
switch (b) {
case "ADD":
System.out.println ("This queue has " + a + " people in it at the moment.");
b = scan.nextLine();
System.out.println ("This queue is too big");
break;
default:
case "EXIT":
System.out.println("End of simulation.");
x = false;
break;
}
}
}
}
I think you need somehting like below:
public static void main(String[] args) {
boolean isExitRequested = false;
int queueSize = 5;
System.out.println ("This queue has "+queueSize+" people in it at the moment.");
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()){
String input = scan.nextLine();
switch (input){
case "ADD":
System.out.println ("This queue has " + queueSize++ + " people in it at the moment.");
if (queueSize > 8) {
System.out.println("This queue is too big");
}
break;
case "REMOVE":
if (queueSize == 0){
System.out.println("There's nobody in the queue.");
} else {
queueSize--;
}
break;
case "EXIT":
isExitRequested = true;
break;
default:
System.out.println("Unknown input: "+input);
}
if(isExitRequested)
break;
}
}

Breaking out of a loop by calling a method

I’m writing a small program that is asking that user to guess a number between 1 and 100. My idea was to make several methods one with game playGame(), one that shows menu showMenu(), one for statistic. I placed the menu inside a while loop in the main method hoping that every time a game is played it will the menu and ask the user for input. Most of it works fine buy I can’t get the program flow right.
Every time I finish a game, a new game starts. I think that the problem is in the while loop inside the many method. It works fine if I change:
public static void showMenu() {
Scanner input = new Scanner(System.in);
System.out.println("1. Play a game.");
System.out.println("2 Show statistics.");
System.out.println("3. Exit.\n");
System.out.println("Make a choise: ");
int selectMenu = input.nextInt();
while (true) {
switch (selectMenu) {
case 1:
playGame();
break;
case 2:
statistics();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Enter valid number:");
}
}
} //end showMenu
To:
public static void showMenu() {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("1. Play a game.");
System.out.println("2 Show statistics.");
System.out.println("3. Exit.\n");
System.out.println("Make a choise: ");
int selectMenu = input.nextInt();
switch (selectMenu) {
case 1:
playGame();
break;
case 2:
statistics();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Enter valid number:");
}
}
} //end showMenu
But I can’t understand why. In the firs example after the game is played a new game starts without showing the menu. It jumps directly to case: 1
Thanks a lot!
You can see the program below:
public class GuessTheNumber {
private static int gameCount;
private static int guessCount;
private static int highestNumber;
private static int lowestNumber;
public static void main(String[] args) {
while (true) {
showMenu();
}
} // end main
public static void playGame() {
int secretNumber, guess;
Scanner input = new Scanner(System.in);
SecureRandom rand = new SecureRandom();
secretNumber = rand.nextInt(100) + 1; //makes random number between 1 and 100
System.out.println(secretNumber);
System.out.println("Guess the secret mumber which is between 1 and 100.");
System.out.print("Make your guess: ");
guess = input.nextInt();
guessCount++;
highestNumber = guess;
lowestNumber = guess;
while (guess != secretNumber) {
// high or low logic
if (guess > secretNumber) {
System.out.println("The number is too high.");
}
else {
if (guess < secretNumber) {
System.out.println("The number is too low.");
}
}
System.out.print("Make a new guess: ");
guess = input.nextInt();
guessCount++;
//get highest and lowest number
if (guess > highestNumber) {
highestNumber = guess;
}
if (guess < lowestNumber) {
lowestNumber = guess;
}
} //end while
System.out.printf("Very good the right number was: %d%n", guess);
} //end playGame()
public static void showMenu() {
Scanner input = new Scanner(System.in);
System.out.println("1. Play a game.");
System.out.println("2 Show statistics.");
System.out.println("3. Exit.\n");
System.out.println("Make a choise: ");
int selectMenu = input.nextInt();
while (true) {
switch (selectMenu) {
case 1:
playGame();
break;
case 2:
statistics();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Enter valid number:");
}
}
} //end showMenu
public static void statistics() {
System.out.println("Games played: " + gameCount);
System.out.println("Total number of guesses: " + guessCount);
System.out.println("The highest number: " + highestNumber);
System.out.println("The lowest number: " + lowestNumber);
}
} //end class GuessTheNumber
As this seems like a homework project, here is a quick tip for finding loop issues - simply add System.out.println("") checks inside and outside of all loop and logic conditions.
EG). System.out.println("Before While True);
System.out.println("Inside Case 1");
System.out.println("Inside Case 2");
You will save yourself a ton of time as you'll be able to tell where your logic is breaking.
As a hint, take a look at int selectMenu = input.nextInt();. I would recommend printing the value of selectMenu using my advice above. You might be surprised to see what's assigned there.

How to validate the selection menu using a loop

Can someone edit my code to make it loop the selection menu. If the choice is not one of the 5 options it will prompt the user to re-enter until it is a valid option. If possible an explanation would be helpful as well. Thanks
Here is my code.
import java.util.*;
public class ShapeLoopValidation
{
public static void main (String [] args)
{
chooseShape();
}
public static void chooseShape()
{
while (true){
Scanner sc = new Scanner(System.in);
System.out.println("Select a shape number to calculate area of that shape!");
System.out.print("Circle = 1. \nRectangle = 2. \nTriangle = 3. \nExit = 4. \nINPUT : ");
int shapeChoice = sc.nextInt();
//while (true) {
if (shapeChoice >= 1 && shapeChoice <=4)
{
if (shapeChoice == 1)
{
circle();
}
else if (shapeChoice == 2)
{
rectangle();
}
else if (shapeChoice == 3)
{
triangle();
}
else if (shapeChoice == 4)
{
return;
}
}
else
{
System.out.print("Error : Choice " + shapeChoice + "Does not exist.");
}
}
class Test {
int a, b;
Test(int a, int b) {
this.a = a;
this.b = b;
}
}
}
First: take a look at switch
Second: read a bit about do-while loops (they are usually a good fit for this kind of situations).
Now, how I would implement it (but you should really learn how to make a loop in this scenarios):
public static void chooseShape () {
boolean valid = false;
do {
Scanner sc = new Scanner(System.in);
System.out.println("Select a shape number to calculate area of that shape!");
System.out.print("Circle = 1. \nRectangle = 2. \nTriangle = 3. \nExit = 4. \nINPUT : ");
int shapeChoice = sc.nextInt();
switch (shapeChoice) {
valid = true;
case 1:
circle();
break;
case 2:
rectangle();
break;
case 3:
triangle();
break;
case 4:
return;
default:
valid = false;
System.out.println("Error : Choice " + shapeChoice + "Does not exist.");
System.out.println("Please select one that exists.")
}
} while (!valid)
}
Use do-while flow control until EXIT code entered:
int shapeChoice;
do {
System.out.println("Select a shape number to calculate area of that shape!");
System.out.print("Circle = 1. \nRectangle = 2. \nTriangle = 3. \nExit = 4. \nINPUT : ");
int shapeChoice = sc.nextInt();
// then use if-else or switch
} while (shapeChoice != 4);
OR
use break statement to loop break at your code as bellow:
else if (shapeChoice == 4)
{
break;
}

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.

How to get and set value in the switch-case statement

I want to make a simple menu with 3 choices:
'Create new employee', 'Display all employees' and 'Quit' in a Employee Manager(code below) but it was not successful(compiling error).
BlueJ editor cannot realize the object 'm', 's' and 'l' in the 'case 2' statement. Is there anyway to get the value of the object in the 'case 1' and use them in the 'case 2'? Thanks a lot!
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int ch;
do{
System.out.println("EMPLOYEE MANAGER\n");
System.out.println("1. Create new employees\n");
System.out.println("2. Display all employees\n");
System.out.println("3. Quit\n");
System.out.print("Your choice: ");
Scanner input = new Scanner(System.in);
ch = input.nextInt();
switch(ch){
case 1: System.out.println("== CREATE NEW EMPLOYEES ==");
System.out.println();
Manager m = new Manager();
Scientist s = new Scientist();
Labourer l = new Labourer();
m.newManager();
s.newScientist();
l.newLabourer();
System.out.println();
break;
case 2: System.out.println("== PREVIEW EMPLOYEES ==");
System.out.println();
m.display();
s.display();
l.display();
System.out.println();
System.out.println();
break;
case 3: System.exit(0);
default: System.out.println("Invalid choice!");
}
} while(ch >= 1 && ch <=4);
}
}
They are local to block, declare them out of switch block
Manager m = new Manager();
Scientist s = new Scientist();
Labourer l = new Labourer();
switch(){...}
This answers your question well, but I would like to add few more details
if you don't put brackets with case block like
switch(i){
case 1:
String str="abc";
System.out.println(str);
case 2:
// it will give you compile time error
//duplcate local variable str
String str="abc";
}
then this str instance is visible in other case blocks as well
Q: Is there anyway to get the value of the object in the 'case 1' and use them in the 'case 2'?
A: No. The whole point of a case block is "either-or".
If you want to do "something" based on "something else", then you'll need two separate control structures.
EXAMPLE:
import java.util.Scanner;
public class Test
{
Manager m = null;
Scientist s = null;
Labourer l = null;
public static void main(String[] args) {
Test test = new Test().doIt ();
}
private void doIt () {
int ch;
do{
System.out.println("EMPLOYEE MANAGER\n");
System.out.println("1. Create new employees\n");
System.out.println("2. Display all employees\n");
System.out.println("3. Quit\n");
System.out.print("Your choice: ");
Scanner input = new Scanner(System.in);
ch = input.nextInt();
switch(ch) {
case 1:
System.out.println("== CREATE NEW EMPLOYEES ==");
getEmployees ();
break;
case 2:
System.out.println("== PREVIEW EMPLOYEES ==");
previewEmployees ();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
} while(ch >= 1 && ch <=4);
}
private void getEmployees () {
System.out.println();
m = new Manager();
s = new Scientist();
labourer l = new Labourer();
m.newManager();
s.newScientist();
l.newLabourer();
System.out.println();
}
private void previewEmployees () {
...
}
Define your objects m, s and l in a broader scope outside the switch. Also, initialize your objects with null value and validate them before using.
Manager m = null;
Scientist s = null;
Labourer l = null;
do{
//your code here....
switch(ch) {
case 1:
m = new Manager();
//the rest of your code here...
break;
case 2:
if (m != null) {
m.display(); //and so on
}
}
}

Categories

Resources