James Smith
James Smith

Reputation: 13

Scanner input needed twice, when only wanted once

However I'm running into an issue, when trying to input a number, it issues it twice? So I type (x) number and it will space down, and ask for another number? even though I just input one?

(see output code to understand better)

Here is my code:

import java.util.Scanner;

public class FahrenToCelcius {
    private static Scanner sc;

    public static void main(String[] args) {
        double celsius, fahrenheit;
        System.out.println("Enter your temperatures");
        sc = new Scanner(System.in);
        int[] temperatures = new int[5];
        for(int i = 0; i < 5; ++i) {
            temperatures[i] = sc.nextInt();

            fahrenheit = sc.nextDouble();
            celsius =(fahrenheit-32)*(0.5556);
            System.out.println("Temperature in Celsius:"+celsius); 

        }    
    }
}

Output:

Enter your temperatures
100
100
Temperature in Celsius:37.7808
100
100
Temperature in Celsius:37.7808
100
100
Temperature in Celsius:37.7808

I only want the user too have to input it once. Help?

Upvotes: 0

Views: 60

Answers (1)

AxelH
AxelH

Reputation: 14572

You are not using the array int[] temperatures so you could remove the statement that insert in it...

If you really want to keep the value in an array. Simply use a Double[] temperatures then read your scanner.

...
fahrenheit = sc.nextDouble();
temperatures[i] = fahrenheit;
...

In short, only read the scanner ONCE if you want only one input..

Upvotes: 2

Related Questions