简体   繁体   English

用户输入->字符串数组->强制转换的双打不起作用

[英]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. 它应该接受用户输入,将行按空格分开,然后将各个单词作为字符串转储到String []数组中。 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. 我无法从用户输入中返回它的值,但是如果我给定getValues()方法(将String转换为double然后将其添加到数组的方法)直接(例如{“ 1”,“ 2”, “ 3”,“ 4”}),它可以正常工作。 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. 单词数组被传递到switch语句,该语句检查数组的第一个单词以弄清楚用户想要做什么。 If they write "add ", it's supposed to run the getValues() method and use the return as the values added to the map. 如果他们写“ add”,则应该运行getValues()方法并将返回值用作添加到地图的值。 The getValues() method is definitely the issue, and it isn't returning anything when it uses the array of Strings from the getOption() method. getValues()方法绝对是问题所在,当使用getOption()方法中的字符串数组时,它不返回任何内容。 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 . 当我注释掉地图中“ add”案例的添加部分,而只是使用println getValues()返回(应为double数组)时, 如果两次调用add案例,它就可以工作。 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]. 例如,如果我执行“ add abc 1 2 3 4”,则什么也不会发生,但是再次运行完全相同的行将正确返回[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. 根据提供的描述,我相信意外行为的原因是多次调用getOption()方法。 Let's analyze the driver() method: 让我们分析一下driver()方法:

  • 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()); 如果第一个单词是'add',那么它将进入switch的'add'情况并执行dataMap.put(getOption()[1], getValues()); operation. 操作。

In above two steps, getOption() method is called twice. 在以上两个步骤中, getOption()方法被调用了两次。 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 . 相反,我们应该编写两种方法,一种方法是读取用户的输入,另一种方法是将其标记化,然后,我们可以在switch使用tokenizer方法返回的数组。

So what you should do is to read the (System.in) input once and past the String array to others method instead. 因此,您应该做的是读取一次(System.in)输入,然后将String数组传递给other方法。

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

...

String[] inputs = getOption();

...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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