-2

I know that my code still has flaws (its a work in progress). But I got these couple errors and I don't understand why. Any help at all is appreciated thank you!!

theses are the errors

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

and here is the code

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);
}

}

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Grace
  • 1
  • 2
  • 1
    [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MadProgrammer Nov 12 '15 at 00:32
  • thank you for your help, but which error is this in reffereance to. The second one? – Grace Nov 12 '15 at 00:41
  • I tried changing it to while (enterAnother.equal("Y" || "y")) and it still gives me the exact same error – Grace Nov 12 '15 at 00:42

1 Answers1

0

C:\Users\me\Documents\MailOrderEMH.java:27: error: variable numBoxes is already defined in method main(String[])
int numBoxes = Integer.parseInt(numBoxesString);

You've declared numBoxes twice

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

C:\Users\me\Documents\MailOrderEMH.java:70: error: bad operand types for binary operator '||'
while (enterAnother == "Y" || "y")

The condition needs to resolve to true or false, where "y" is a String, which makes no sense. Also == is not how you compare Strings in Java, a simpler solution would be to do something like...

 while ("Y".equalsIgnoreCase(enterAnother) {

C:\Users\me\Documents\MailOrderEMH.java:102: error: incompatible types: String cannot be converted to int
( "Enter Number of Boxes: " );

JOptionPane#showInputDialog returns a String value, so trying to assign a String to an int won't work.

You could do something like...

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

Remember, JOptionPane.showInputDialog can also return null if the dialog is canceled by the user, so you need to prepare for that eventuallaity

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Suggesting `enterAnother == "y" || enterAnother == "Y"` is much better than `"Y".equalsIgnoreCase(enterAnother)`, seeing that OP is a beginner and misunderstanding how `||` in C-like languages differs from "or" in English. –  Nov 12 '15 at 00:41
  • @Rhymoid Except `==` is not how `String` comparison works in Java – MadProgrammer Nov 12 '15 at 00:43
  • Right! My mistake. Still, I'd make the steps a bit more explicit, with `"y".equals(enterAnother) || "Y".equals(enterAnother)` as an (intermediate) suggestion. –  Nov 12 '15 at 00:45
  • @MadProgrammer thank you very much I did what you said and it fixed all of them except for the numBoxes I still get error: incompatible types: String cannot be converted to int ( "Enter Number of Boxes: " ); and I did what you said and I put String value = JOptionPane.showInputDialog("Enter Number of Boxes: "); if (value != null) { numBoxes = Integer.parseInt(value); } but I still get the error – Grace Nov 12 '15 at 04:19
  • I figured out that it taking about the second one in my code tward the bottom like 98. because its a while loop how can I get another input for numboxes if the answer "Y" with out I yelling at me for decaring sring value twice and why is the error ony on the second one – Grace Nov 12 '15 at 04:32
  • You just need to ask the user if they want to repeat. May be using a `int n = JOptionPane.showOptionDialog(frame, "Would you like to add another?" "More", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"Yes", "No"}, options[0]);`. If it returns `0` then the answer is "yes" – MadProgrammer Nov 12 '15 at 04:35