【问题标题】:Java Scanner nextDouble command skipping values with switch case?Java Scanner nextDouble命令使用switch case跳过值?
【发布时间】:2018-03-07 09:20:10
【问题描述】:

我正在将带有 switch case 的参数文件扫描到 Stack 并使用 .nextDouble 命令跳过值?

这是我的代码片段:

while (stackScanner.hasNextLine()) {        

    switch(stackScanner.next()) { 

    case"+": {
        operator= new operationNode("+");
        stack.push(operator);}

    case"-":{
        operator= new operationNode("-");
        stack.push(operator);}

    case"*":{
        operator= new operationNode("*");
        stack.push(operator);}

    case"/":{
        operator= new operationNode("/");
        stack.push(operator);}

    case"^":{
        operator= new operationNode("^");
        stack.push(operator);}

    while(stackScanner.hasNextDouble()) {
        stack.push(new numberNode(stackScanner.nextDouble()));
    }
}

问题出在最后一行,其中参数文件包含以下内容:^ 2 - 3 / 2 6 * 8 + 2.5 3

然而,扫描器只收集:^ 2 - 3 / 6 * 8 + 3

所以它跳过了第一个成对出现的数字(2 和 2.5)。

问题是,当我在 while 循环的末尾添加 stackScanner.next(); 时,它保存的唯一数字是那些值 2 和 2.5?

【问题讨论】:

  • 您是否注意到您的案例中没有中断,并且您的 while 循环位于 switch 语句中?
  • @MauricePerry 我把它作为默认值:但它没有读取某些值。休息似乎也不会影响我的结果(?)
  • 你确定你已经发布了真实的代码吗?当我复制并粘贴它时,我没有看到您所说的结果。特别是,我的堆栈看起来像:[^, 2.0, -, *, /, ^, 3.0, /, ^, 2.0, 6.0, *, /, ^, 8.0, +, -, *, /, ^, 2.5, 3.0],这与@MauricePerry 的观察一致,即您缺少break 语句。
  • @DaveyDaveDave 哦,我完全意识到我做错了什么——没有正确读取堆栈。感谢您的帮助

标签: java switch-statement arguments java.util.scanner next


【解决方案1】:

复制您的代码并稍作修改以使用 Stack<String> 而不是实现您的 operationNodenumberNode 类,我发现以下工作(我认为)符合您的预期:

public static void main(String... args) {
    Scanner stackScanner = new Scanner("^ 2 - 3 / 2 6 * 8 + 2.5 3");

    Stack<String> stack = new Stack<>();

    while (stackScanner.hasNextLine()) {

        switch (stackScanner.next()) {
            case "+": {
                stack.push("+");
                break;
            }

            case "-": {
                stack.push("-");
                break;
            }

            case "*": {
                stack.push("*");
                break;
            }

            case "/": {
                stack.push("/");
                break;
            }

            case "^": {
                stack.push("^");
                break;
            }
        }

        while (stackScanner.hasNextDouble()) {
            stack.push(Double.toString(stackScanner.nextDouble()));
        }
    }

    System.out.println(stack);
}

也就是说,我添加了您似乎不需要的 break; 语句(可能是某种 JVM 差异?)并将 while 循环移到了 switch 之外。

【讨论】:

    【解决方案2】:

    您需要将switch 包装成while 并将double 的处理移动到default 块中,例如:

    while (stackScanner.hasNextLine()) {
        String nextToken = stackScanner.next();
        switch(nextToken) { 
    
        case"+": {
            System.out.println("+");
            break;
            }
    
        case"-":{
            System.out.println("-");
            break;
        }
    
        case"*":{
            System.out.println("*");
            break;
        }
    
        case"/":{
            System.out.println("/");
            break;
        }
    
        case"^":{
            System.out.println("^");
            break;
        }
    
        default:
            if(isDouble(nextToken)){
                //Do something
            }
            break;
        }
    }
    

    您还需要编写一个方法来检查double。它看起来像这样:

    private boolean isDouble(String number){
        try{
            Double.parseDouble(number);
            return true;
        }Catch(Exception e){
            return false;
        }
    }
    

    【讨论】:

    • 对不起 - 我不认为这实际上是正确的。这将只处理每行一个令牌。如果输入是单行——“^ 2 - 3 / 2 6 * 8 + 2.5 3”,它将处理“^”然后停止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多