-1

Hello I am trying to build a simple program that converts a string containing a number into an integer. I am receiving the error on the System.out.println and not sure why, can anyone help?

public class TypeConvert {

int strToInt;

public int convert (String s){

    strToInt = Integer.parseInt(s);
    return strToInt;    
}

public static void main(String[] args) {
    String strNumber=("100");

    TypeConvert convertToInt = new TypeConvert();
    convertToInt.convert(strNumber);

    System.out.println(strToInt);


  }

}

This has been marked as duplicate so I am editing. I did actually read all the relevant posts to my problem but as I did not understand how to fix my problem with theirs I created my own post.

1
  • 1
    If you do not understand something about the other question or its answers, please point out what you don't understand. Commented Jan 13, 2015 at 0:52

2 Answers 2

2

Change this,

System.out.println(strToInt);

to

System.out.println(convertToInt.strToInt);

because strToInt is a field of the TypeConvert instance (which you've named convertToInt).

Alternatively, you could write

System.out.println(convertToInt.convert(strNumber));

since the convert function returns the result.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this worked. I'd like to understand why though - I thought something like this wouldn't be needed as strToInt is a global variable? Why do I need to refer to the method to print the result? Sorry, I'm very new to Java as you can probably tell. Thanks.
int strToInt is a package level instance field. If you wanted it to be a class level field (one per class, not one per instance), it would need to be static int strToInt. I don't think that's what you want, since you have a TypeConvert instance.
0

Your convert method should be a static method since she doesn't need any "state" information.

public class TypeConvert {
    public static int convert (String s){
        int strToInt = Integer.parseInt(s);
        return strToInt;    
    }

    public static void main(String[] args) {
        String strNumber=("100");

        int strToInt = TypeConvert.convert(strNumber);

        System.out.println(strToInt);
    }
}

Usually you create non-static fields and instance methods when you need to use a state. For example let's print the name of a person:

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public static void main(String[] args) {
        Person bob = new Person("Bob");
        Person john = new Person("John");

        System.out.println(bob.getName()); // Prints "Bob"
        System.out.println(john.getName()); // Prints "John"
    }
}

In this case you absolutely need to "save" the name variable in a property of the instance because each Person is going to have a different name.

In the example you gave us, for a given string, the ouput will always be the same so you can use a static method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.