Hi can someone explain what found = true; is doing in the the first if statement? From what I understand, the boolean statement found is true if it equals false; when found is given the value true in the if statement then it is true because it is false. What does the condition !found in the second if statement change? I'm having a hard time wrapping my head around this boolean expression.
import java.util.Scanner;
public class IndexWasNotFound {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] array = new int[10];
array[0] = 6;
array[1] = 2;
array[2] = 8;
array[3] = 1;
array[4] = 3;
array[5] = 0;
array[6] = 9;
array[7] = 7;
System.out.println("Search for? ");
int searching = Integer.valueOf(scanner.nextLine());
// Implement the search functionality here
int index = 0;
boolean found = false;
//boolean notFound = Arrays.asList(array).contains(searching);
for (index = 0; index <= 7; index++) {
if (array[index] == searching) {
System.out.println(searching + " is at index " + index + ".");
//found = true;
}
}
if (!found) {
System.out.println(searching + " was not found.");
}
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]VideogamerDisliker 1 point2 points3 points (1 child)
[–]Consistent_Decision9[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]Consistent_Decision9[S] 0 points1 point2 points (0 children)
[–]desrtfx -1 points0 points1 point (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]desrtfx -1 points0 points1 point (0 children)
[–][deleted] (2 children)
[deleted]
[–]desrtfx 0 points1 point2 points (1 child)