0

so I am fairly new to Java, and most of my previous knowledge is with Python and C++ so I am not really sure how to store data that is inputted so it can be compared. So essentially I am looking to have the user input a bunch of integer values - whatever number they would like. However, when they keep entering these values what will happen is when it is descending (if it descends 3 times in a row) it will break the loop and the program will end. I am not sure how to make the inputted data be stored so it can be compared to the previous entry, or if there is a way to do this without using lists (as I would do in python).

So far what I have is very basic and I need to figure out how to make the if statement in the while loop to be able to compare the previous inputs.

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

    System.out.println("Enter your number: ");

    int n = keyboard.nextInt();
    while (true) {
        if (n > 0) {
            System.out.println("Enter your new number: ")
            int n = keyboard.nextInt();
        }
    }
}

Sorry if this isn't a lot to go on, but I really do not know where to start in terms of comparing the values that are entered in - only really know how to get them to be entered in.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Gramaru
  • 7
  • 2
  • 5
  • If you want to store the numbers, you can add them to an [`ArrayList`](http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html). But you don't necessarily need to store them just to track if the current number is less than the previous number. – khelwood Jan 23 '17 at 16:08
  • As khelwood said, save them in an ArrayList, or in a Stack. Then you can check the last three entries in the ArrayList and verify if they are all descending, or peek the top three in the Stack and make the same check. – pringi Jan 23 '17 at 16:10
  • Also see this http://stackoverflow.com/questions/18005437/checking-an-array-for-descending-order – pringi Jan 23 '17 at 16:11
  • Khelwood - if I do not store them in the ArrayList, what is the way to compare them to the previous input? – Gramaru Jan 23 '17 at 16:12
  • Oh wow, thank you Pringi, that question is essentially what I am looking for - cheers! – Gramaru Jan 23 '17 at 16:13
  • @Gramaru You can refer to elements in an ArrayList with get(int); – Steve Smith Jan 23 '17 at 16:13

1 Answers1

0

I missed the comment, that you only want to know how to check if the list is sorted. So I wrote some code that solves your issue :) You may compare it to your solution:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {

    public static final int SIZE = 3;
    private static List<Integer> lastThreeValues;

    public static void addNewValue(int value) {
        if (isFull()) {
            lastThreeValues.remove(0);
        }
        lastThreeValues.add(value);
    }

    public static boolean isFull() {
        return lastThreeValues.size() == SIZE;
    }

    public static boolean isDescending() {
        int prev = Integer.MAX_VALUE;
        for (int value : lastThreeValues) {
            if (prev > value) {
                prev = value;
            } else {
                return false;
            }
        }
        return true;
    }

    public static boolean close() {
        if (isFull()) {
            return isDescending();
        }
        return false;
    }

    public static void main(String[] args) {
        lastThreeValues = new ArrayList<>(SIZE);

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter your number: ");
        int n = keyboard.nextInt();
        addNewValue(n);

        do {
            addNewValue(n);

            if (n > 0) {
                System.out.println("Enter your new number: ");
                n = keyboard.nextInt();
            }
        } while (!close());

        keyboard.close();
        System.out.println("closed");
    }
}

Some more thoughts:

Community
  • 1
  • 1
ppasler
  • 3,579
  • 5
  • 31
  • 51