Bart g
Bart g

Reputation: 585

return true if there is no letters

This method is one of several. I was wondering if I am correct in the line here that says "if(dig1.contains() && dig2.contains() && res.contains())". in the regex 1. I need to make sure that there is no letters in the string 2. I need to see if the sum of string1 and string2 add up to the third string. else return false. Thank you all for your help. This is so far what I have.

/**
  Returns true if the puzzle is solved.
  @return true if the puzzle has no letters and the 
  first two numbers add up to the third
*/
public boolean isSolved()
{
   String dig1=""+add1;
   String dig2=""+add2;
   String res=""+result;
   //String a1=""+dig1;
   if(dig1.contains("[^A-Z]") && dig2.contains("[^A-Z]") && res.contains("[^A-Z]")){
      int i=Integer.parseInt(dig1);
      int j=Integer.parseInt(dig2);
      int k=Integer.parseInt(res);

        if(i+j==k)
            return true;
        else
            return false;
     }
   else
       return false;

}

Upvotes: 1

Views: 234

Answers (5)

mindandmedia
mindandmedia

Reputation: 6825

No, that's a complicated way to convert a string to an integer. (if thats what you are asking)

public boolean isSolved()
{
   String dig1=""+add1;
   String dig2=""+add2;
   String res=""+result;
   try{
      int i=Integer.parseInt(dig1);
      int j=Integer.parseInt(dig2);
      int k=Integer.parseInt(res);

     if((i+j)==k) return true;
     return false;
   } catch {
     return false;
   }
}

So, basically, you could just wrap this in a try/catch-block, then you don't have to use the regular-expressions to check the strings beforehand.

I added my addtl comment here: if you are going to use this all over the place, refactor it into a method:

private boolean IsOnlyNumbers( String input ){ 
  try{ 
    Integer.parseInt(input); 
    return true; 
  } catch { 
    return false;
  }
}

or, since you need the number, you could do this:

private int GetNumber( String input ){ 
  try{ 
    return Integer.parseInt(input); 
  } catch { 
    return -1;
  }
}

and then check:

var dig1 = GetNumber(add1);
var dig2 = GetNumber(add2);
var res = GetNumber(add2);
if( dig1 > 0 && dig2 > 0 && dig3 > 0 ) return dig1+dig2 == res;

Upvotes: 2

Boris Strandjev
Boris Strandjev

Reputation: 46943

I would suggest you to use another regex. IT will check if your strings contain only digits:

if (dig1.matches("^[0-9]*$")
    && dig2.matches("^[0-9]*$")
    && res.matches("^[0-9]*$"))

With your current code you will get exceptions in many cases, e.g. when there are small-cased letters.

Upvotes: 4

user219882
user219882

Reputation: 15844

I would recommend this solution

public boolean isSolved() {
    try {
        int dig1 = Integer.parseInt(add1);
        int dig2 = Integer.parseInt(add2);
        int res = Integer.parseInt(result);

        return dig1 + dig2 == res;
    } catch (NumberFormatException ex) {
        return false;
    }
}

In case all strings are numbers, the conversion is correct and than you can compare the numbers. In case on of the contains letters or other bad characters, an exception is thrown and false is returned.

Upvotes: 2

Till Helge
Till Helge

Reputation: 9311

Why would you use regular expressions for that? Integer.parseInt() will throw an exception if conversion is not possible. Just catch the exception and then you know that the strings contained something else than digits. Something along the line of this:

public boolean isSolved() {
    try {
        int i = Integer.parseInt(dig1);
        int j = Integer.parseInt(dig2);
        int k = Integer.parseInt(res);
        return (i+j) == k;
    }
    catch (NumberFormatException e) {
        return false;
    }
 }

Upvotes: 4

Jon
Jon

Reputation: 3194

You could have a try-catch statement:

try {
    int i=Integer.parseInt(dig1);
    int j=Integer.parseInt(dig2);
    int k=Integer.parseInt(res);

    if(i+j==k)
        return true;
    else
        return false;
    }
} catch (NumberFormatException numForEx) {
    return false;
} 

Upvotes: 2

Related Questions