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

}

Edited 4 Years Ago by 3nrichedd

Can you post the output from the progam that shows what is does?

Your recursive call can be a problem. What will be the values of the variable that is returned by the method when the recursive call returns? Add a println in the catch block to print out its value if you can not see what is happening. Think about what the method returns and what your code does with it.

Try to guess my two-digit number, and I'll
tell you how many digits from your guess
appear in my number. Enter -1 to give up.

18
Your guess? 19
Incorrect (hint: 1 digits match)
18
Your guess? 81
Incorrect (hint: 2 digits match)
18
Your guess? 28
Incorrect (hint: 0 digits match)
18
Your guess?

it works for the tens place, and for both digits in opposite places, but when i try 28 when 18 is the random number, it dosnt show it as a match, yet it was working before I added the try catch.
Im gonna keep working on it but thanks for any help!

i tried adding the print statements but they appear to be correct both tempInput and strInput

Where did you add the printlns? You think you are having a problem with the try/catch block, did you add one in the catch block?

yes i added a print statement both in the catch and the try braces, then displayed the values of tempInput and strInput, they both appeared correct.

Another approach is to validate the input string before performing the conversion using Java regular expression. I like that second approach because it is more elegant and it will keep your code clean.

public boolean isInteger( String input )   
{   
   try  
   {   
      Integer.parseInt( input );   
      return true;   
   }   
          catch( Exception )   
   {   
      return false;   
   }   
}  

Edited 4 Years Ago by poojavb

I debugged ur code....when the time u r entering a wrong value and u get an exception....
the second time if u enter some other value even if there is one match the input value is equal to 0 so it says no matches found....

Edited 4 Years Ago by poojavb

I tried the below way....check if it works for u...or u can make some changes

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.println("In getInput");
        System.out.print("Your guess? ");
        String strInput;
        int tempInput = 0;
        strInput = sc.next();
        Pattern p = Pattern.compile( "([0-9][0-9])" );
        Matcher m = p.matcher(strInput);
        if  (m.matches() )
            tempInput = Integer.parseInt(strInput);

        else
        {
            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;
        int flag=0;
        System.out.println("Input: "+input);
        if(input == 0){
            //returns nothing, so there's no incorrect message
            System.out.println("Error understanding number. Please enter again");
            return;
        }
        if(input == -1){
            //returns nothing, so there's no incorrect message
            return;
        }
        if(input%10 == rndNum%10 || input%10 == rndNum/10){
        //  System.out.println("if");
            matches++;
            System.out.println("1st : "+matches);
            flag=1;
        }
        else
        {
            flag=0;
        }
        if(input/10 == rndNum%10 || input/10 == rndNum/10){
            System.out.println("else if");
            matches++;
        //  System.out.println("2nd : "+matches);   
            flag=1;
        }
        else
        {
            flag=0;
        }
System.out.println("Macthes: "+matches);
        if(flag==0){
        //  System.out.println("else");
            matches = 0;
            System.out.println("Incorrect (hint: "+matches+" digits match)");
        }
        else if(flag==1)
        {
            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");
    }

}

Error part

return tempInput;

This code returns 0 after u make a mistake like entering string or 3 digits...

Edited 4 Years Ago by poojavb

This article has been dead for over six months. Start a new discussion instead.