hello, I am lik 98% done with this assignment where the user enters a number, and then it will output if you guessed the right number, or if you have any matching digits, so for example, the if the random number is 24 and the user inputs 20 it should say, incorrect (1match) etc.. but for some reason after I added this try catch to handle non numerical inputs, it will not evaluate the ones place digits correctly, so now if the random number is 54 if 24 is entered it dosnt show the match..
here is my code-- any advice would be appreciated..
import java.util.*;
public class NumberGuess{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
int rndNum = rnd.nextInt(100),
input = 0,
tries = 0;
guessIntro();
do{
System.out.println(rndNum);
input = getInput(sc);
tries++;
checkForWin(input,rndNum,tries);
checkForMatches(input,rndNum,tries);
}while(checkForExit(input,rndNum));
}
public static int getInput(Scanner sc){
System.out.print("Your guess? ");
String strInput = sc.next();
int tempInput = 0;
try{
tempInput = Integer.parseInt(strInput);
}catch(NumberFormatException nFE){
System.out.println("\""+strInput+"\" is not an integer;"+
" try again.");
getInput(sc);
}
return tempInput;
}
public static void checkForMatches(int input, int rndNum, int tries){
int matches = 0;
if(input == -1){
//returns nothing, so there's no incorrect message
return;
}
if(input%10 == rndNum%10 || input%10 == rndNum/10){
matches++;
}
if(input/10 == rndNum%10 || input/10 == rndNum/10){
matches++;
}
else{
matches = 0;
}
System.out.println("Incorrect (hint: "+matches+" digits match)");
}
public static boolean checkForExit(int input, int rndNum){
if(input==-1){
System.out.println("My secret number was "+rndNum);
return false;
}
else{
return true;
}
}
public static void checkForWin(int input, int rndNum, int tries){
if(input==rndNum){
System.out.println("You got it right in "+tries+" tries");
System.exit(0);
}
}
public static void guessIntro(){
System.out.println("Try to guess my two-digit number,"+
" and I'll\ntell you how many digits"+
" from your guess\nappear in my"+
" number. Enter -1 to give up.\n");
}
}