Dillon
Dillon

Reputation: 11

Java - Integer In Array Coming Up As Not Found When There Are Multiple Instances

I've written a script that creates an array and fills it with random numbers. The user inputs a number and it shows the number and its index. Some numbers are duplicates and all must be found. However, if the user enters a number that does not exist they need to be told so. My problem is that most of the time (not all the time) if the number exists it lists it like it should, but also says that it's not found. I believe what is happening is that the last loop through it doesn't find any subsequent instances of the number so it also thinks that it doesn't exist. How do I get it to only say that the number is not found if it is truly not there? Thank you for any and all help!

        int[] $myUnsorted = new int[10]; // Define a new array and fill it with ten elements

    for(int x = 0; x < $myUnsorted.length; x++) { // Begin For Loop
        $myUnsorted[x] = (int)(Math.random() * 20 + 1); // Make the elements random numbers between 1 & 20
    } // End For Loop

    int[] $mySorted = new int[$myUnsorted.length]; // Define a new array and fill it with the same amount of elements as the first array

    for(int x = 0; x < $myUnsorted.length; x++) 
        $mySorted[x] = $myUnsorted[x]; // Copy the first array
        Arrays.sort($mySorted); // Sort the second array

    System.out.println("Unsorted Array \t \t \t Sorted Array"); // Print the labels with tabs separating them

    for(int x=0; x<$myUnsorted.length; x++) { // Begin For Loop
        System.out.printf("%d \t \t \t \t %d \n",$myUnsorted[x],$mySorted[x]); // Print the numbers with tabs separating them to fall under the respective labels
    } // End For Loop

    Scanner $myScan = new Scanner(System.in); // Load the Java Scanner Class

    System.out.print("\nPlease enter number to search for: "); // Print the instructional text

    int $mySearch = $myScan.nextInt(); // Define a new variable for the search

    for(int x = 0; x < $myUnsorted.length; x++) { // Begin For Loop
        if($myUnsorted[x] == $mySearch) { // If a number matches the search then...
            System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the unsorted array"); // ...print the search results
        } // End If Statement
        else if(x == $myUnsorted.length - 1) { // If a number does NOT match the search then...
            System.out.println("Search Value: " + $mySearch + " was not found"); // ...print the text
        } // End ElseIf Statement
    } // End For Loop

    for(int x = 0; x < $mySorted.length; x++) { // Begin For Loop
        if($mySorted[x] == $mySearch) { // If a number matches the search then...
            System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the sorted array"); // ...print the search results
        } // End If Statement
    } // End For Loop

Upvotes: 1

Views: 44

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51910

Create a local Boolean variable to keep track if something is found

boolean isFound = false;
for(int x = 0; x < $myUnsorted.length; x++) { 
  if($myUnsorted[x] == $mySearch) { 
     System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the unsorted array");
     isFound = true;
   } 
} 

if (!isFound) {
  System.out.println("Search Value: " + $mySearch + " was not found"); 
} else {
  for(int x = 0; x < $mySorted.length; x++) {
    if($mySorted[x] == $mySearch) { 
        System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the sorted array"); // ...print the search results
    }
} 

Upvotes: 1

shmosel
shmosel

Reputation: 50716

Use a variable to track if anything's been found yet:

boolean found = false;
for(int x = 0; x < $myUnsorted.length; x++) {
    if($myUnsorted[x] == $mySearch) {
        found = true;
        System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the unsorted array");
    }
}
if(!found) {
    System.out.println("Search Value: " + $mySearch + " was not found");
}

Upvotes: 2

Related Questions