0

Hello I'm a beginner in Java and i'm finding issuses to increment the variable age as it is red as a string or it is an integer This is the Code :

   public static void main(String[] args)
{
    Scanner name= new Scanner(System.in);
    System.out.println("Hello your name is "+name.nextLine());
    Scanner in = new Scanner(System.in);
    int age = in.nextInt();
    System.out.println("In 17 September i will become "+age+1+" years old");

}
Malek Ajmi
  • 11
  • 1
  • 2
    It's a bit non-obvious, but the linked question provides the answer. Basically, `"... become " + age + 1 + " years..."` is evaluated as `(("... become " + age) + 1) + " years..."`, not `"... become " + (age + 1) + " years..."`, as you wanted. – yshavit Aug 18 '17 at 15:28

1 Answers1

0

Whenever you add a String in a print statement, all further + signs are considered to be the concatenation operator, instead of the addition sign.

To fix, enclose it in brackets, like (age+1).

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34