why my scanner can only take two numbers? Java
I am trying to use a Scanner object to read keyboard input (double type
numbers). The program compiles, but it can only take two numbers. Below is
my code, please help me find the reason. Thank you!
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
ArrayList<String> container = new ArrayList<String>();
double number = 0;
System.out.println("Type in the polynomials in increasing powers:");
while (!keyboard.nextLine().isEmpty())
{
// number = keyboard.nextDouble();
try {
number = keyboard.nextDouble();
} catch(Exception e) // throw exception if the
user input is not of double type
{
System.out.println("Invalid input");
}
container.add("-" + number);
}
No comments:
Post a Comment