Question

Depending on how many values are found, the program is supposed to react in a certain way.

import javax.swing.*;
import java.util.*;


public class labActivityArrays{

public static void main(String[] args){

//1

//ask for number of rows
String rowS;
rowS = JOptionPane.showInputDialog("Number of rows: ");
int row =  Integer.parseInt(rowS);

//ask for number of columns
String columnS;
columnS = JOptionPane.showInputDialog("Number of columns: ");
int column = Integer.parseInt(columnS);

//ask for initial value
String initialS;
initialS = JOptionPane.showInputDialog("Initial value: ");
int initial =  Integer.parseInt(initialS);

//define the new array
int[][] arr = new int [row][column];

int temp = initial; 

for(int i=0; i < row; i++){
  for(int j=0; j < column; j++){
     arr[i][j] = temp;
     temp += 1;
  }
}
//print the array
for(int i=0; i < row; i++){
  for(int j=0; j < column; j++){
     System.out.print(arr[i][j] + " ");
  }
  System.out.println();
}

//2

//ask for the first value to check for
String check1S;
check1S = JOptionPane.showInputDialog("First value to check for: ");
int check1 =  Integer.parseInt(check1S);

//ask for the second value to check for
String check2S;
check2S = JOptionPane.showInputDialog("Second value to check for: ");
int check2 =  Integer.parseInt(check2S);

//check for the first value  
boolean found = false;
int n = -1;
int o = -1;

while(!found && ++n < row){
  o = -1;
  while(!found && ++o < column){
     found = (arr[n][o] == check1);
  }
}

//print whether the value is present or not
if(found){
  System.out.println("Found " + check1 + " at index [" + n + "][" + o + "].");
}
else{
  System.out.println(check1 + " is not in this array.");
}

//check for the second value
boolean found2 = false; 
int m = -1; 
int p = -1;

while(!found2 && ++m < row){
  p = -1;
  while(!found2 && ++p < column){
     found2 = (arr[m][p] == check2);
  }
} 
//print whether the value is present or not
if(found2){
  System.out.println("Found " + check2 + " at index [" + m + "][" + p + "].");
}
else{
  System.out.println(check2 + " is not in this array.");
}

The part in question is the following code. Except the found boolean value doesn't seem to ever be right, so it doesn't respond properly.

//3

//if only one value was found
if((found == true && found2 == false) || (found == false && found2 == true)){
  for(int i=0; i < row; i++){
     for(int j=0; j < column; j++){
        System.out.print(arr[i][j] + " ");
     }
     System.out.println();
  }
}

//if neither value was found
if(!(found == false && found2 == false)){
  String askAgain;
  askAgain = JOptionPane.showInputDialog("Since no user values were found, would you like to search for 2 more values? (yes/no) ");
     if(!(askAgain.equals("yes") || askAgain.equals("no"))){
        askAgain = JOptionPane.showInputDialog("Since no user values were found, would you like to search for 2 more values? (yes/no) ");
     }
  if(askAgain.equals("yes")){
     //ask for the first value to check for
     check1S = JOptionPane.showInputDialog("First value to check for: ");
     check1 =  Integer.parseInt(check1S);

     //ask for the second value to check for
     check2S = JOptionPane.showInputDialog("Second value to check for: ");
     check2 =  Integer.parseInt(check2S);

     found = false;
     n = -1;
     o = -1;

     //check for the first value  
     while(!found && ++n < row){
        o = -1;
        while(!found && ++o < column){
           found = (arr[n][o] == check1);
        }
     }     

     //print whether the value is present or not
     if(found){
        System.out.println("Found " + check1 + " at index [" + n + "][" + o + "].");
     }
     else{
        System.out.println(check1 + " is not in this array.");
     }

     //check for the second value
     found2 = false; 
     n = -1; 
     o = -1;

     while(!found2 && ++n < row){
        o = -1;
        while(!found2 && ++o < column){
           found2 = (arr[n][o] == check2);
        }
     }
     //print whether the value is present or not
     if(found2){
        System.out.println("Found " + check2 + " at index [" + n + "][" + o + "].");
     }
     else{
        System.out.println(check2 + " is not in this array.");
     }
  }
}

//if both values were found
if(found == false && found2 == false){
  String swap;
  swap = JOptionPane.showInputDialog("Would you like the two values to be swapped? (yes/no)");
     if(!(swap.equals("yes") || swap.equals("no"))){
        swap = JOptionPane.showInputDialog("Would you like the two values to be swapped? (yes/no)");
     }
  if(swap.equals("yes")){
     int one = arr[n][o];
     int two = arr[m][p];
     arr[n][o] = two;
     arr[m][p] = one;

     for(int i=0; i < row; i++){
        for(int j=0; j < column; j++){
           System.out.print(arr[i][j] + " ");
        }
        System.out.println();
     }
  }
}






}
}
Was it helpful?

Solution

you have checked wrongly, For second if case with found: The following condition also appears to be contradictory with your comment "if neither value was found"

//if neither value was found
if(!(found == false && found2 == false))

You should remove the ! from the checking. As both of found and found2 is false, (found == false && found2 == false) will result in true and !(true) will result in false.

Again with third if checking, as it appears with your own comment:

//if both values were found
if(found == false && found2 == false)

you might want it as:

//if both values were found
if(found == true && found2 == true)

However, With the help of bitwise operators(^, &, |), i would simply write the three if condition as follows:

 if(found ^ found2 == true) // at least one of them is true: found
 {
   // do blah blah 
 }
 else if(found & found2 == true) // both of them is true: found
 {
     // do blah blah
 }
 else // if above two is false this one obviously true: none of them found
 {
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top