-1

i intend to read lines of numbers separated by comma's example below

08090417241, 080904152335, 080998766231, 080913456786, 08035467901, 08070415243, 080675534387, 080355525288, 080345887698, 0809966355343, ...

to the last number.

The original code I used in my program is this

 String file1 = FileArea6.getText(); 

This gives an error when I try to read the numbers with the above example. But when the numbers are arranged in the order below, it's read perfectly.

08035467901
08035474477
08024444448
08012233333
09033222333.......

What is the right way to read using the first example, i.e. separated by comma's?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Ribo01
  • 59
  • 1
  • 7

3 Answers3

2

All you need is str.split(',')

List<String> nums = Arrays.asList(str.split("\\s*,\\s*"));

or

List<String> nums = Arrays.asList(str.split(","));

or

String[] nums = str.split(",");
meteor
  • 2,518
  • 4
  • 38
  • 52
0

The following simple code snippet should work.

            try {
                BufferedReader bReader = new BufferedReader(new FileReader("input file"));
                String line = "";
                while ((line = bReader.readLine()) != null) {
                    String[] strArray = line.split(",");
                    for (String str : strArray) {
                        System.out.println( str.trim());

                        }
                    }
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
KDP
  • 1,481
  • 7
  • 13
0
String[] separateByComma(String s){
    String[] tmp = s.split(",");
    int n = 0, i=0;

    while(i<tmp.length){
        if(check(tmp[i])) tmp[n++] = tmp[i]; 
        i++;
    }       
    if(n==0) return null;

    String[] res = new String[n];

    while(--n >= 0) res[n] = tmp[n];
    return res;
}

/*
 * Your check function
 * You can filter anything
 */
boolean check(String s){
    if(s==null || s.length()<1) return false;
    return true;
}
Zephyr Guo
  • 1,083
  • 1
  • 8
  • 14