Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a txt file with 4 by 3 rows and columns of numbers but there is some empty lines (space) in some places, and I am supposed to print the average of each row and column (which I already did) but when I come across an empty line I have to print "empty line" instead.

example:
grid txt file:
40 33 23

20 12 89

22 15 17
78 36 76

output:
average:
200
empty line
222
empty line
120
390

What I have tried:

for (int i = 1; i <= rows; i++){
               for (int j = 1; j <= col; j++){

                   String line = scan.nextLine();
                   while (!line.equals(null)){
                       if (line.equals(" ")){
                           System.out.println("empty line");
                       }

                       double value = scan.nextDouble();

                   switch (c){
                       case 1:
                           col1+=value;
                           break;
                       case 2:
                           col2+=value;
                           break;

                       case 3:
                           col3+=value;
                           break;
                   }
                   switch(r){
                       case 1:
                           row1 += value;
                           break;
                       case 2:
                           row2 += value;
                           break;
                       case 3:
                           row3 += value;
                           break;
                       case 4:
                           row4 += value;
                           break;
                       case 5:
                           row5 += value;
                           break;
                       case 6:
                           row6 += value;
                           break;
                       case 7:
                           row7+= value;
                           break;

                   }


                   }// while
               }

           }
Posted
Updated 24-Jan-22 16:00pm
Comments
Richard MacCutchan 25-Jan-22 3:49am    
An empty line may consist of nothing but a newline character. You should check the length of the actual text read in to see if it is zero.

1 solution

"1"  is a string containing a single digit
" "  is a string containing a single space
""   is an empty string
 
Share this answer
 
Comments
Member 15511449 24-Jan-22 22:10pm    
I tried "" but still won't work
Luc Pattyn 25-Jan-22 1:01am    
there are two cases: one with numbers, one with empty line.
don't try to find numbers once you decided the line is empty!
use if - else
Member 15511449 25-Jan-22 1:26am    
as in do an else for the rest of the code after the if statement?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900