NightcoreMaker
NightcoreMaker

Reputation: 19

Parsing a space delimited string of integers into a filtered array of intergers

I have read in a string with a list of numbers. I then have created an array with the length of 3. My question, is how do I get the numbers from the string into the array (It's okay if there are more or fewer than 3 numbers > 11).

I have tried going through the string sequentially and putting the values into the output array but that hasn't worked. I have also tried making the output array a String array but that also proved to be incorrect.

    public int[] findNums(String line)
{
    int[] array = new int[3];
    list = line;
    String[] parts = array.split(" ");
    int index = 0;
    for(int spot=0; spot<parts.length();spot++)
    {
        if(parts[spot] > 11)
        {
            array[index] = spot;
            index++;
        }
    }
    return array;
}

This is my method that I have so far as you can see I have a loop that goes through the string and the if-statement to check the value of the digit. There might be some syntax errors(sorry in advance) I'm new to arrays in Java. Here's an example of the DAT file i am bringing in.

-99 1 2 3 4 5 6 7 8 9 10 12345
10 9 8 7 6 5 4 3 2 1 -99
10 20 30 40 50 -11818 40 30 21 10 
32767
250 19 17 15 13 11 10 9 6 3 2 1 -455
255 255

the output of the new array should be

[12345,0,0]

Since there is only one number greater than 11.

This is the output for the first line but it is the same format for all lines within the DAT file.

Can someone tell my how i should fix my method?

Upvotes: 0

Views: 121

Answers (2)

Jesse Ivy
Jesse Ivy

Reputation: 335

I need to confirm if your goal is to pull all the numbers which have a value greater than 11 from the list passed in? You seem to be confusing your index for the value. If what you really want is all entries 11 or greater try this. You aren't instantiating the list variable correctly, if it's a comma separated list you'll need to use the split function as mentioned by the commenter above.

public ArrayList<Integer> findNums(String line)
{
    ArrayList<Integer> twelvePlus = new ArrayList<Integer>();
    String[] numbers = line.split(" ");
    for(int index=0; index<numbers.length;index++)
    {
        if(Integer.valueOf(numbers[index]) > 11)
        {
            twelvePlus.add(new Integer(Integer.valueOf(numbers[index])));
            index++;
        }
    }
    return twelvePlus;
}

...it returns a variable number of numbers, which represent all numbers greater than eleven from the list you passed in. If you want to enforce an upper limit you can add a break statement inside the condition based on the size of the array list. Do you really want the zeros? Does it have to be an integer array return value? My example illustrates the use of generics. Also, guessing you never hit the condition because you havea type mismatch error...

Upvotes: 0

J. Lengel
J. Lengel

Reputation: 588

Here's a nice solution:

public int[] getNumsFromString (String text) {
    String[] individNums = text.split(" ");
    int[] nums = new int[3];

    int counter = 0;

    for (int i = 0; i < individNums.length; i++) {
        int currentValue = Integer.parseInt(individNums[i]);
        if (currentValue > 11 && counter < 3) {
            nums[counter++] = currentValue;
        }
    }
    return nums;
}

Hope that helps!

Upvotes: 2

Related Questions