I'm very new to java and I'm wondering on how to stop the user from entering an input after the 10th. (inputs are separated by space)
sample output would be:
Please type 10 temperatures in Celsius: 30 12 20.5 ..
Temperatures in Celsius: 30.0 12.0 20.5 …
Temperatures in Fahrenheit: 86.0 53.6 68.9 …
Here's my code:
import java.util.Scanner;
public class array {
public static void main(String args[]) {
double [] a = new double [10];
Scanner sc=new Scanner(System.in);
System.out.print("Please type 10 temperatures in Celsius: ");
for(int j=0; j<10; j++)
a[j] = sc.nextDouble();
System.out.print("Temperatures in Celsius: ");
for (int i=0;i<a.length;i++)
System.out.print(a[i] + " ");
System.out.print("\nTemperatures in Fahrenheit: ");
for (int i = 0; i < a.length; i++)
a[i] = a[i]*1.8+32;
for (int i = 0; i < a.length; i++) {
System.out.printf("%.1f ", a[i]);
}
}
}
This is not possible, because input is provided from a terminal/shell, and this terminal usually reads input linewise. You have to do much more low level coding to handle scenarios like these.
Just output an error if the users enters more than 10 values and let him enter them again.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments