Search code examples
javajava.util.scannerprintln

Java: different output when scanning input with spaces or enters as delimiters


I'm trying to get input from the user and print it to the console using Java 8 (IDE BlueJ, Windows 10). When printing the output there is a bug: the program prints Equation 2 twice instead of printing Equation 1 and Equation 2.

This is the code:

import java.util.Scanner;

public class Equations
{
        public static void main (String[] args)

    {
        Scanner scan = new Scanner (System.in); 

        System.out.println("This program solves a system of 2 linear equations");
        System.out.println("Enter the coefficients a11 a12 a21 a22 b1 b2");

        int a11 = scan.nextInt();
        int a12 = scan.nextInt();
        int a21 = scan.nextInt();
        int a22 = scan.nextInt();
        int b1 = scan.nextInt();
        int b2 = scan.nextInt();

        System.out.println("Eq1: "+ a11 +"*x1+"+ a12 +"*x2="+ b1);
        System.out.println("Eq2: "+ a21 +"*x1+"+ a22 +"*x2="+ b2);

    }
}

this is the expected output:

This program solves a system of 2 linear equations Enter the coefficients a11 a12 a21 a22 b1 b2
1 2 3 4 5 6
Eq1: 1*x1+2*x2=5
Eq2: 3*x1+4*x2=6

And this is the output:

This program solves a system of 2 linear equations Enter the coefficients a11 a12 a21 a22 b1 b2
1 2 3 4 5 6
Eq2: 3*x1+4*x2=6
Eq2: 3*x1+4*x2=6

Note that the bug exists only when typing the input on a single line with white spaces between the numbers, and does not exist when pressing the Enter key after each number.

Meaning, if the input comes one number at a time, the expected output is recieved correctly:

This program solves a system of 2 linear equations Enter the coefficients a11 a12 a21 a22 b1 b2

1

2

3

4

5

6

Eq1: 1*x1+2*x2=5
Eq2: 3*x1+4*x2=6

Since it's hard to believe and hardly reproducible here is a screenshot: enter image description here

What causes the difference when the input comes in a single line, separated by spaces, vs when it comes in separate lines, separated by enters?

How to have the desired output when the input comes in a single line format?


Solution

  • Looks like a bug with your IDE. Consider the following:

    import java.util.Scanner;
    
    public class Equations
    {
        public static void main (String[] args) {
    
            Scanner scan = new Scanner("1 2 3 4 5 6");
    
            System.out.println("This program solves a system of 2 linear equations");
            System.out.println("Enter the coefficients a11 a12 a21 a22 b1 b2");
    
            int a11 = scan.nextInt();
            int a12 = scan.nextInt();
            int a21 = scan.nextInt();
            int a22 = scan.nextInt();
            int b1 = scan.nextInt();
            int b2 = scan.nextInt();
    
            System.out.println("Eq1: "+ a11 +"*x1+"+ a12 +"*x2="+ b1);
            System.out.println("Eq2: "+ a21 +"*x1+"+ a22 +"*x2="+ b2);
    
        }
    }
    

    It's exactly the same code, with the exception that it does not rely on user input. The input is separated by spaces, and the output is the expected:

    This program solves a system of 2 linear equations
    Enter the coefficients a11 a12 a21 a22 b1 b2
    Eq1: 1*x1+2*x2=5
    Eq2: 3*x1+4*x2=6
    

    See online Java compiler

    Try to set the delimiter explicitly:

    scan = new Scanner(System.in).useDelimiter(" |\n");