To enter "N" twice to display related information

I am trying to code something that will sort the numbers in ascending or descending order depending on what the user enters. The program can sort them in ascending order when the user enters "Y", however, if they enter "N" to sort in descending order, the user has to put the "N" twice before it displays. I've posted the syntax below, so if anyone wants to tell me what's missing / done wrong, feel free to do it.

import java.util.*;

public class SortProgram { 
    public static void main(String args[]) { 
        Scanner in = new Scanner(System.in);
        System.out.println("\nHow many numbers do you want sorted?: ");
        int count = in.nextInt();
        int[] array = new int[count];

        for (int i = 0; i < count; i++) {
            System.out.print("Enter number #" + (i+1) + ": ");
            array[i] = in.nextInt();
        }

        System.out.print("\nPress 'Y' to sort numbers in  order, press 'N' to sort numbers in DESCENDING order: ");

        in.nextLine();

        boolean ascending = true;
        boolean descending = false;

        if (in.nextLine().toUpperCase().charAt(0) == 'Y') {
            ascending = true;
        } else if (in.nextLine().toUpperCase().charAt(0) == 'N') {
            descending = true;
            ascending = false;
        }

        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count - 1; j++) {
                if (ascending) {
                    if (array[j] > array[j + 1]) {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                } else if (!ascending) {
                    if (array[j] < array[j + 1]) {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
        }
        System.out.print("\nThe sorted numbers are: ");

        for (int i = 0; i < count; i++) {
            System.out.print(array[i] + " ");
        }
    }
}

      

+3


source to share


1 answer


It looks like you are calling twice in.nextLine()

. Each time you call this, the program reads in a different case from the user.

By default, your code runs in ascending order. However, since you are typing input twice, you need to type "N" twice (when it is "counting", ie the second time you actually use the result) for it to work.

The fix is ​​simple: just remove in.nextLine()

right before the declaration ascending

and descending

.

By the way, your test user input code is a little more complicated than it needs to be. To install correctly ascending

, all you need is:

ascending = in.nextLine().equalsIgnoreCase("y");

      



It uses equalsIgnoreCase()

, which is the equivalent of your method to get the first character, superscript and compare it to 'Y'

.

This will replace the if / elseif block before the for loop.

Side note # 2: you have a lot of duplicate code for the loop. Since the only thing that changes is the condition in the if block, you can simply use a boolean expression instead of the inner if / else statement:

    for (int i = 0; i < count; i++) {
        for (int j = 0; j < count - 1; j++) {
            boolean swap = ascending
                    ? array[j] > array[j + 1]
                    : array[j] < array[j + 1];
            if (swap) {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }

      

0


source







All Articles