【问题标题】:Parsing a string to boolean in java在java中将字符串解析为布尔值
【发布时间】:2019-02-10 02:43:08
【问题描述】:

我有以下简单的销售程序,但无法围绕该程序创建重启循环。我的主要问题是将布尔值从字符串转换为布尔类型,我在 Eclipse 中收到错误消息,提示“方法 parseBoolean(String) 未定义布尔类型”。

不过,我在顶部定义了一个布尔变量,boolean tryAgain = false;

由于某种原因,我无法将用户输入扫描仪设置为无错误地获取 True 或 False 值。当我尝试将用户的 nextLine 转换为布尔值时,最后一行发生错误。

import java.util.Scanner;
public class Sales {
    public static void main(String args[]) {

        String item1, item2, item3;                 //Three vars for items
        double price1, price2, price3;              //Three vars for price
        int quantity1, quantity2, quantity3;        //Three vars for quantity
        Scanner userInput = new Scanner(System.in); //Creates scanner for user input
        double sum;                                 //var for total before tax
        double tax;                                 //var for tax
        double total;                               //var for total with tax
        double tax_total;                           //var to calculate tax
        boolean tryAgain = true;                    //boolean for try again loop to restart program

        // First set of inputs

        while (tryAgain) {

            System.out.println("Please enter the first item: ");
            item1 = userInput.nextLine();
            System.out.println("Please enter the price of the first item: ");
            price1 = userInput.nextDouble();
            System.out.println("Please enter the quantity purchased: ");
            quantity1 = userInput.nextInt();

            // Second set of inputs

            System.out.println("Please enter the second item: ");
            item2 = userInput.next(); 
            System.out.println("Please enter the price of the second item: ");
            price2 = userInput.nextDouble();
            System.out.println("Please enter the quantity purchased: ");
            quantity2 = userInput.nextInt();

            // Third set of inputs

            System.out.println("Please enter the third item: ");
            item3 = userInput.next(); //skipping over item 2.  Why?
            System.out.println("Please enter the price of the third item: ");
            price3 = userInput.nextDouble();
            System.out.println("Please enter the quantity purchased: ");
            quantity3 = userInput.nextInt();

            System.out.println("Please enter the sales tax rate: ");            //Prompt user for tax rate
            tax = userInput.nextDouble();

            //Print line item totals

            System.out.println("Line item totals");
            System.out.println("____________________");
            System.out.println("Item 1: " + item1 + "\t" + "$" + price1);
            System.out.println("Item 2: " + item2 + "\t" + "$" + price2);
            System.out.println("Item 3: " + item3 + "\t" + "$" + price3);
            System.out.println();

            //Process final output for display

            sum = price1 + price2 + price3;
            total = (sum * tax) + sum;
            tax_total = tax * sum;
            System.out.println("Total cost(no tax):\t" + "$" + sum);                    //display total cost witout tax
            System.out.println("Total tax(" + tax + " rate): \t" + "$" + tax_total);    //display total tax
            System.out.println("Total cost(with tax):\t" + "$" + total);                //display total with tax
            System.out.println();
            System.out.println("Program created by James Bellamy");

            System.out.println("Do you want to run the program again? (True or False)");

            tryAgain = Boolean.parseBoolean(userInput.nextLine());

        }




    }

}

【问题讨论】:

  • 你定义了一个名为Boolean的类吗?

标签: java variables boolean


【解决方案1】:

它给你带来麻烦的原因是当用户输入一个双精度然后点击回车时,刚刚输入了两件事 - 双精度和一个“换行符”,即\n。

您正在调用的方法“nextDouble()”仅读取双精度,从而将换行符留在输入流中。但是调用 nextLine() 确实会读入换行符,这就是为什么您必须在代码运行之前调用 nextLine() 的原因。

在最后一行之前添加这一行。

userInput.nextLine();

【讨论】:

  • @chrylis 代码中没有编译错误。请再次检查 Boolean 是 java 定义的类,它有一个 parseBoolean 方法,所以没有编译错误。你能告诉我你得到了什么编译错误吗?
  • 阅读问题:The method parseBoolean(String) is undefined for the type Boolean
  • 大家好,感谢您的反馈。我按照纳拉扬所说的做了,它奏效了——它接受了输入。但是,当用户输入 true 时,程序挂起终止,这不是我想要的。为什么要终止?它应该转到顶部并继续循环。
【解决方案2】:

好吧,由于某种原因,我无法让它与布尔值一起使用,所以我改为使用 do while 循环。这是最终的代码。此外,我确实使用提示来清除 nextLine()。

import java.util.Scanner;
public class Sales2 {
    public static void main(String args[]) {

        String item1, item2, item3;                 //Three vars for items
        double price1, price2, price3;              //Three vars for price
        int quantity1, quantity2, quantity3;        //Three vars for quantity
        Scanner userInput = new Scanner(System.in); //Creates scanner for user inputjea
        double sum;                                 //var for total before tax
        double tax;                                 //var for tax
        double total;                               //var for total with tax
        double tax_total;                           //var to calculate tax
        boolean tryAgain = true;                    //boolean for try again loop to restart program

        // First set of inputs

        String answer;                              //Define var type of String for do while loop to restart program
        do {                                        //Do this loop while string == (last line of code while loop)

            System.out.println("Please enter the first item: ");
            item1 = userInput.nextLine();
            System.out.println("Please enter the price of the first item: ");
            price1 = userInput.nextDouble();
            System.out.println("Please enter the quantity purchased: ");
            quantity1 = userInput.nextInt();

            // Second set of inputs

            System.out.println("Please enter the second item: ");
            item2 = userInput.next(); 
            System.out.println("Please enter the price of the second item: ");
            price2 = userInput.nextDouble();
            System.out.println("Please enter the quantity purchased: ");
            quantity2 = userInput.nextInt();

            // Third set of inputs

            System.out.println("Please enter the third item: ");
            item3 = userInput.next(); //skipping over item 2.  Why?
            System.out.println("Please enter the price of the third item: ");
            price3 = userInput.nextDouble();
            System.out.println("Please enter the quantity purchased: ");
            quantity3 = userInput.nextInt();

            System.out.println("Please enter the sales tax rate: ");            //Prompt user for tax rate
            tax = userInput.nextDouble();

            //Print line item totals

            System.out.println("Line item totals");
            System.out.println("____________________");
            System.out.println("Item 1: " + item1 + "\t" + "$" + price1);
            System.out.println("Item 2: " + item2 + "\t" + "$" + price2);
            System.out.println("Item 3: " + item3 + "\t" + "$" + price3);
            System.out.println();

            //Process final output for display

            sum = price1 + price2 + price3;
            total = (sum * tax) + sum;
            tax_total = tax * sum;
            System.out.println("Total cost(no tax):\t" + "$" + sum);                    //display total cost witout tax
            System.out.println("Total tax(" + tax + " rate): \t" + "$" + tax_total);    //display total tax
            System.out.println("Total cost(with tax):\t" + "$" + total);                //display total with tax
            System.out.println();
            System.out.println("Program created by James Bellamy");

            System.out.println("Do you want to run the program again? (yes or no)");    //Prompt user for restart
            userInput.nextLine();                                                       //Clear scanner 
            answer = userInput.nextLine();

        }
        while (answer.equalsIgnoreCase("Yes"));                                         //Connected to do while loop
                                                                                        //




    }

}

【讨论】:

    【解决方案3】:

    Scanner 支持nextBoolean()。就用它吧:

    tryAgain = userInput.nextBoolean()
    

    【讨论】:

      猜你喜欢
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2021-02-10
      • 2013-11-11
      • 1970-01-01
      • 2011-07-10
      相关资源
      最近更新 更多