0

I have an array of strings. The user (hopefully) will fill it with {"word", "word", ...} and then some number of doubles/values after that. I need to take all those values after the first two words, parse them to doubles, and add them to a double array. For example, if the String array is {"add", "key", "2", "4", "5", "1"}, my new double array would return {1, 2, 4, 5, 1}. Here's a code snippet, let me know what you can make of it. For reference, getOption() is the method that returns the array of Strings. Thanks!

public double[] getValues(){
    double values[] = new double[getOption().length - 2]; //first two values cannot be parsed to double
    for (int i = 2; i < getOption().length; i++){
        double valueDouble;
        valueDouble = Double.parseDouble(getOption()[i].trim());
        values[i] = valueDouble;
    }
    return values;
}

I'm attempting to use the for loop to pull one value (getOption()[i]), parse it to a double, add it to the values array, and restart the loop until the end of the String array (getOption()) is reached.

edit: forgot to mention my issue: it returns nothing and throws an ArrayOutOfBoundsException 1 in a few seconds.

Qdhcjv
  • 45
  • 1
  • 6

3 Answers3

4

The first thing to notice is that:

values[i] = valueDouble;

Should be:

values[i - 2] = valueDouble;

As you've offset options by 2, but values does not reflect that.

Also, you should really consider storing the result of getOptions(). That might be why you're having an issue of it returning nothing since each one could be different. This would be the case where getOptions() requires user input or could change between calls. It is also just quicker and clearer.

String[] options = getOptions();
double values[] = new double[options.length - 2]; 
// ...
Obicere
  • 2,999
  • 3
  • 20
  • 31
  • I appreciate the advice. I added the values[i - 2] bit (great suggestion!) and created String[] options as you suggested. Still, when I call my switch case in my controller method that summons getValues() I get java.lang.ArrayIndexOutOfBoundsException: 1 still. It marks the line that calls getValues() as the issue. – Qdhcjv Mar 04 '16 at 22:02
  • @Qdhcjv `It marks the line that calls getValues() as the issue.` Is that line calling the method like `getValues()[1]`, or is the line inside the `getValues()` method? – Obicere Mar 04 '16 at 22:04
  • It's calling getValues() in a HashMap put statement. (dataMap.put(keyString, getValues())). keyString is the key for the input, getValues() is the array of values for that key. – Qdhcjv Mar 04 '16 at 22:09
  • @Qdhcjv testing the [corrected code appears to work fine](http://ideone.com/6sHWBd). The issue is probably coming from somewhere else. – Obicere Mar 04 '16 at 22:15
  • Huh... I'll keep digging through my code. Thank you! – Qdhcjv Mar 04 '16 at 22:19
  • I still can't figure it out. Want to help? My case calls System.out.println(Arrays.toString(getValues())); to ensure it's working and it prints nothing. If I force the option array in getValues to a certain value rather than calling getOption, it works, but it doesn't work with values given with getOption. Hope that makes sense... – Qdhcjv Mar 04 '16 at 22:41
  • @Qdhcjv try debugging and seeing what `getOption()` is returning. To me it seems like it isn't returning what you expect it to be. – Obicere Mar 04 '16 at 22:47
  • Seems normal... entering add dsn 2 4 5 to my switch statement returns [add, dsn, 2, 4, 5] like normal. Unless the [] mean something different from {}? – Qdhcjv Mar 04 '16 at 22:55
  • weird, I added a debug case for my switch statement and it does return the values [1, 2, 3, 4] if I enter the string "debug key 1 2 3 4" twice. – Qdhcjv Mar 04 '16 at 22:56
1

First, you should never say "The user wont do that", because you cant be sure of that.

Second, your values array is starting on index 2 and it only has 4 positions. When you're assigning it values you should substract 2 from it: values[i - 2] = valueDouble;

DCruz22
  • 806
  • 1
  • 9
  • 18
  • Thanks for the tip!! I know I can't trust the users to enter it properly- but for the sake of our assignment (this is high school level :/), we can expect it and throwing an error when the user does it wrong is okay. – Qdhcjv Mar 04 '16 at 22:03
  • If you know how to handle the exception then you'll be alright. Hope you've solved the issue. – DCruz22 Mar 04 '16 at 22:07
  • I am using values[i - 2], I still see the error. 99% sure it's related to my getValues() call, so the issue is still present. Working on it though. – Qdhcjv Mar 04 '16 at 22:13
0

You should use values[i-2]. It think it will solve the problem ;)

Alexis Clarembeau
  • 2,776
  • 14
  • 27