0

I want to manually read in some values in my code. It works fine for all the values except the last one(lambda2). I can infinitely keep on typing values, even when they are not doubles, without anything happening. If I type something else with any other of the other values I get an error message, as well as with the first time I enter lambda2. I did it in a similar (only creating a different object in the end) way in another code and it worked fine.

    System.out.print("All the asked input must be divided by 'Enters' \n");
    Scanner userinput = new Scanner(System.in);
    double b1;
    double b2;
    System.out.print("Enter the backlog costs for product 1 and 2: \n");
    b1 = userinput.nextDouble();
    b2 = userinput.nextDouble();
    double h0;
    double h1;
    double h2;
    System.out.print("Enter the holding costs for components 0, 1 and 2: \n");
    h0 = userinput.nextDouble();
    h1 = userinput.nextDouble();
    h2 = userinput.nextDouble();
    double lambda1;
    System.out.print("Enter the demand rate of product 1: \n");
    lambda1 = userinput.nextDouble();
    double lambda2;
    System.out.print("Enter the demand rate of product 2: \n");
    lambda2 = userinput.nextDouble();
    userinput.close();  

    double c1 = b1 + h0 + h1;
    double c2 = b2 + h0 + h2;
    SP solvable = new SP( b1, b2, h0, h1, h2, lambda1, lambda2, c1, c2);
    return solvable;
avk
  • 871
  • 1
  • 9
  • 22
Rosa
  • 1

1 Answers1

1

You have to have some way to indicate to your program that you are finished collecting input. After you've entered your last value, there are no further instructions to be executed before taking more input, so it will keep trying to collect and never pass control back to your program. Check this answer for a comprehensive explanation of how this can be achieved.

Community
  • 1
  • 1
adam5990
  • 346
  • 2
  • 13