1

I am getting a string to int conversion error. I have tried to seek answers here: How to convert a String to an int in Java? But i could not solve the issue. My code is as follows:

import javax.swing.JOptionPane;
public class CarlysEventPrice
{
    public static void main(String[] args)
    {
    int total_Guests, total_Price, price_Per_Guest;
    total_Guests = JOptionPane.showInputDialog(null, "Please input the number of guests");
    int total_Guests = Integer.parseInt(total_Guests);
    total_Price = price_Per_Guest * total_Guests;
    JOptionPane.showMessageDialog(null,
                                  "************************************************\n" +
                                  "* Carly's makes the food that makes it a party *\n" +
                                  "************************************************\n");
    JOptionPane.showMessageDialog(null,
                                  "The total guests are " +total_Guests+ "\n" +
                                  "The price per guest is " +price_Per_Guest+ "\n" +
                                  "The total price is " +total_Price);
    boolean large_Event = (total_Guests >= 50);
    JOptionPane.showMessageDialog(null,
                                  "Is this job classified as a large event: " +large_Event);      

    }
}

my code is showing this error:

CarlysEventPrice.java:10: error: incompatible types: String cannot be converted to int
      total_Guests = JOptionPane.showInputDialog(null, "Please input the number of guests");
                                                ^
CarlysEventPrice.java:11: error: variable total_Guests is already defined in method main(String[])
        int total_Guests = Integer.parseInt(total_Guests);
            ^
CarlysEventPrice.java:11: error: incompatible types: int cannot be converted to String
        int total_Guests = Integer.parseInt(total_Guests);
                                            ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output

I am using jGrasp for programming, and i also tried it to compile using cmd but it gave the same error. Thank you for helping.

Community
  • 1
  • 1
Shayan Ahmed
  • 33
  • 1
  • 9
  • `total_Guests` is already an `int`. You are trying to parse an `int` to an `int`, hence the error. `parseInt` method expects a String argument. – Darshan Mehta May 14 '17 at 14:44

2 Answers2

2

The problem is that you defined a total_Guests variable twice (1) and tried to assign a String result of the showInputDialog method to the int variable (2).

To achieve what you actually want to:

String input = JOptionPane.showInputDialog(null, "--/--");
int totalGuests = Integer.parseInt(input);

Have a look at the showInputDialog method declaration:

String showInputDialog(Component parentComponent, Object message)
^^^

You should understand that the String and the int (or the Integer wrapper) are totally different data types and in such the statically typed languages as Java is you are not allowed to perform conversions even a String "12" looks like an int 12.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0

1.total_Guests is an int, not a String. Integer#parseInt expects a String. 2. You declare totalGuest twice. Try

total_Guests = Integer.parseInt(JOptionPane.showInputDialog(null, "Message"));

Also, give some initial value to price_Per_Guest, like

int total_Guests, total_Price, price_Per_Guest = 5;

or else it would give variable not initialised error.

Community
  • 1
  • 1
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34