1

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 :)

0

3 Answers 3

2

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();
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @AlpeshJikadra it works with nextLine()
1

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();
        }

    }
}

1 Comment

Do not add code only describe what you have changed and why
0

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.

1 Comment

Thank you @AlexeyGavluk. I am totally new in Java so every hints are helpful for me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.