简体   繁体   中英

Java returning null and result

can someone take a look at this example and tell me why I get null10 as printed value instead of 10 only?

and is there and easier solution for this program without using global String variable "word"

public class UserInput {

    public static String word;

    public static class TextInput {
        public void add(char c) {
            word = word + c;
        }

        public static String getValue() {
            return word;
        }
    }

    public static class NumericInput extends TextInput {
        @Override
        public void add(char c) {
            if (Character.isDigit(c)){
                word = word + c;
            }
        }
    }

    public static void main(String[] args) {
        TextInput input = new NumericInput();
        input.add('1');
        input.add('a');
        input.add('0');
        System.out.println(input.getValue());

    }
}

EDIT: I need use inherits from TextInput

You need to give your static word field an initial value, otherwise it will default to being null . And when Java concatenates String objects, it will treat a null reference as the literal string "null". So you're effectively always starting off with the String "null".

If you give your class field a starting value of "" (empty string) then your code should do what you expect.

With regard to a better way of doing this, I would instead give the class a non-static field of type StringBuilder (and initialise it so that it's not null ). Then your add method can simply append(c) the new characters to the StringBuilder field object, which will be more efficient than repeatedly using string concatenation (which is what you get with word + c ).

You are not initializing input , so it is null . You need to initialize input first in order to make concatenating work.

So, use this:

public static String word = "";

Rather than using a static variable that is shared over all instances and children of the TextInput class, you should be using an instance variable.

You'll still have to initialize a non null value

That would look like

public static class TextInput {
    protected String word;

    public TextInput() {
        this.word = ""; 
    } 

    public void add(char c) {
        word = word + c;
    }

    public String getValue() {
        return word;
    }
}

To better understand the problem, try your code with this

    TextInput input = new TextInput();
    input.add('a');
    System.out.println(input.getValue());
    TextInput input2 = new NumericInput();
    input2.add('1');
    input2.add('0');
    System.out.println(input2.getValue());

Additional, see @Bobulous comment about using StringBuilder

You were not initializing the "word".

public class TextInput {
        public static String word=""; // a lil change here
        public static class TextInput {
            public void add(char c) {
                word += c;
            }
            public String getValue() {
                return word;
            }
        }
        public static class NumericInput extends TextInput {
            public void add(char c) {
                if (Character.isDigit(c)){
                    word += c;
                }
            }
        }
        public static void main(String[] args) {
            NumericInput input = new NumericInput();
           input.add('1');
           input.add('a');
           input.add('0');
           System.out.print(input.getValue());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM