I am trying to make battleship. Here i want to get input from the console with a Scanner to determain a place to shoot.
while (x) {
while (counter) {
userInput = input.nextLine();
if (userInput.equals("cheat")) {
cheat = true;
}
uppercased = userInput.toUpperCase();
char[] c = uppercased.toCharArray();
if (uppercased.length() < 2) {
System.out.println("Error, invalid input9, try again");
break;
} else if (uppercased.equals("")) {
System.out.println("Error, invalid input1, try again");
break;
} else if (uppercased.length() > 2) {
System.out.println("Error, invalid input2, try again");
break;
} else if (c[0] < 65 || c[0] > 74) {
System.out.println("Error, invalid input3, try again");
break;
} else if (c[1] < 48 || c[1] > 57) {
System.out.println("Error, invalid input4, try again");
break;
} else {
code = uppercased;
target = map.get(uppercased);
targetPosition = target.getSymbol();
if (targetPosition != 46) {
System.out.println("You have shot here, try again");
break;
}
}
counter = false;
}
x = false;
code = uppercased;
}
the problem is, when I give a wrong input, it doesn't ask about new input, but it will execute the rest of my program.
It is supposed to filter input so it is like this:
"A1"
than it is used to determain a place to shoot.
How can I filter the wrong input and get new input?
using break, will move you outside of current loop.
using continue, will move you to the beginning of current loop.
So in your case continue would be better, because it will move you to part of code where you are reading input in inner loop.
Extracting main body from this loop (like below) would also be easy to test:)
Update:
while (x) {
while (!runCheck()) { Your main method is extracted from loop, making it more readable
}
x = false;
code = uppercased;
}
boolean runCheck(){
userInput = input.nextLine();
if ("cheat".equals(userInput)) { // prefer to compare String agains a constant in this way, it's immune to NPE, when by any chance userInput would be a null
cheat = true;
}
uppercased = userInput.toUpperCase();
char[] c = uppercased.toCharArray();
if (uppercased.length() < 2) {
System.out.println("Error, invalid input9, try again");
return true;
} else if ("".equals(uppercased)) {
...
} else {
code = uppercased;
if(!map.conainsKey(uppercased)){ // target can be null, or not existing in map
return true;
}
target = map.get(uppercased);
targetPosition = target.getSymbol();
if (targetPosition != 46) {
System.out.println("You have shot here, try again");
return true;
}
}
return false;
}
Related
Coding a simple HiLo card game where the user is given a card value from a deck of cards and then inputs 'higher', 'lower' or 'equal' trying to guess the balue of the next card.
Just really can't get my head around user input validation with iteration ie. not moving on until a string with the required parameters has been entered.
My code so far:
import java.util.Scanner;
import java.util.Random;
public class HiLoGame {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Random randomCard = new Random();
int numberOfSuccesses = 0;
boolean finished = false;
int card = (randomCard.nextInt(13) + 2);
while (finished != true) {
int nextCard = (randomCard.nextInt(13) + 2);
String pictureCard = "";
if (((numberOfSuccesses < 0) ? nextCard : card) == 11) {
pictureCard = "Jack";
} else if (((numberOfSuccesses < 0) ? nextCard : card) == 12) {
pictureCard = "Queen";
} else if (((numberOfSuccesses < 0) ? nextCard : card) == 13) {
pictureCard = "King";
} else if (((numberOfSuccesses < 0) ? nextCard : card) == 14) {
pictureCard = "Ace";
}
System.out.println("The card is a " + ((card > 10) ? pictureCard : card));
if (numberOfSuccesses == 4) {
System.out.println("Congratulations. You got them all correct");
finished = true;
break;
}
while (!reader.nextLine().toLowerCase().equals("higher")
|| !reader.nextLine().toLowerCase().equals("lower")
|| !reader.nextLine().toLowerCase().equals("equal")) {
System.out.println("Try again!");
reader.next();
}
String userGuess = reader.nextLine().toLowerCase();
//TODO validate input
if (userGuess.equals("higher")) {
if (nextCard > card) {
numberOfSuccesses++;
} else {
finished = true;
break;
}
} else if (userGuess.equals("lower")) {
if (nextCard < card) {
numberOfSuccesses++;
} else {
finished = true;
break;
}
} else if (userGuess.equals("equal")) {
if (nextCard == card) {
numberOfSuccesses++;
} else {
finished = true;
break;
}
}
System.out.println(numberOfSuccesses);
card = nextCard;
}
if (numberOfSuccesses < 4) {
System.out.println("Sorry, incorrect!");
}
}
}
and the relevant code extract:
while (!reader.nextLine().toLowerCase().equals("higher")
|| !reader.nextLine().toLowerCase().equals("lower")
|| !reader.nextLine().toLowerCase().equals("equal")) {
System.out.println("Try again!");
reader.next();
}
It kinda just gets stuck at the above part giving "Try again" over and. I've completed programs having to use .hasNextInt() but I'm struggling with this string validation.
Thanks for any and all help/comments!
You are calling reader.nextLine() up to 3 times and so you are comparing 3 different strings.
If I enter "xxx" your code says "xxx != higher so read another line" - it never compares "xxx" to "lower" or "equal".
Also pay attention to && vs ||.
Solution is to read one line into a variable and use that variable for each condition. I'm not going to write it out as this is clearly homework or a self learning exercise, so best for you to do it yourself.
I think your condition logic needs to change. You are checking if input not equal to "higher" or not equal to "lower" or not equal to "equal" so it will always be false overall even if you enter expected value - if you enter "higher" it's not equal to lower. You need to change ors to ands.
How do I convert my do-while loop to a while loop?
int numAttempts = 0;
do
{
System.out.println("Do you want to convert to Peso or Yen?");
pesoOrYen = readKeyboard.nextLine();
if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen"))
{
notPesoOrYen = false;
}
else if (numAttempts < 2)
{
System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type. Try again:");
notPesoOrYen = true;
}
numAttempts++;
} while(notPesoOrYen==true && numAttempts < 3);
I tried to do while(notPesoOrYen==true && numAttempts < 3) then the statement but it did not work.
MY FULL CODE
package currencyconverter;
import java.util.Scanner;
import java.text.NumberFormat;
public class CurrencyConverter
{
public static void main(String[] args)
{
Scanner readKeyboard = new Scanner(System.in);
double doubleUsersCaptial;
boolean notPesoOrYen=true;
String pesoOrYen;
double usersConvertedCapital;
boolean userInputToRunProgramAgain=true;
final double US_DOLLAR_TO_PESO = 13.14;
final double US_DOLLAR_TO_YEN = 106.02;
do
{
System.out.println ("How much money in US dollars do you have?");
String usersCaptial = readKeyboard.nextLine();
doubleUsersCaptial = Double.parseDouble(usersCaptial);
int numAttempts = 0;
do
{
System.out.println ("Do you want to convert to Peso or Yen?");
pesoOrYen = readKeyboard.nextLine();
if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen"))
{
notPesoOrYen = false;
}
else if (numAttempts < 2)
{
System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type. Try again:");
notPesoOrYen = true;
}
numAttempts++;
}while(notPesoOrYen==true && numAttempts < 3);
if(numAttempts==3)
{
System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type.");
System.out.println("You entered the wrong currency type too many times\nGood Bye");
System.exit(0);
}
if (pesoOrYen.equalsIgnoreCase("Peso"))
{
usersConvertedCapital = doubleUsersCaptial*US_DOLLAR_TO_PESO;
}
else
{
usersConvertedCapital = doubleUsersCaptial*US_DOLLAR_TO_YEN;
}
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String formatUsersCaptial = formatter.format(doubleUsersCaptial);
String formatUsersConvertedCapital = formatter.format(usersConvertedCapital);
System.out.println(formatUsersCaptial+"US Dollars = "
+formatUsersConvertedCapital+" "+pesoOrYen);
System.out.println("Would you like to run the Program Again?(enter 'yes' or 'no')");
String runProgramAgain = readKeyboard.nextLine();
if (runProgramAgain.equalsIgnoreCase("yes"))
{
userInputToRunProgramAgain = true;
}
else if (runProgramAgain.equalsIgnoreCase("no"))
{
System.out.println("Goood Bye");
System.exit(0);
}
else
{
System.out.println ("You entered something other than 'yes' or 'no'\n"
+"Good Bye");
System.exit(0);
}
}while (userInputToRunProgramAgain==true);
}
}
while and do... while are almost the same, do... while simply performs an iteration before evaluating for the first time the exit condition, whereas while evaluates it even for the first iteration (so eventually the body of a while loop can never be reacher whereas a do... while body will always be executed at least once).
Your code snippet is not complete but I guess you didn't initialized notPesoOrYen to true before the loop and that's why it is not working. Finally, don't write while(notPesoOrYen==true && numAttempts < 3) but while(notPesoOrYen && numAttempts < 3), the == true comparison is unnecessary.
Initialise your boolean variable outside while loop:
int numAttempts = 0;
boolean notPesoOrYen=true;
while (notPesoOrYen && numAttempts < 3) {
System.out.println("Do you want to convert to Peso or Yen?");
pesoOrYen = readKeyboard.nextLine();
if (pesoOrYen.equalsIgnoreCase("Peso") || pesoOrYen.equalsIgnoreCase("Yen")) {
notPesoOrYen = false;
} else if (numAttempts < 2) {
System.out.println("Sorry, but '" + pesoOrYen + "' is not a valid currency type. Try again:");
notPesoOrYen = true;
}
++numAttempts;
};
I am not asking anyone to do my work I just need a little help solving this mismatch. This is my program:
import java.util.InputMismatchException;
import java.util.Scanner;
class FibonacciNumbers {
FibonacciNumbers() //default constructor
{
}
Scanner in = new Scanner(System.in);
public int fOf(int n)
{
if (n == 0) //the base case
{
return 0;
}
else if (n==1)
{
return 1;
}
else
{
return fOf(n-1)+fOf(n-2);
}
}
public static void main(String[] args)
{
FibonacciNumbers fNumbers = new FibonacciNumbers(); //creates new object
Scanner in = new Scanner(System.in);
String userInput;
int n = 0;
boolean IsRepeat = true ;
boolean isQuit;
boolean checkException = false;
isQuit = false;
while (!isQuit)
{
try {
{
System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");
n = in.nextInt();
System.out.print("The Fibanocci number for "+n+" is: ");
n = fNumbers.fOf(n);
System.out.println(n);
System.out.print("Do you want to run again? Press 'N' for No or anything else to continue: ");
userInput = in.next();
if(userInput.equalsIgnoreCase("N") )
{
isQuit = true;
System.out.println("Good-bye!");
}
else
{
IsRepeat = true;
}
}
}
catch(InputMismatchException ex) {
userInput = in.nextLine();
if ((userInput.charAt(0) == 'q') || (userInput.charAt(0) == 'Q') )
{
isQuit = true;
System.out.println("Good-bye!");
}
else {
checkException = true;
IsRepeat = true;
System.out.println("Invalid entry, Try again!");
}
}
catch (ArrayIndexOutOfBoundsException a)
{
n = in.nextInt();
if (n<0 || n>46)
{
System.out.println("Invalid entry! Please enter an integer that is greater than 0 but less than 46 :");
checkException = false;//sets boolean value to false, continues the loop
}
else
{
IsRepeat = true;
}
}
}
}
}
I did everything I got everything to work but at this part it is not going as I want it to run:
catch (ArrayIndexOutOfBoundsException a)
{
n = in.nextInt();
if (n<0 || n>46)
{
System.out.println("Invalid entry! Please enter an integer that is greater than 0 but less than 46 :");
checkException = false;//sets boolean value to false, continues the loop
}
else
{
IsRepeat = true;
}
}
When I run it if the user inputs higher than 46 or lower than 0 then ask them for a different input but it is just doing the math. It wont do as i wrote the program.
It throws a "java.lang.StackOverflowError" instead of an "ArrayIndexOutOfBoundsException".
The better way would be to catch an invalid input at
System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");
n = in.nextInt();
you could set the "n = in.nextInt();" into a do - while- loop,
like:
do {
ask for number
} while (check if number is correct);
I have a method that should scan for one of 3 letters in caps or lowercase and return the lower case version of the letter. If an improper letter is entered the user is warned and reasked for a letter. I have two issues, 1: as soon as the method is run I get the outputted line with the error message telling the user invalid entry without waiting for an entry! (so the second the method is run I see High, low or sevens (H/L/S):Invalid entry. Please try again using H/L/S! before entering anything then the method is recalled again and all works fine form there except for my next issue) 2: the entry that is gotten from the scanner never passes any of my if statements even though it should.
my code:
private static char getHighLow(Scanner inScanner) {
System.out.print("High, low or sevens (H/L/S):");
String entered = inScanner.nextLine();
System.out.print(entered);
if(entered.equals("H") || entered.equals("h")){
return 'h';
}
else if (entered.equals("L") || entered.equals("l")){
return 'l';
}
else if(entered.equals("S") || entered.equals("s")){
return 's';
}
char result = 0;
while(result != 'l' || result != 'h' || result != 's'){
System.out.println("Invalid entry. Please try again using H/L/S!");
result=getHighLow(inScanner);
}
return result;
}
Instead of using while(), you can use 'else' like this-
private static char getHighLow(Scanner inScanner) {
System.out.print("High, low or sevens (H/L/S):");
String entered = inScanner.nextLine();
System.out.print(entered);
if(entered.equals("H") || entered.equals("h")){
return 'h';
}
else if (entered.equals("L") || entered.equals("l")){
return 'l';
}
else if(entered.equals("S") || entered.equals("s")){
return 's';
}
else {
System.out.println("Invalid entry. Please try again using H/L/S!");
return getHighLow(inScanner);
}
}
You can simply use equalsIgnoreCase and trim the entered string. And add a while loop util your condition is satisfied.
Scanner scanner = new Scanner(System.in);
boolean loop = true;
String choice = null;
while (loop) {
System.out.print("High, low or sevens (H/L/S):");
choice = scanner.nextLine();
if ("H".equalsIgnoreCase(choice.trim())
|| "L".equalsIgnoreCase(choice.trim())
|| "S".equalsIgnoreCase(choice.trim())) {
System.out.println("Correct Choice");
loop = false;
}
else
{
System.out.println("Wrong Choice");
}
}
System.out.print(choice);
char result;
while(true){
System.out.print("High, low or sevens (H/L/S):");
String entered = inScanner.nextLine();
System.out.print(entered);
if(entered.equals("H") || entered.equals("h")){
result = 'h';break;
}
else if (entered.equals("L") || entered.equals("l")){
result = 'l';break;
}
else if(entered.equals("S") || entered.equals("s")){
result = 's';break;
}else{
System.out.println("Invalid entry. Please try again using H/L/S!");
}
}
Hey you are not breaking out of the while loop at all. Did you see that ?
This is what you want. Here is the program to iterate over characters in a String. And convert them in lower case letter if they are H,L OR S.
package testproj;
import java.util.Scanner;
public class TestProj {
public static void main(String[] args) {
Scanner scanner = new Scanner("HLs");
String result = getHighLow(scanner);
System.out.println("Result :"+result);
}
private static String getHighLow(Scanner inScanner) {
System.out.println("High, low or sevens (H/L/S):");
String entered;
String result = "";
boolean isCharFound = false;
String temp = "";
while (inScanner.hasNext()) {
temp = inScanner.next();
System.out.println(temp);
for (int index = 0; index < temp.length(); index++) {
entered =new Character(temp.charAt(index)).toString() ;
if (entered.equals("H") || entered.equals("h")) {
result = result + 'h';
isCharFound = true;
} else if (entered.equals("L") || entered.equals("l")) {
result = result + 'l';
isCharFound = true;
} else if (entered.equals("S") || entered.equals("s")) {
result = result + 's';
isCharFound = true;
}
if (!isCharFound) {
System.out.println("Invalid entry. Please try again using H/L/S!");
}
isCharFound = false;
}
}
return result;
}
}
*EDIT: Okay after fixing the try catch error I get a problem in the catch {.. when it prints.
*, Basically when I say I want to play again it continues the game as it should but it also prints the first catch and then asks for an input at line 23.
if (decision.equalsIgnoreCase("yes"))
{
ai = (int)(Math.random()*101);
System.out.println("From 0 to 100, what number do you think I have generated?");
tryCatch = true;
loop = true;
rtrn = true;
while (tryCatch == true)
{
while (loop == true)
{
try
{
guess = Integer.parseInt(iConsole.nextLine());
if (guess >= 0)
{
loop = false;
}
}
catch (NumberFormatException e)
{
System.out.println("Invalid input. Please try again.");
}
catch (InputMismatchException e)
{
System.out.println("Invalid input. Please try again!");
}
}
Hi this is my first post so if I get the code formatting on the forum wrong I'll edit it.
Right now I'm coding a game in java eclipse where the cpu generates a number and the user has to guess it. I am using the scanner class for most of this. What I am having trouble doing is creating a try catch to check the user input if it is a valid Integer.
What ends up happening is that the code block below it doesn't recognize the already-initialized variable.
package ics3U;
import java.util.*;
import java.io.*;
public class highLow
{
static public void main (String args[]) throws IOException
{
String name;
String decision;
String decision2;
int ai;
int guess;
int counter = 1;
boolean fullGame = true;
boolean tryCatch = true;
boolean rtrn = true;
Scanner iConsole = new Scanner(System.in);
System.out.println("Hello! Welcome to HiLo!");
System.out.println("What is your full name?");
name = iConsole.nextLine();
System.out.println("Hello " + name + "! Would you like to play?");
decision = iConsole.nextLine();
while (fullGame == true)
{
if (decision.equalsIgnoreCase("yes"))
{
ai = (int)(Math.random()*101);
System.out.println("From 0 to 100, what number do you think I have generated?");
tryCatch = true;
rtrn = true;
while (tryCatch == true)
{
try
{
guess = Integer.parseInt(iConsole.nextLine());
}
catch (Exception e)
{
System.out.println("Invalid input. Please try again.");
}
while (guess != ai)
{
if (guess < ai)
{
System.out.println("Too low!");
guess = iConsole.nextInt();
}
else if (guess > ai)
{
System.out.println("Too high!");
guess = iConsole.nextInt();
}
counter = counter + 1;
}
System.out.println("Correct! You guessed it after " + counter + " tries!");
counter = ((counter - counter)+1);
System.out.println("Would you like to play again?");
while (rtrn == true)
{
decision2 = iConsole.next(); //finally..
if (decision2.equalsIgnoreCase("yes"))
{
fullGame = true;
tryCatch = false;
rtrn = false;
break; //do-while may be needed, have to bypass catch, 'break' works after restating value of tryCatch & rtrn
}
else if (decision2.equalsIgnoreCase("no"))
{
System.out.println("Goodbye.");
fullGame = false;
tryCatch = false;
rtrn = false;
iConsole.close();
}
else
{
System.out.println("Sorry?");
}
}
/*catch (Exception e)
{
System.out.println("Invalid input. Please try again.");
}
catch (NumberFormatException e)
{
System.out.println("Invalid input. Please try again.");
}
//More specific Exceptions, turn this on later
catch (InputMismatchException e)
{
System.out.println("Invalid input. Please try again!");
}*/
}
}
else if (decision.equalsIgnoreCase("no"))
{
System.out.println("Goodbye.");
fullGame = false;
tryCatch = false;
rtrn = false;
iConsole.close();
}
else
{
System.out.println("Sorry?");
decision = iConsole.nextLine();
}
}
}
}
Add a continue statement in your catch block. That way, if the user enters something that's not an integer and parsing fails, it will immediately try again rather than trying to run the rest of the loop.
try
{
guess = Integer.parseInt(iConsole.nextLine());
}
catch (Exception e)
{
System.out.println("Invalid input. Please try again.");
continue; // jump to beginning of loop
}
Try moving all your code after the catch block (in the loop) inside the try block after this line
guess = Integer.parseInt(iConsole.nextLine());
As you currently have it, anytime there is an exception in the parseInt, it will still try to process the unassigned guess instead of restarting the loop.
Since the statements are in a try block there's a chance that they will fail, and your program has a chance of trying to use a non-initialized variable. The solution is to initialize the variables to a default value that makes sense, i.e.,
int guess = -1; // some default value
You should also wrap the while loop around the try/catch block. Don't let the program progress until inputted data is valid.
boolean validGuess = false;
while (!validGuess) {
// prompt user for input here
try {
guess = Integer.parseInt(iConsole.nextLine());
if (/* .... test if guess is valid int */ ) {
validGuess = true;
}
} catch (NumberFormatException e) {
// notify user of bad input, that he should try again
}
}
You could even encapsulate all of this into its own method if you need to do similar things throughout the program.