0

The input will be a text file with an arbitrary amount of integers from 0-9 with NO spaces. How do I populate an array with these integers so I can sort them later?

What I have so far is as follows:

BufferedReader numInput = null;
    int[] theList;
    try {
        numInput = new BufferedReader(new FileReader(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
        e.printStackTrace();
    }
    int i = 0;
    while(numInput.ready()){
        theList[i] = numInput.read();
        i++;

Obviously theList isn't initialized, but I don't know what the length will be. Also I'm not too sure about how to do this in general. Thanks for any help I receive.

To clarify the input, it will look like: 1236654987432165498732165498756484654651321 I won't know the length, and I only want the single integer characters, not multiple. So 0-9, not 0-10 like I accidentally said earlier.

1
  • My bad--meant to say 0-9, not -10. Commented Apr 1, 2013 at 18:56

3 Answers 3

2

Going for Collection API i.e. ArrayList

ArrayList a=new Arraylist();
while(numInput.ready()){
       a.add(numInput.read());
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is only part of the problem. There's also the issue of the return value of BufferedReader.read().
So now I'm populating an array list. All is good in that department, but I'm read input as integers of two characters long. i.e. i'm getting numInput.get(0) to return 49 when it should equal 4
You probably have to subtract '0' from the value returned by read. Like a.add(numInput.read() - '0');
Thanks David! That worked and I don't know why; I wouldn't mind an explanation if you're up for one.
It's a problem of ASCII encoding. Text in a text file is stored as numeric values and the character '0' is not stored with value 0. It is stored with value 48. '0' is a way to say "the ASCII value for character 0" (sort of).
|
0

You could use a List<Integer> instead of a int[]. Using a List<Integer>, you can add items as desired, the List will grow along. If you are done, you can use the toArray(int[]) method to transform the List into an int[].

Comments

0

1 . Use guava to nicely read file's 1st line into 1 String

readFirstLine

2 . convert that String to char array - because all of your numbers are one digit lengh, so they are in fact chars

3 . convert chars to integers.

4 . add them to list.

public static void main(String[] args) {

    String s = "1236654987432165498732165498756484654651321";
    char[] charArray = s.toCharArray();
    List<Integer> numbers = new ArrayList<Integer>(charArray.length);
    for (char c : charArray) {
        Integer integer = Integer.parseInt(String.valueOf(c));
        numbers.add(integer);
    }

    System.out.println(numbers);
}

prints: [1, 2, 3, 6, 6, 5, 4, 9, 8, 7, 4, 3, 2, 1, 6, 5, 4, 9, 8, 7, 3, 2, 1, 6, 5, 4, 9, 8, 7, 5, 6, 4, 8, 4, 6, 5, 4, 6, 5, 1, 3, 2, 1]

Comments

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.