1

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
6
  • 5
    The $'s are frying my brain. Commented Oct 19, 2018 at 18:16
  • Also your naming convention is very strange. Why is every variable prefixed with my? Commented Oct 19, 2018 at 18:27
  • Ah, sorry guys. I'm a PowerShell guy first and foremost so that's where the '$' is coming from. It helps me to see that those are variables/arrays since I'm new to Java. As far as the "my" goes, that's also just a naming thing I have for variables/arrays. Is it bad to do it this way in Java? Commented Oct 19, 2018 at 18:31
  • It's very unconventional. Avoid it if you expect anyone else to read your code. Commented Oct 19, 2018 at 18:31
  • Look at your logic for printing "not found". You're printing this on the last loop through, if the number did not match the last number in the list. If the user enters the last number in the list, it would not print that. Wrong way to test; the right way is given as an answer. Commented Oct 19, 2018 at 18:34

2 Answers 2

2

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");
}
Sign up to request clarification or add additional context in comments.

Comments

1

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
    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.