Is there a statement to check if a row of a 2D array contains an empty element?
This is related to homework. The restrictions are no libraries or lists or hashmaps or anything. I have a 2D string array, for instance {{"a","a","a"},{"b","","b"},{"c","c","c"}}. If I print it without checking anything, and some formatting I would get
a
a
a
b
b
c
c
c
I'm supposed to check whether the whole row has something empty and skip it if it is, so the correct output would be
a
a
a
c
c
c
I can check a single string if(data[i][j] != null && !data[i][j].isEmpty())
, but is there a statement for rows?
The exception stuff is something else
public class Test {
public static void main(String[] args) {
String[][] data = {{"a","a","a"},{"b","","b"},{"c","c","c"}};
for (int i=0;i<data.length;i ){
System.out.println(" ");
try{
if(/*row data[i] has no empty elements*/)
for (int j=0;j<data[i].length;j ){
System.out.println(data[i][j]);
}
else
throw new EmptySpotException("Row(" i "is missing elements, skipping line");
} catch(EmptySpotException e){
System.out.println(e.getMessage());
}
}
}
}
CodePudding user response:
Need to check the full row for empty before printing.(or only blanks)
In order to do in one loop, row_output_string is constructed step by step and is print only is condition is false (skip).
public static void main(String[] args)
{
String[][] data = {{"a","a","a"},{"b","","b"},{"c","c","c"}};
for (int i=0;i<data.length;i ){
boolean skip=false;
//only to iterate once
StringBuffer sb= new StringBuffer();
try{
//check for empty
for (int j=0;j<data[i].length;j ){
if(data[i][j].isBlank())
{
skip=true;
throw new Exception("Row[" i "]is missing elements, skipping line");
}
sb.append((j!=data[i].length-1) ? data[i][j] "\n" :data[i][j]);
}
}
catch(Exception e){
//System.out.println("to be comment" e.getMessage());
};
if(!skip)
{
System.out.println(" ");
System.out.println(sb);
}
}
}
Note: Considering the termination of try-block
when exception arise an enhancement could be:
public static void main(String[] args)
{
String[][] data = {{"a","a","a"},{"b","","b"},{"c","c","c"}};
for (int i=0;i<data.length;i ){
//only to iterate once
StringBuffer sb= new StringBuffer();
try{
//check for empty
for (int j=0;j<data[i].length;j ){
if(data[i][j].isBlank())
{
sb=null;
throw new Exception("Row[" i "]is missing elements, skipping line");
}
sb.append((j!=data[i].length-1) ? data[i][j] "\n" :data[i][j]);
}
}
catch(Exception e){
System.out.println("to be comment" e.getMessage());
};
if(sb!=null)
{
System.out.println(" ");
System.out.println(sb);
}
}
}
CodePudding user response:
Your expected output shows no output from an exception so I am excluding exception handling. You can add that back in where you deem appropriate.
I would recommend using a labelled loop which is a normal part of Java and should meet your requirements.
- simply iterate thru the array of arrays as shown.
- then check each single array for an empty string.
- if the string is empty, continue to the next array via the outer loop
- otherwise print the appropriate output.
String[][] data = {{"a","a","a"},{"b","","b"},{"c","c","c"}};
next:
for(String[] arr : data) {
for(String v : arr) {
if (v.isEmpty()) {
// empty element found so skip this array
continue next;
}
}
System.out.println(" ");
for (String v : arr) {
System.out.println(v);
}
}
prints
a
a
a
c
c
c
Note: Just like iterating over an array of SomeType
from a single array (e.g. for (SomeType s : arrayOfSomeType)
You can also iterate over an array of array of SomeType the same way (for(SomeType[] arr : arrayOfArrayOfSomeType)
. This can greatly simplify processing "2D"
arrays as demonstrated above.