-2

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
Progman
  • 16,827
  • 6
  • 33
  • 48

1 Answers1

1

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

goatofanerd
  • 468
  • 2
  • 12
  • It should be noted that this isn't something that's magically different inside `System.out.println`: those are general rules that apply to string concatenation in any context. So `String s = g + "\n" + g + 1` would face the same issue. – Joachim Sauer Aug 22 '21 at 07:36
  • It is even more basic than that. Think operator precedence. – kriegaex Aug 22 '21 at 07:38
  • No problem! Mark this as the accepted answer and make sure to read the other comment left as it provides a bit more insight :) – goatofanerd Aug 22 '21 at 07:38