1

I am very new to Java and have been trying to figure out this question. Why does taking an input after print and taking it after println differ the order of execution.

    Scanner input = new Scanner(System.in);
            System.out.println("Enter real part of the number:");
            r=input.nextInt();
            System.out.println("Enter imaginary part of the number:");
            i=input.nextInt();

Output

    Enter real part of the number:
     1 
    Enter imaginary part of the number:
     2
    Scanner input = new Scanner(System.in);
            System.out.print("Enter real part of the number:");
            r=input.nextInt();
            System.out.print("Enter imaginary part of the number:");
            i=input.nextInt();

Output

    1
    2
Enter real part of the number:Enter imaginary part of the number:
Smaran Das
  • 11
  • 1
  • 2
    That code will execute in order. You are experiencing some other issue that we can only guess at, for example your IDE/console may have an input buffer that is at fault. – sorifiend May 31 '21 at 05:30
  • 2
    try running this code in terminal. The statement will print in order of execution. – Yoshikage Kira May 31 '21 at 05:35
  • Your Scanner is just reading the integer. You have to add an additional input.nextLine to consume the line end. – Gilbert Le Blanc May 31 '21 at 07:50

2 Answers2

0

print - print method is implemented as it prints the text on the console and the cursor remains at the end of the text at the console.

println -On the other hand, println method is implemented as prints the text on the console and the cursor remains at the start of the next line at the console and the next printing takes place from next line.

0

I found out that the error or problem is specific to apache netbeans. Using a different software to write or run java programs will not face any such issue.

Smaran Das
  • 11
  • 1