简体   繁体   中英

Lonely Integer - Output is correct. But still getting additional message

Im trying to find the lonely integer in an array. My output is correct, but still getting the extra message. Please have a look at the code. I'm using Java to write the program.

Code:

import java.util.InputMismatchException;
import java.util.Scanner;

public class LonelyInteger {

    private static int inputArray[];
    private static int inputLength;
    private static final Scanner scanner = new Scanner(System.in);;

    public static void main(String[] args) {

        try {
            if (getInput()) {
                sortAndPrintArray();
                findLonelyInteger();
            } else {
                System.out.println("OOPS, something is not right! Try again!");
            }
        } catch (NumberFormatException | InputMismatchException nfime) {
            System.out.print("Number Format Exception or Input Mismatch Exception Occured: " + nfime);
        } catch (Exception e) {
            System.out.print("Exception Occured: " + e.getMessage());
        }

    }

    private static boolean getInput() throws NumberFormatException, InputMismatchException, Exception {

        System.out.print("Enter the array length: ");
        inputLength = scanner.nextInt();
        if (inputLength <= 0) {
            return false;
        }
        inputArray = new int[inputLength];
        System.out.println("Enter the array:");
        for (int i = 0; i < inputLength; i++) {
            inputArray[i] = scanner.nextInt();
        }
        return true;
    }

    private static void sortAndPrintArray() {
        sortArray();
        printSortedArray();
    }

    private static void sortArray() {
        int temp = 0;
        for (int i = 0; i < inputLength; i++) {
            for (int j = 0; j < i; j++) {
                if (inputArray[i] < inputArray[j]) {
                    temp = inputArray[i];
                    inputArray[i] = inputArray[j];
                    inputArray[j] = temp;
                }
            }
        }
    }

    private static void printSortedArray() {
        System.out.println("Sorted Array:");
        for (int i = 0; i < inputLength; i++) {
            System.out.print(inputArray[i] + " ");
        }
        System.out.println();
    }

    private static void findLonelyInteger() {
        boolean foundLonelyInteger = false;
        for (int i = 0; i < inputLength; i++) {
            if ((i+1) == inputLength) {
                System.out.println("Lonely Integer: " + inputArray[i]);
                break;
            }
            if (inputArray[i] == inputArray[++i]) {
                continue;
            } else {
                System.out.println("Lonely Integer: " + inputArray[i-1]);
                foundLonelyInteger = true;
                i--;
            }
        }
        if (!foundLonelyInteger) {
            System.out.println("Lonely integer not available!");
        }
    }

}

Here is my output, which is seen in Command Prompt:

Output:

Enter the array length: 5
Enter the array:
1
2
2
1
2
Sorted Array:
1 1 2 2 2 
Lonely Integer: 2
Lonely integer not available!

You did not set the flag, in your findLonelyInteger() method's first if condition!

if ((i+1) == inputLength) {
    System.out.println("Lonely Integer: " + inputArray[i]);
    foundLonelyInteger = true;  // --> HERE
    break;
}

Command Prompt? Start using Eclipse! And learn debugging!

Set your foundLonelyInteger = true; while you are checking for if((i+1) == inputLength)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM