DESCRIPTION:
You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array.
INPUT:
All input has been handled for you:
PROCESSING:
OUTPUT:
Sample input/output:
Input | Output | |||||||||
3
|
There were a total of 4 full cells |
Given code :
import java.util.Scanner;
public class CellFinder {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int rows = in.nextInt();
int cols = in.nextInt();
in.nextLine();
String item;
String[][] matrix = new String[rows][cols];
for (int i = 0; i<rows ; i++) {
matrix[i] = in.nextLine().split(" ");
}
//************************Your code starts here****************************************************************************************
}
}
please answer question use JAVA!
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int rows = in.nextInt();
int cols = in.nextInt();
int c=0;
in.nextLine();
String item;
String[][] matrix = new String[rows][cols];
for (int i = 0; i<rows ; i++)
{
matrix[i] = in.nextLine().split(" ");
}
//************************Your code starts here****************************************************************************************
for (int i = 0; i<rows ; i++)
{
for (int j = 0; j<cols ; j++)
{
if(matrix[i][j].equals("empty")) //checks if the element in ith row and jth column is 'empty'
{
continue; //if empty then dont count
}
else
{
c++; //if it is not empty then increase the value
}
}
}
System.out.println("\nThere were a total of " + c + " full cell(s)");
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.