2
votes

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
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.MWB
what have you tried so far?Scary Wombat
Please share a code with your attempting to achieve the result. Meanwhile please read a reference How to create a Minimal, Complete, and Verifiable exampleYuriy Tsarkov
You code will only compare after entering a set of two numbers and only comparing these numbers to each other.Scary Wombat

4 Answers

1
votes

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;
}
0
votes

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!");
-1
votes

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.

-1
votes

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+"";
        }