简体   繁体   中英

User input -> array of strings -> casted doubles not working

I have a moderately simple program that I'm having issues with. It's supposed to take user input, split the line up by the spaces and dump the individual words as Strings into a String[] array. This all works just fine. However, I need to take the values that are numbers (we know which words can be casted already, no need to check), cast them to doubles, add the doubles to an array, and return it. I cannot get it to return the values from the user input, but if I give the getValues() method (the one that converts String to double then adds to an array) values directly (ex. {"1", "2", "3", "4"}) it works fine. The user inputted String array does not work.

double data[] = {3.0, 15.0, 7.0, 27.0};
HashMap<String,double[]> dataMap = new HashMap();

public String[] getOption(){
    Scanner input = new Scanner(System.in);
    System.out.println("\nInput string\n");
    String line = input.nextLine();
    return split(line);
}

public String[] split(String line){
    String[] optionLine = line.split(" ");
    return optionLine;
}

public double[] getValues(String[] inputs){
double values[] = new double[inputs.length - 2];
for (int i = 2; i < inputs.length; i++){
    double valueDouble;
    valueDouble = Double.parseDouble(inputs[i]);
    values[i-2] = valueDouble;
    }
return values;
}

public void driver(){
    String option = getOption()[0];
    while (!"quit".equals(option)){
    switch (option) {
        case "add": if (options.length < 2){
                        break;
                    }
                    else{
                        dataMap.put(options[1], getValues(options));
                    }
                    //System.out.println(dataMap.isEmpty());
                    //System.out.println(Arrays.toString(getValues()));
                    break;
        case "quit": return;
    }
    option = getOption()[0];
    }
}
public static void main(String[] args) {
    StatsAgain sa = new StatsAgain();
    sa.driver();
}
}

Let me know what you can make of it. Basically, the user enters a String that is broken down into an array of words. The array of words is passed to the switch statement that checks the first word of the array to figure out what the user wants to do. If they write "add ", it's supposed to run the getValues() method and use the return as the values added to the map. The getValues() method is definitely the issue, and it isn't returning anything when it uses the array of Strings from the getOption() method. I hope that made sense...

edit: Forgot to mention! When I comment out the map adding part of the "add" case, and instead just println the getValues() return (should be double array), it works if I call the add case twice . For example, if I do "add abc 1 2 3 4", nothing happens, but running the exact same line again will properly return [1.0, 2.0, 3.0, 4.0].

Based on the description provided, I believe the reason of unexpected behavior is multiple calls of getOption() method. Let's analyze the driver() method:

  • String option = getOption()[0]; assigns the first word of user's input to option string.
  • If the first word is 'add' then, it goes into 'add' case of switch and performs dataMap.put(getOption()[1], getValues()); operation.

In above two steps, getOption() method is called twice. As it reads user's input every time, the input string provided by user for the first call gets lost (except for the first token). We should instead write two methods, one to read user's input and other to tokenize it and then, we can use the array returned by tokenizer method in switch .

So what you should do is to read the (System.in) input once and past the String array to others method instead.

public String[] getOption(){
    String line = new Scanner(System.in).nextLine();
    return line.split(" ");
}

...

String[] inputs = getOption();

...

case "add": dataMap.put(inputs[1], getValues(inputs));
....    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM