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;
}
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: