繁体   English   中英

为什么不能将此字符串转换为整数?

[英]Why can't I convert this string into an integer?

无论如何,我都会遇到相同的错误,我需要使用JOptionPane获取用户输入,然后将其转换为整数。

这是我的代码

import javax.swing.JOptionPane; // Imports JOptionPane class.

public class MailOrderEMH {
    public static void main(String[] args) {
        // Declare string variables
        String title;
        String firstName;
        String lastName;
        String streetAddress;
        String city;
        String state;
        String zip;
        int numBoxes;
        int count = 1;
        String enterAnother = "Y"; //INITILIZE the loop control variable


        //Conver srring to integer
        numBoxes = Integer.parseInt(JOptionPane.showInputDialog("Enter Number of Boxes: "));

        //get input values from user
        title = JOptionPane.showInputDialog("What is your title ex. (Ms. Mr. Dr.) ");

        //get input values from user
        firstName = JOptionPane.showInputDialog("Enter First Name: ");

        //get input values from user
        lastName = JOptionPane.showInputDialog("Enter Last Name: ");

        //get input values from user
        streetAddress = JOptionPane.showInputDialog("Enter Street Address: ");

        //get input values from user
        city = JOptionPane.showInputDialog("Enter City: ");

        //get input values from user
        state = JOptionPane.showInputDialog("Enter State: ");

        //get input values from user
        zip = JOptionPane.showInputDialog("Enter Zip Code: ");


        while (count <= numBoxes) {
            System.out.println(title + firstName + lastName);
            System.out.println(streetAddress);
            System.out.println(city + state + zip);
            System.out.println("Box" + count + "of" + numBoxes);
            count = count + 1;
        }
        //get input values from user
        enterAnother = JOptionPane.showInputDialog(" Do you want to produce more labels? Y or N ");

        while (enterAnother.equal("Y" || "y")) {
            //get input values from user
            title = JOptionPane.showInputDialog("What is your title ex. (Ms. Mr. Dr.) ");

            //get input values from user
            firstName = JOptionPane.showInputDialog("Enter First Name: ");

            //get input values from user
            lastName = JOptionPane.showInputDialog("Enter Last Name: ");

            //get input values from user
            streetAddress = JOptionPane.showInputDialog("Enter Street Address: ");

            //get input values from user
            city = JOptionPane.showInputDialog("Enter City: ");

            //get input values from user
            state = JOptionPane.showInputDialog("Enter State: ");

            //get input values from user
            zip = JOptionPane.showInputDialog("Enter Zip Code: ");

            //get input values from user
            numBoxes = JOptionPane.showInputDialog("Enter Number of Boxes: ");
        }
        // End program.
        System.exit(0);
    }
}

这是错误

error: incompatible types: String cannot be converted to int
            ( "Enter Number of Boxes: " );

while语句enterAnother.equal("Y" || "y")什么? 一定是

while (enterAnother.equals("Y") || enterAnother.equals("y"))

在这一行

numBoxes = JOptionPane.showInputDialog
                    ( "Enter Number of Boxes: " );

您正在尝试将String转换为int。 您需要以这种方式使用parseInt

numBoxes = Integer.parseInt(JOptionPane.showInputDialog
                ( "Enter Number of Boxes: " ));

暂无
暂无

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

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