-5

I am supposed to get the product of the integer entered by user util a number that is less than 1 is entered, and print the product. But when the program does nothing after I enter the numbers. How can I fix this.

Here is the code:

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter integers: "); 
    int num = 1; 
    int product = 1; 
    while(input.hasNextInt() && num>=0){ 
       num = input.nextInt(); product = product*num; 
     } 
    System.out.print("Product: "+product); 
} 
Eric S
  • 1,336
  • 15
  • 20
nate lin
  • 1
  • 1
  • 2
    Can you share your code? – Amer Qarabsa Sep 29 '16 at 17:16
  • public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter integers: "); int num = 1; int product = 1; while(input.hasNextInt() && num>=0){ num = input.nextInt(); product = product*num; } System.out.print("Product: "+product); } – nate lin Sep 29 '16 at 17:18
  • 1
    Hi Nate. As Amer said, please share your code as text. I suggest you check out http://stackoverflow.com/help/how-to-ask to help you get started. A properly formatted question will get answered a lot quicker. – Eric S Sep 29 '16 at 17:19
  • http://stackoverflow.com/questions/26566773/how-to-use-nextint-and-hasnextint-in-a-while-loop – Tom Sep 29 '16 at 17:19
  • I edited my question so you guys can see the code – nate lin Sep 29 '16 at 17:26

3 Answers3

1

First, your test num>=0 is not a test for ending the loop when "a number that is less than 1 is entered", since 0 is less then 1 but won't end the loop.

Second, the test of num is done after num has been applied to product. You don't want that.

So you need to test num after getting it, but before applying it to product. If less than 1, you want to end the loop, which you can do with a break statement.

Also, it is generally bad style to put multiple statements on a single line.

System.out.println("Enter integers:");
int product = 1;
while (input.hasNextInt()) {
    int num = input.nextInt();
    if (num < 1)
        break;
    product *= num;
}
System.out.println("Product: " + product);
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

In your code, the while loop keep running forever. So that's why it do not print anything.

Try this instead:

 while(input.hasNextInt() && num>=0){ 
   num = input.nextInt(); 
   product = product*num; 
   System.out.print("Product: "+product); 
 } 

EDIT: Try to enter the number less than 0 then your while loop should terminate.

  • I've tried what you said but that's not what I need. this is what I need: for example, I enter 2,3 and -2, I want the program to print 6 for the product. – nate lin Sep 29 '16 at 17:34
0

You could try something like this:

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter integers: "); 
    int num = 1; 
    int product = 1;
    while(true)
    {
        try
        {
            num = input.nextInt();
            if(num < 0)
            {
                break;
            }
            product = product * num;
        } catch(InputMismatchException e)
        {
            System.out.println("Please enter a number.");
            input.nextLine();
        }
    }
    System.out.print("Product: "+product);
}
brso05
  • 13,142
  • 2
  • 21
  • 40
  • 1) Code without explanation is less helpful. 2) It is generally better to use `hasNextInt()`, rather than using exceptions. See [Defensive Programming vs Exception Handling?](http://programmers.stackexchange.com/q/139171/202153) – Andreas Sep 29 '16 at 17:49
  • @Andreas That is your opinion. Also if they need an explanation they can ask and I will explain it. – brso05 Sep 29 '16 at 17:50
  • @Andreas also way to link to a `python` question...this is **java**. – brso05 Sep 29 '16 at 17:51
  • The purpose of StackOverflow is to provide a repository of answers to questions, for the benefit of the general public. Your answer is not just for the person who asked the question, but for everybody who will find the question in the future when searching for similar problems. Please consider that when answering questions. – Andreas Sep 29 '16 at 17:52
  • @Andreas there are multiple answers and other similar questions. If they can't find what they are looking for in my answer I'm sure there are plenty more options. – brso05 Sep 29 '16 at 17:53
  • "Defensive Programming vs Exception Handling" is a *programming* topic. The language is of minor concern. Did you notice that the Python question accepted a .NET answer? It is equally applicable to Java. But you're right. It is an opinion, though one that is generally accepted. – Andreas Sep 29 '16 at 17:53
  • @Andreas it doesn't change the fact that it is an **opinion**. People have different styles. It doesn't make one way right vs. another way. – brso05 Sep 29 '16 at 17:55
  • And I'm allowed to leave a *comment* based on an opinion. Besides, I never said you're wrong. I used the word "generally", with link to article backing my "opinion". – Andreas Sep 29 '16 at 18:02
  • Sure you can: `while (! hasNextInt()) { tell user input is bad and prompt again }` – Andreas Sep 29 '16 at 18:06