0

i have a text file contains this:

1 1 0 0
1 2 0 0
1 0 1 0
1 0 2 0
1 0 0 1
1 0 0 2
2 1 0 0
2 2 0 0
2 0 1 0
2 0 2 0
2 0 0 1
2 0 0 2

then, i put the contents in an arraylist. the result will be same as in file. next, i want to process the data 1 by 1 and put each row of the content in an array[][] where the the data will be seperated by row. the result will be like this:

output[0][]={1 1 0 0}
output[1][]={1 2 0 0}
output[2][]={1 0 1 0}
output[3][]={1 0 2 0}
....
....
....

question, how can i take the string in arraylist to become a separated data? i code in java

thanks

1
  • 2
    The String#split() method should do exactly what you want. It will split your String into a String[]. Commented Jul 21, 2011 at 7:12

4 Answers 4

1

you can use " public String[] split(String regexp)" method to split string in to array by specifying character by which you split in argument. e.g.String temp = "1 2 3 4"; temp.split(" "); you will split by blank space in your case..

Sign up to request clarification or add additional context in comments.

Comments

1

As @Benoit already stated, you can split each line using String#split(regex), like this:

String line = ...;
String[] parts = line.split( "\\s+" ); //split on whitespace

Note that leading whitespace might result in empty strings at the beginning, i.e. " 1 2 3 4 " would result in {"", "1", "2", "3", "4"}. You might also use the Apache Commons Lang class StringUtils whose split(...) method takes care of that.

Also note the expression \s+ which would also split on multiple whitespacem, i.e. "1   2 3  4" would still result in {"1", "2", "3", "4"}.

You then might parse the individual parts as integers etc.

Comments

0

Here's some really simple code that does all the work in a couple of lines using Scanners, and handles any number of numbers per line.

EDITED: Note return type of List<List<Integer>> chosen for sanity over int[][]

public static List<List<Integer>> parseIntArrays(InputStream in) {
    Scanner s = new Scanner(in);
    List<List<Integer>> list = new ArrayList<List<Integer>>();
    while (s.hasNextLine()) {
        Scanner ns = new Scanner(s.nextLine());
        List<Integer> nums = new ArrayList<Integer>();
        list.add(nums);
        while (ns.hasNextInt())
            nums.add(ns.nextInt());
    }
    return list;
}

Here's some test code for you executing pleasure:

public static void main(String[] args) {
    String input = "1 0 0\n1 2 0 0\n1 0 1 0\n1 0 2 0 0 0 1\n1";
    List<List<Integer>> result = parseIntArrays(new ByteArrayInputStream(input.getBytes()));

    for (List<Integer> line : result)
        System.out.println(line);
}

Output:

[1, 0, 0]
[1, 2, 0, 0]
[1, 0, 1, 0]
[1, 0, 2, 0, 0, 0, 1]
[1]

2 Comments

boheimian,how about if the numbers inside the array are more than 4
Since you have shown interest, I'll code it to handle an arbitrary length list of numbers per line. See updated answer.
-1

Question is: Why are you using a 2 dimensional array to store the data? Parse the file by new line. For each new line, add that line to the array list. For each element in the array list, just add it to the array. I wrote a simple program. It assumes you've already parsed the file and have populated the ArrayList with new lines.

public static void main(String[] args) {
     List<String> list = new ArrayList<String>();
     list.add("1 0 1 1");
     list.add("1 1 1 1");
             // etc

     String[][] array = new String[list.size()][list.size()];
     int i = 0;
     for (String s : list) {
         stringArray[i++][0] = s;
     }

     for (int y = 0 ; y < array.length; y++) {
         System.out.println(stringArray[y][0]);
     }
}

2 Comments

1. Why is the second dimension of array list.size()? 2. Where is the split? The OP stated he needs the individual numbers in each line, not just the line itself.
1. is it the size of the row n column,this means both row and column have same size.i dont thinks its correct? 2. Split can i use list.split("")?can i declare the array earlier?i mean outside the main function?

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.