1

I have a txt file like this:

5
1
3
6
9

I want to read them using java and store all of the numbers into a array.How do I do that? read them in as string and convert to arrray? (how to convert?) the file only contain ints.

I tried read them into a string and use this to convert

 static public int[] strToA(String str)
{
    int len = str.length();
    int[] array = new int[len];
    for (int i = 0; i < len; i++)
    {
        array[i] = Integer.parseInt(str.substring(i,i+1));
    }
    return array;
}
6
  • Store them seperately like [1,2,3,4,5] or as one int? Commented Mar 16, 2012 at 6:48
  • seperately into a int[] array Commented Mar 16, 2012 at 6:49
  • 2
    While I don't think this question qualifies as against rules for stackoverflow, it's generally bad form to ask for the complete solution to a problem without having tried things (especially one that looks like a complete homework question). Commented Mar 16, 2012 at 6:51
  • i posted the code I already got. I was trying to post it but it didn't work. Commented Mar 16, 2012 at 6:53
  • I assume you are new to SO. +1'd for your effort to edit your question. Commented Mar 16, 2012 at 7:04

3 Answers 3

5

Scanner can help you read your file, and you can use a List or something else to store the info.

After that, you can use this List to convert your Array.

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

1 Comment

+1: Using List is better when you are not sure of the array size. After coming out of the loop use ArrayList.toArray() to put the contents into an array. You might as well retain the ArrayList instead of creating a new int array
2
public static Integer[] read2array(String filePath) throws IOException {
    List<Integer> result = new ArrayList<Integer>();
    RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "r");
    String line = null;
    while(null != (line = randomAccessFile.readLine())) {
        result.add(new Integer(line));
    }
    randomAccessFile.close();
    return result.toArray(new Integer[result.size()]);
}

Comments

1

Code would be something like this. This is untested code and may have Syntax errors.

Path file = "yourfile";
// open file
try (InputStream in = Files.newInputStream(file);
    BufferedReader reader =
      new BufferedReader(new InputStreamReader(in))) {
    String line = null;
    intArr = new int[10]; // bad code could fail if file has more than 10
    int i = 0;
    while ((line = reader.readLine()) != null) {
        intArr[i++] = Integer.parseInt(line); // parse String to int
    }
} catch (IOException x) {
    System.err.println(x);
}

To use List instead of array change line

intArr = new int[10];

to

List intArr = new ArrayList();

Code would be something like

    List intArr = new ArrayList();
    while ((line = reader.readLine()) != null) {
        intArr.add(Integer.parseInt(line)); // parse String to int
    }

2 Comments

Thanks. But I have more than one file to use. And most of them are bigger than 10. Do I just set a very big number then?
See updated answer. For ease of coding you can use List instead of array for variable length. If your requirement is to return array type you can always cast List to array javadoc

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.