Search code examples
javabluejparseint

Why is the variable String type, even though I converted it to int?


The variable int g is supposed to be a int, but on output it is being taken as a String, can't figure out what's wrong.

class scantest
    {
        public static int String_to_Int(String a)       
        {
            int n=Integer.parseInt(a);
            return n;
        }
        public static int brooh()
        {
            String a="43";
           int s=String_to_Int(a);
            return s;
        }
        public static void main()
        {
            int g=brooh();
            System.out.println(g +"\n" +g+1);
        }
    }

Output

43
431

Solution

  • All arithmetic (in your case +) inside of a System.out.println statement, must be done INSIDE parenthesis.

    Your print statement would look like this:

    System.out.println(g +"\n" + (g+1));
    

    Note that g+1 is inside parenthesis