2015-11-12 4 views
0

Нет mater, как я это делаю. Я продолжаю получать ту же ошибку. Мне нужно получить ввод пользователя с помощью 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: "); 
+0

Я думаю, что вы не сохранили свой файл перед компиляцией. – LEQADA

+0

@LEQADA Я точно сохраняю его, поэтому не знаю, что случилось. Я добавил свой полный код к вопросу. может что-то в этом быть причиной ошибки – Grace

ответ

0

Что это в то время как заявление enterAnother.equal("Y" || "y")? Должно быть

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

И в этой линии

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

Вы пытаетесь преобразовать строку в целое. Вам нужно использовать parseInt таким образом

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

Я сохраняю файл java – Grace

+0

вот остальная часть кода, это что-то еще вниз, это беспорядок вверх – Grace

+0

Я добавил полный код к проблеме – Grace

Смежные вопросы