Exception in thread "main" StackoverFlow error - java

I am writing a program that verifies if a password meets the appropriate requirements. I have all of my code written, and I feel it should work, but I get the following error:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.String.length(String.java:623)
at PasswordVerifier.isValid(PasswordVerifier.java:5)
at PasswordVerifier.isValid(PasswordVerifier.java:6)
and then it repeats the last line of the error for quite some time. I've been looking around and cannot seem to figure out my issue. I know something is continually looping that I don't want to, but the fix eludes me. Here is my code
public class PasswordVerifier{
private static int MIN_PASSWORD_LENGTH = 6;
public static boolean isValid(String str){
if (str.length() >= MIN_PASSWORD_LENGTH){
if (PasswordVerifier.isValid(str) == true){
if (PasswordVerifier.hasUpperCase(str) == true){
if (PasswordVerifier.hasLowerCase(str) == true){
if (PasswordVerifier.hasDigit(str) == true){
return true;
}
}
}
}
}
return false;
}
private static boolean hasUpperCase(String str){
for (char c : str.toCharArray()){
if (Character.isUpperCase(c)){
return true;
}
}
return false;
}
private static boolean hasLowerCase(String str){
for (char c : str.toCharArray()){
if (Character.isLowerCase(c)){
return true;
}
}
return false;
}
private static boolean hasDigit(String str){
for (char c : str.toCharArray()){
if (Character.isDigit(c)){
return true;
}
}
return false;
}
}
Any help would be appreciated!

public static boolean isValid(String str){
// ...
if (PasswordVerifier.isValid(str) == true){
// ...
}
// ...
}
You're calling isValid(String) from within itself, which is causing the infinite loop recursion.
I'm going to take a wild guess and say this is what you want instead:
public static boolean isValid(String str){
if (str.length() >= MIN_PASSWORD_LENGTH){
// Removed call to .isValid(String)
if (PasswordVerifier.hasUpperCase(str)){
if (PasswordVerifier.hasLowerCase(str)){
if (PasswordVerifier.hasDigit(str)){
return true;
}
}
}
}
return false;
}

Related

Java Beginner Recursion with boolean

I don't understand why I am getting "return statement missing".
Here is the image with the code:
In your 2nd and 3rd if conditions there are no returns. Instead of your else, just return false.
So it reads:
public class isTrans {
public static boolean isTrans(String s,String t) {
if (t.length()==1 && (s.charAt(s.length()-1))==t.charAt(0)){
return true;
} else if (s.charAt(0)==t.charAt(0)){
return isTrans(s,t.substring(1));
} else if (s.charAt(1)==t.charAt(1)){
return isTrans(s,t.substring(1), t);
}
return false;
}
}
In this case you have to return in all the conditions or return at the end of the method.
if(/*...*/) {
return true;
}
else if(/*...*/) {
return isTrans(/*...*/); // return whatever isTrans returns
}
else if(/*...*/) {
return isTrans(/*...*/); // here too
}
else {
return false;
}
You have to return the result of the function execution in your else if, so the recursion works properly. Like this:
return isTrans(s, t.substring(1))

Binary search tree String search

I have a program where I need to search for a specific word in a binary tree, the code I came up with for the search method for the string is not working. if someone could take a look at it I would greatly appreciate it.
I have tried a few alterations to this but it still did not work.
public boolean check(BSTNode t,String key){
if(!t.word.equals(key)){
check(t.left,key);
check(t.right,key);
}
else{
return true;
}
return false;
}
This could be written like this;
public boolean check(BSTNode t,String key) {
return
t.word.equals(key) || check(t.left,key) || check(t.right,key)
}
or, more verbosely;
public boolean check(BSTNode t,String key) {
if (t.word.equals(key)) return true;
if (check(t.left,key)) return true;
if (check(t.right,key)) return true;
return false;
}
You don't need a lot of else statements because the return statements stop execution in the function.
Edit:
You must also check to see that your BSTNode is not null, or you will get null pointer exceptions when you reach the end of the tree. This could be done at the start of the function, or before the inner recursive check calls:
public boolean check(BSTNode t,String key) {
if (t == null) return false;
if (t.word.equals(key)) return true;
if (check(t.left,key)) return true;
if (check(t.right,key)) return true;
return false;
}
or;
public boolean check(BSTNode t,String key) {
if (t.word.equals(key)) return true;
if (t.left != null && check(t.left,key)) return true;
if (t.right != null && check(t.right,key)) return true;
return false;
}

How would I run this method with the main method?

This is is the code:
public class monkeyTrouble {
public static void main(String[]args){
monkeyTrouble s = new monkeyTrouble();
s.trouble(false, false);
}
public boolean trouble (boolean aSmile, boolean bSmile) {
if (aSmile == true && bSmile == true){
return true;
} else if (aSmile == false && bSmile == false) {
return true;
} else {
return false;
}
}
}
How can I run the boolean method in the main method so it will correctly run?
First, you are already running your method; you aren't displaying or storing the result. You could print the result like
public static void main(String[]args){
monkeyTrouble s = new monkeyTrouble();
System.out.println(s.trouble(false, false));
}
Also, your method could be simplified like
public boolean trouble (boolean aSmile, boolean bSmile) {
return aSmile == bSmile;
}

returning true with an occurance of a letter in a string?

it should return true as there is 'c' in the string S but it keeps returning False?
public class A {
public static void main(String[] args){
System.out.println(contains('c', "cccc"));
}
public static boolean contains(char c, String s) {
boolean to_return = true;
while(true) {
if(s.equals("")){
return to_return = false;
} else {
char c2 = s.charAt(0);
if(c==c2) {
to_return = true;
}
}
s=s.substring(1);
}
}
}
I have NO idea why it isnt? it only returns false if the string is empty which is clearly is(I am NOT allowed to use for loops etc..
you are not returning true anywhere in your code. So recursion happens till s.equals("") is evaluated to true and returns false.
Change
if(c==c2) {
to_return = true;
}
to
if(c==c2) {
return true;
}
works:
public static boolean contains(char c, String s) {
return s.indexOf(c) != -1;
}
If you want a loopy version, use a for loop like this, as it should be cleaner:
public static boolean contains(char c, String s) {
boolean to_return = true;
for(int i=0;i<s.length();i++) {
if(s.charAt(i) != c) {
to_return = false;
}
}
return to_return;
}
Alternatively, you can just do:
public static boolean contains2(char c, String s) {
return s.contains(String.valueOf(c));
}
Can you try a more cleaner method, it seems too cluttered.
public static boolean contains(char c, String s) {
if (s == null)
return false;
if (s.equals(""))
return false;
int size = s.length();
for (int i = 0; i < size; i ++) {
if(s.charAt(i) == c)
return true;
}
return false;
}

How to return a boolean method in java?

I need help on how to return a boolean method in java. This is the sample code:
public boolean verifyPwd(){
if (!(pword.equals(pwdRetypePwd.getText()))){
txtaError.setEditable(true);
txtaError.setText("*Password didn't match!");
txtaError.setForeground(Color.red);
txtaError.setEditable(false);
}
else {
addNewUser();
}
return //what?
}
I want the verifyPwd() to return a value on either true or false whenever I want to call that method. I want to call that method like this:
if (verifyPwd()==true){
//do task
}
else {
//do task
}
How to set the value for that method?
You're allowed to have more than one return statement, so it's legal to write
if (some_condition) {
return true;
}
return false;
It's also unnecessary to compare boolean values to true or false, so you can write
if (verifyPwd()) {
// do_task
}
Edit: Sometimes you can't return early because there's more work to be done. In that case you can declare a boolean variable and set it appropriately inside the conditional blocks.
boolean success = true;
if (some_condition) {
// Handle the condition.
success = false;
} else if (some_other_condition) {
// Handle the other condition.
success = false;
}
if (another_condition) {
// Handle the third condition.
}
// Do some more critical things.
return success;
try this:
public boolean verifyPwd(){
if (!(pword.equals(pwdRetypePwd.getText()))){
txtaError.setEditable(true);
txtaError.setText("*Password didn't match!");
txtaError.setForeground(Color.red);
txtaError.setEditable(false);
return false;
}
else {
return true;
}
}
if (verifyPwd()==true){
addNewUser();
}
else {
// passwords do not match
System.out.println("password do not match");
}
public boolean verifyPwd(){
if (!(pword.equals(pwdRetypePwd.getText()))){
txtaError.setEditable(true);
txtaError.setText("*Password didn't match!");
txtaError.setForeground(Color.red);
txtaError.setEditable(false);
return false;
}
else {
addNewUser();
return true;
}
}
You can also do this, for readability's sake
boolean passwordVerified=(pword.equals(pwdRetypePwd.getText());
if(!passwordVerified ){
txtaError.setEditable(true);
txtaError.setText("*Password didn't match!");
txtaError.setForeground(Color.red);
txtaError.setEditable(false);
}else{
addNewUser();
}
return passwordVerified;
Best way would be to declare Boolean variable within the code block and return it at end of code, like this:
public boolean Test(){
boolean booleanFlag= true;
if (A>B)
{booleanFlag= true;}
else
{booleanFlag = false;}
return booleanFlag;
}
I find this the best way.

Categories