0

I have like these lines in a file:

101 xxx 3 yyy
102 aaa 2 bbb
103 ppp 3 qqq
104 iii 5 jjj

I want to put each elements of each line in an array, for example

int[] par1 = {101,102,103,104}
String[] par2 = {"xxx", "aaa", "ppp", "iii"}
int[] par3 = {3,2,3,5}
String[] par4 = {"yyy", "bbb", "qqq", "jjj"}
Ehsan
  • 2,273
  • 8
  • 36
  • 70
  • Your arrays represent columns not rows or am I mistaken? – Sirko May 10 '13 at 12:56
  • you forgot pasting your codes . . .. .? – Kent May 10 '13 at 12:56
  • @Sirko yes you're right – Ehsan May 10 '13 at 12:58
  • @Aquillo I have no idea – Ehsan May 10 '13 at 12:59
  • 1
    [Scanner](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) (next / nextInt) with [ArrayList](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) (add) (instead of arrays) is probably the easiest way (or [LinkedList](http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) for improved efficiency). – Bernhard Barker May 10 '13 at 13:00

2 Answers2

2

Read lines with Guava / Apache Commons and so on:

Results in:

List<String> lines;

Next (using lines):

List<Integer> par1 = new ArrayList();
List<String> par2 = new ArrayList();
List<Integer> par3 = new ArrayList();
List<String> par4 = new ArrayList();

try {
    for(String line : lines) {
        String[] elements = line.split(" ");
        if(elements.length == 4) {
            par1.add(Integer.valueOf(element[0])); 
            par2.add(element[1]));
            par3.add(Integer.valueOf(element[2]));
            par4.add(element[3]));
        }
        else {
            throw new Exception("Conversion failed");
        }
    }
}
catch(NumberFormatException ex) {
    throw new Exception("Conversion failed");
} 

// Convert ArrayList to Array if you need to.
Community
  • 1
  • 1
Menno
  • 12,175
  • 14
  • 56
  • 88
  • Reading all the lines into a `List`, really? That's not at all (space) efficient. – Bernhard Barker May 10 '13 at 13:06
  • Thank you, Be sure that I search it for a hour. – Ehsan May 10 '13 at 13:07
  • @Dukeling: It'll probably the easiest way to accomplish this by iterating over the lines. In case you have a better solution, please share :) – Menno May 10 '13 at 13:09
  • @Aquillo As per my comment to the question, use [Scanner](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) (next / nextInt). Saying much more than that would be too much doing-someone-else'-homework for my liking. – Bernhard Barker May 10 '13 at 13:16
1

Something like this should work (didnt test)

    List<String> lines = Files.readAllLines(Paths.get("1.txt"), StandardCharsets.UTF_8);
    int[] par1 = new int[lines.size()];
    String[] par2 = new String[lines.size()];
    int[] par3 = new int[lines.size()];
    String[] par4 = new String[lines.size()];
    for(int i = 0; i < lines.size(); i++) {
        String[] a = lines.get(i).split(" +");
        par1[i] = Integer.parseInt(a[0]);
        par2[i] = a[1];
        par3[i] = Integer.parseInt(a[2]);
        par4[i] = a[4];
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275