简体   繁体   中英

string splitted by comma in java csv file

It seems to be very easy problem to solve but I've stuck on it. Anybody can help me and explain where I do a mistake ? I read a csv file where I have:

Time,Activ,Switch,A,B,C,D,E
5:58,on,,,,,,
6:00,on 123,,,ab cd,,,
6:02,on - off,1->2,6,ab cd,1,,
6:04,on,,,cd ab,1,,
6:06,off,,,,,,
7:22,on / off,,,,,,
7:30,on / of,3->4,15,,,,
7:34,home on,,,ab cd,,,

I would like to separate data in a row only by the commas and save into arrays that will look like (first row omitted):

1---> ["5:58","on", "", "", "", "", "", ""]
2---> ["6:00", "on 123", "", "", "ab cd", "", "", ""]
3---> ["6:02", "on - off", "1->2", "6", "ab cd", "1", "", ""]
6---> ["7:22", "on / off", "3->4", "15", "", "", "", ""]

and so on... but I receive arrays with splitted string also by space and other characters

1---> ["5:58","on", "", "", "", "", "", ""]
2---> ["6:00", "on", "123", "", "", "ab", "cd", "", "", ""]
3---> ["6:02", "on", "-", "off", "1->2", "6", "ab", "cd", "1", "", ""]
6---> ["7:22", "on", "/", "off", "3->4", "15", "", "", "", ""]

Here is my code:

public class Test {
    public static void main(String []args) {

    String fileName = "TestData.csv";
    File file = new File(fileName);

    try{
        Scanner inputStream = new Scanner(file);
        String firstLine = inputStream.next();
            while(inputStream.hasNext()){
                String dataRow = inputStream.next();

                String[] valuesInDataRow = dataRow.split(",",-1);

                for(int i=0;i<valuesInDataRow.length;i++){
                    System.out.println("---> " + valuesInDataRow[i]);
                }

            }
            inputStream.close();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

I would be very grateful for any explanations :)

You should use inputStream.nextLine() instead of inputStream.next()

Try something like

 public static void main(String[] args) {
        String fileName = "TestData.csv";
        File file = new File(fileName);

        try{
            Scanner inputStream = new Scanner(file);
            String firstLine = inputStream.nextLine();
            while(inputStream.hasNext()){
                String dataRow = inputStream.nextLine();

                String[] valuesInDataRow = dataRow.split(",",-1);

                for(int i=0;i<valuesInDataRow.length;i++){
                    System.out.println("---> " + valuesInDataRow[i]);
                }

            }
            inputStream.close();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
    }

Here, I used Scanner.nextLine() and regx \\\\s*,\\\\s* to remove unwanted white space.

public class Test {
    public static void main(String []args) {

        String fileName = "TestData.csv";
        File file = new File(fileName);

        try{
            Scanner inputStream = new Scanner(file);
            String firstLine = inputStream.nextLine();
            while(inputStream.hasNext()){
                String dataRow = inputStream.nextLine();

                String[] valuesInDataRow = dataRow.split("\\s*,\\s*", -1);

                for(int i=0;i<valuesInDataRow.length;i++){
                    System.out.println("---> " + valuesInDataRow[i]);
                }

            }
            inputStream.close();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

And just as additional tip, I think, it will be good recommendation to use Java 1.7 feature "try-for-resource"

https://www.journaldev.com/592/java-try-with-resources

Instead of:

try{
   Scanner inputStream = new Scanner(file);
   ...
   inputStream.close();
}
catch(FileNotFoundException e) {
  ...
}

Always better to use:

try(Scanner inputStream = new Scanner(file)) {
...
}
catch (IOException e) {
...
}

Especially if file is suppose to be big. However works from Java 1.7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM