这些Java错误意味着什么?

时间:2015-11-12 00:31:53

标签: java compiler-errors syntax-error

我知道我的代码仍有缺陷(它正在进行中)。但我得到了这些错误,我不明白为什么。 感谢任何帮助,谢谢!!

这些是错误

C:\Users\me\Documents\MailOrderEMH.java:27: error: variable numBoxes is already defined in method main(String[])
    int numBoxes = Integer.parseInt(numBoxesString);
        ^
C:\Users\me\Documents\MailOrderEMH.java:70: error: bad operand types for binary operator '||'
     while (enterAnother == "Y" || "y")
                                ^
  first type:  boolean
  second type: String
C:\Users\me\Documents\MailOrderEMH.java:102: error: incompatible types: String cannot be converted to int
            ( "Enter Number of Boxes: " );
            ^
3 errors

Tool completed with exit code 1

这是代码

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;
String numBoxesString;
int numBoxes;
int count = 1;
String enterAnother = "Y"; //INITILIZE the loop control variable

//get input values from user
numBoxesString = JOptionPane.showInputDialog
( "Enter Number of Boxes: " );

//Conver srring to integer
int numBoxes = Integer.parseInt(numBoxesString);

//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 == "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);
}

}

1 个答案:

答案 0 :(得分:0)

  

C:\ Users \ me \ Documents \ MailOrderEMH.java:27:错误:变量numBoxes   已经在方法main(String [])中定义了       int numBoxes = Integer.parseInt(numBoxesString);

您已宣布numBoxes两次

int numBoxes;
//...
int numBoxes = Integer.parseInt(numBoxesString);
  

C:\ Users \ me \ Documents \ MailOrderEMH.java:70:错误:错误的操作数类型   对于二元运算符&#39; ||&#39;
       while(enterAnother ==&#34; Y&#34; ||&#34; y&#34;)

条件需要解析为truefalse,其中"y"String,这没有任何意义。另外==不是你在Java中比较String的方法,更简单的解决方案是做类似的事情......

 while ("Y".equalsIgnoreCase(enterAnother) {
  

C:\ Users \ me \ Documents \ MailOrderEMH.java:102:错误:不兼容的类型:字符串无法转换为int
              (&#34;输入数量:&#34;);

JOptionPane#showInputDialog会返回String值,因此尝试将String分配给int不会起作用。

你可以做点像......

String value = JOptionPane.showInputDialog("Enter Number of Boxes: ");
if (value != null) {
    numBoxes = Integer.parseInt(value);
}

请记住,如果用户取消了对话框,JOptionPane.showInputDialog也可以返回null,因此您需要为该事件做好准备