Home > Back-end >  Need Java help to set and return a string
Need Java help to set and return a string

Time:07-10

I am new to java trying to complete an introductory course and am really struggling. I have included the code I believe should be working but when i run the test they fail and return null. can anyone help to see what is wrong with my code? thank you so much

public class CustomString {
    String myString;
    boolean isSet;

    public CustomString() {
        this.myString = (null);
        this.isSet = (false);
    }
}

if the string hasn't been set with setString it should return null. setString should set the value of the current string and set this.isSet to true. and if the given string is null, sets this.isSet to false

public String getString() {
    if (this.myString == null || this.isSet == false) {
        return null;
    } else {
        return this.myString;
    }
}

public void setString(String string) {
    if (this.myString == null) {
        this.isSet = false;
    } else {
        this.isSet = true;
    }
}

CodePudding user response:

Few pointers on the code generally:

  • You don't need to use this.foo unless foo is already defined in the method's scope
  • You don't need brackets around (null) or (false)
  • If myString is not set, it will be null, so the logic in getString is useless, as just return myString would have the same result, unless isSet can be changed in a way other than setString

As for the issue, setString doesn't actually set the string, you'd need to add myString = string at the start.

CodePudding user response:

setString should set the value of the current string and set this.isSet to true. and if the given string is null, sets this.isSet to false

You are describing that setString() should:

  • Save the new string value (if it isn't null) and also record a value of "true" for isSet
  • Record a value of "false" for isSet if the input string is "null"

Your code was close. But instead of checking this.myString == null, you will want to check against the input value (I named it "newString" below). Also, if that input string is non-null, you weren't saving that "newString" value anywhere; you can see that the code below does save the new string with this.myString = newString.

public void setString(String newString) {
    if (newString == null) {
        this.isSet = false;
        // seems like a good idea to also do: this.myString = null;
    } else {
        this.isSet = true;
        this.myString = newString;
    }
}

CodePudding user response:

You have the logic now on setter-level. Perhaps it placed better on the getter-level (less logic required).

public class CustomString {

    private String myString = null;

    public boolean isSet() {
        return myString != null;
    }

    public void setString(String newValue) {
        myString = newValue;
    }

    public String getString() {
        return myString;
    }
}

And now you can shortcut your whole class with lombok.

@Data
public class CustomString {

    private String myString;

    public boolean isSet() {
        return myString != null;
    }
}

CodePudding user response:

you have two problems in the method setString.

  1. You need set the string in the attribute myString
  2. You need check if the "string" value is null, and not "myString"

Some like that:

public void setString(String string) {
        if (this.string == null) {
           this.isSet = false;
        } else {
           this.isSet = true;
           this.myString = string;
        }
}
  •  Tags:  
  • java
  • Related