C. Miller
C. Miller

Reputation: 5

Scanner not waiting for NewLine to collect input with a loop

I am very new to coding and was hoping you guys could help me. I am having an issue with nextDouble not waiting for a print.ln to collect data. With my priming input I have to put in two numbers for it to move to the next line and ask for the second number that I already put in because it didn't wait. By that point it also decides to ask for the third number and accept input for that one as well, but it is fine asking for third and the fourth numbers with no issues. It also happens inside the loop when it repeats.

I understand that using nextDouble doesn't mean it will "wait" for the newline and I know nextNumbertype doesn't have a newline flag but the usual trick of adding in "input.nextLine" after getting a double does not seem to be working.

I know I could just take all input as strings and convert them later, but this is a homework assignment and my prof seems to want things a very specific way.

Any ideas for what is wrong or what I could try?

Here is what outputs:

Enter x1 or <crtl + z> to quit
1
4
Please input the x2 number:
Please input the y1 number:
1
Please input the y2 number:
5
Distance is 5.000000 

Enter x1 or <crtl + z> to quit

Here is the actual code:

import java.util.Scanner;

public class distanceFinder {

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    double x1 = 0;

    System.out.println("Enter x1 or <crtl + z> to quit");
    x1 = input.nextDouble();


    while(input.hasNext())
    {

    System.out.println("Please input the x2 number:");
    double x2 = input.nextDouble();

    System.out.println("Please input the y1 number:");
    double y1 = input.nextDouble();

    System.out.println("Please input the y2 number:");
    double y2 = input.nextDouble();

    double x3 = 0;
    double y3 = 0; 
    double xy1 = 0;

    x3 = Math.pow((x2 - x1), 2);
    y3 = Math.pow((y2 - y1), 2);

    xy1 = x3 + y3;

    double distance = Math.sqrt(xy1);

    System.out.printf("Distance is %.6f %n%n%n",  distance);

    System.out.println("Enter x1 or <crtl + z> to quit");
    x1 = input.nextDouble();

    }
input.close();
}

}

Upvotes: 0

Views: 105

Answers (2)

Vasyl Lyashkevych
Vasyl Lyashkevych

Reputation: 2222

You need to correct your code:

import java.util.Scanner;

public class DistanceFinder {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter x1 or <crtl + z> to quit");

        while (input.hasNext()) {
            double x1 = input.nextDouble();

            System.out.println("Please input the x2 number:");
            double x2 = input.nextDouble();

            System.out.println("Please input the y1 number:");
            double y1 = input.nextDouble();

            System.out.println("Please input the y2 number:");
            double y2 = input.nextDouble();

            double x3 = 0;
            double y3 = 0;
            double xy1 = 0;

            x3 = Math.pow((x2 - x1), 2);
            y3 = Math.pow((y2 - y1), 2);

            xy1 = x3 + y3;

            double distance = Math.sqrt(xy1);

            System.out.printf("Distance is %.6f %n%n%n", distance);

            System.out.println("Enter x1 or <crtl + z> to quit");
        }
        input.close();
    }
}

OUTPUT:

Enter x1 or <crtl + z> to quit
1
Please input the x2 number:
2
Please input the y1 number:
1
Please input the y2 number:
3
Distance is 2.236068 


Enter x1 or <crtl + z> to quit
1
Please input the x2 number:
2
Please input the y1 number:
1
Please input the y2 number:
3
Distance is 2.236068 


Enter x1 or <crtl + z> to quit

Upvotes: 0

shmosel
shmosel

Reputation: 50716

The problem is that Scanner.hasNext() blocks until there's input. Once you type something, it enters the loop, executes the first print statement, grabs the input, prints the second statement, and waits for the next input.

Another issue is that you're checking hasNext() before reading x2 instead of before x1. Try this structure instead:

System.out.println("Enter x1 or <crtl + z> to quit");
while (input.hasNext()) {
    double x1 = input.nextDouble();

    System.out.println("Please input the x2 number:");
    double x2 = input.nextDouble();

    //...

    System.out.println("Enter x1 or <crtl + z> to quit");
}

Upvotes: 1

Related Questions