0

Well i tried to make a loop within a loop, kinda like that movie Inception. But it messes up some how, the second loop won't stop when i enter "N" for no, and won't go back to the the 1st loop, which is "Continue with another set?", when i enter "Y" for yes. Instead it keeps going back to the "What value to check?" no matter what :(

while (true){
    // 2. Ask the user for size of arbitrary numbers.
    System.out.print("Please enter a size for arbitray numbers: ");
    int size = indata.nextInt(); 
    int [] SortedNumbers = new int [size];

    // 3. Process arbitrary numbers and remove all duplicates
    int numDuplicates = generate_data(SortedNumbers);

    // 4. Print the numbers and number of duplicates
    printArray(SortedNumbers, numDuplicates);

    while (true){
        // 5. Ask user for target value
        System.out.print("\nWhat target value to check? ");
        int target = indata.nextInt();

        // 6. Perform linear and binary search
        int linearVal = linear_search(SortedNumbers,target);
        int binaryVal = binary_search(SortedNumbers,target);
        if (linearVal == -1)
            System.out.println("Linear search: " + target + " does not exist in the array.");
        else 
            System.out.println("Linear search: " +target + " is in location " + linearVal);

        if (binaryVal == -1)
            System.out.println("Binary search: " + target + " does not exist in the array.");
        else 
            System.out.println("Binary search: " +target + " is in location " + binaryVal);

        // 7. Repeat steps 5 and 6 until the user is satisfied.
        System.out.println("Continue? Y/N");
        String answer = indata.next();
        if (answer=="Y"){
            break;
        }
    }
    // 8. Repeat steps 2-7 until the user is done working with different arbitrary sets of numbers.
    System.out.println("Continue with another set? Y/N");
    String answer2 = indata.next();
    if (answer2=="Y"){
        break;
    }
}

So technically i need it to stop when i enter N, or ask me if i want "a new set" when i enter Y, and then on the second loop go back to "value to check" on N, or start from the beginning for Y.

Khang Le
  • 3
  • 4

0 Answers0