-2

I have a .txt or a .xlsx with a lot of rows and 4 or 5 columns. I would like to write a java program that deletes all the rows that have 5 columns.

How would I read the data and delete the rows based on the number of columns?

Delete row which has more than X columns in a csv I found this question here but it is not in java and i don't know how to use awk

  • 1
    Have try to look some stuff like : how to read file in Java, how to write to file in Java ? No one is going to build the whole code for you – azro May 18 '21 at 16:57

1 Answers1

0

First, you need to read the file line by line and create StringBuilder to store the valid lines

when you read line by line and check if this line has 5 columns or not, if it not add it to the StringBuilder then in the end you can write it on file

To check if this line contains 5 columns or not

If your line look like this a,b,c,d,e then the 5 columns line will contain 4 , and there are many ways to check if it contains 4 , or not for example one way is to replace it with empty char and check the line length before and after

int numberOfComma = line.length() - line.replaceAll(",", "").length();

or use a loop and count the number of , in the line

AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30