1

I'm trying to make a simple program where you can put integers in, and it will tell you if it increased or decreased from the previous integer input. But when I run the code, I have to put an integer value twice, but I only want it put once.

The input and output should look like (numbers typed by me, words output by the program):

Starting... 5 Increasing 4 Decreasing 6 Increasing 

etc. etc.

But instead it looks like:

Starting... 5 5 Increasing Input Number: 1 2 Not Increasing 

etc. etc.

This is my code:

import java.util.Scanner; public class Prob1 { public static void main(String[] args) { System.out.println("Starting..."); int input; int previousInput = 0; Scanner scan = new Scanner(System.in); while (!(scan.nextInt() <= 0)) { input = scan.nextInt(); if (input > previousInput) { System.out.println("Increasing"); previousInput = input; } else { System.out.println("Not Increasing"); previousInput = input; } System.out.println("Input Number:"); } scan.close(); } } 

Why does this problem occur, and how can I fix it?

2
  • 2
    Welcome to Stack Overflow. Please try to think about the code carefully, a step at a time. Where it says while (!(scan.nextInt() <= 0)) {, what do you expect that to do? Where it says input = scan.nextInt();, what do you expect that to do? Between those two lines of code, how many times do you see the code scan.nextInt()? When you run the code, how many times do you have to type the number? Do you see a correlation? Commented Jul 6, 2022 at 3:22
  • 2
    The way it's written, there's a scan.nextInt() in the while statement, so that runs first, and then if the rest of the condition is true (so, if !(scan.nextInt() <= 0) returns "true") the next line is one more scan.nextInt(). Each pass through the loop, the same thing will happen – the while condition, then one more inside the loop. Commented Jul 6, 2022 at 3:23

3 Answers 3

2

The loop behavior you are describing is:

  • read a numeric input value
  • do something with it (print a message)
  • if the loop value meets a condition (input is 0 or less), exit the loop
  • otherwise, repeat

Here's a "do-while" loop that reads like those steps above:

Scanner scan = new Scanner(System.in); int input; do { input = scan.nextInt(); System.out.println("input: " + input); } while (input > 0); System.out.println("done"); 

And here's input+output, first entering "1", then entering "0":

1 input: 1 0 input: 0 done 
Sign up to request clarification or add additional context in comments.

Comments

0

while (!(scan.nextInt() <= 0)) { takes an int and then input = scan.nextInt(); takes another one. You need to change the while loop to use input.

Comments

0

modified based on your code

 public static void main(String[] args) { System.out.println("Starting..."); int input; int previousInput = 0; Scanner scan = new Scanner(System.in); while (true) { System.out.println("Input Number:"); input = scan.nextInt(); if (input <= 0) { System.out.println("app shut down"); break; } if (input > previousInput) { System.out.println("Increasing"); } else { System.out.println("Not Increasing"); } previousInput = input; } scan.close(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.