2

I am a new-bee in java, I have a problem that i cant figure out to compare previous entered number(int) with next one continuously and I need to write a program that repeatedly reads numbers from the user’s keyboard. The program stops looping when the user types the same number twice in a row.

Thanks in advance for your kind guidance.

Here’s a sample run of the program: 5 13 21 5 4 5 5 Done!

Following was my unsuccessful effort :)

Scanner input = new Scanner(System.in); System.out.println("Enter Numbers");

    int x = 0;
    int y = 0;
    x = input.nextInt();
    y = input.nextInt();
    while (x != y) {

        x = input.nextInt();
        y = input.nextInt();

    }

    System.out.println("Done!!!!!!!");
    input.close();
4
  • 3
    Just store the previous value in a variable and check whether or not the new value is the same as the previous value. If so: stop loop, if not, update previous value and re-enter the loop. Commented Oct 31, 2018 at 7:06
  • 4
    what have you tried so far? Commented Oct 31, 2018 at 7:08
  • Please share a code with your attempting to achieve the result. Meanwhile please read a reference How to create a Minimal, Complete, and Verifiable example Commented Oct 31, 2018 at 7:17
  • You code will only compare after entering a set of two numbers and only comparing these numbers to each other. Commented Oct 31, 2018 at 8:22

4 Answers 4

1

You can use loop to read number from console and stop if previous nubmer equals to current one. As marker of first number you can use e.g. null value of Integer prv (as alternative, you can use boolean isFirstLine flag for first line or res.isEmpty()).

public static List<Integer> receiveNumbers() {
    List<Integer> res = new LinkedList<>();
    Integer prv = null;

    try (Scanner scan = new Scanner(System.in)) {
        while (true) {
            int num = scan.nextInt();

            if (prv != null && prv == num)
                break;

            res.add(num);
            prv = num;
        }
    }

    return res;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@ScaryWombat Yes. This is Integer but not int
@ScaryWombat No, this is not enough. If prv == null, then if(prv == num) throws NPE, because if(prv == num) is if(prv.intValue() == num)
Sorry, I was missing that
0

import java.util.Scanner;

public class OngoingPractice {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int previous, current;

    previous = keyboard.nextInt();
    current = keyboard.nextInt();

    while (current != previous) {
        previous = current;
        current = keyboard.nextInt();
    }

    System.out.println("Done!");

Comments

-1

Just use an infinite while loop and break if the new int is equal to previous one. As a suggestion you should show how you tried.

Scanner sc = new Scanner(System.in);
    int i;
    Integer j = null;
    boolean flag = false;
    while(true) {
        i = sc.nextInt();
        if(j==null) {j=i; flag = true;}
        if(j==i&&!flag) {
            System.out.println("Done");
            break;}
        j=i;
        flag = false;
    }

Edited : if first one is -1, it won't work as in the comment. so I modified some.

2 Comments

what happens if the first number entered is -1
@ScaryWombat My modification should do the job.
-1

Remember the last entered data in one variable and check it with the current data. If both matches, break the loop.

        Scanner scanner = new Scanner(System.in);            
        String previousNumber="";
        while (scan.hasNextInt()) {
          int number = scan.nextInt(); 
          if(!previousNumber.equals("") && number==Integer.parseInt(previousNumber)) { 
            break;
          }else {
            System.out.println(number);
          }            
          previousNumber=number+"";
        }

2 Comments

What if first number will be 0?
Condition can be handled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.