首页 > 解决方案 > 在java中将字符串解析为布尔值

问题描述

我有以下简单的销售程序,并且无法围绕该程序创建重新启动循环。我的主要问题是将布尔值从字符串转换为布尔类型,我在 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());

        }




    }

}

标签: javavariablesboolean

解决方案


好吧,由于某种原因,我无法让它与布尔值一起工作,所以我改为使用 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
                                                                                        //




    }

}

推荐阅读