Search code examples
javastringcomponentsjoptionpane

"String cannot be converted to Component" in JOptionPane


Keep getting a "String cannot be converted to Component" in the last JOptionPane My apologies for the awful looking format. I know using switch is much easier, this is just an assignment for my Java class. Any and all suggestions welcomed. Thankyou

import javax.swing.JOptionPane;
import java.util.*;
import java.text.*;
import java.lang.String;

public class Project4A
{
    public static void main(String[] args)
    {
        NumberFormat formatter = NumberFormat.getCurrencyInstance();
        Scanner sc = new Scanner(System.in);
        double dcostBagel = 2.00;  //Variables
        double dcostDonut = 1.50;
        double dcostCrois = 3.00;
        double dcostLatte = 1.50;
        double dcostCoffee = 1.25;
        double dcostMilk = 1.00;
        double dcostTea = 0.50;
        double dfoodChoiceCost;
        double dbevChoiceCost;
        double dtotDue;
        double dtotCost;
        int ichoiceFood;
        int ichoiceBev;
        int inumOrdered;
        String choiceFood;
        String choiceBev;
        String finChoiceFood;
        String finChoiceBev;
        //Prompts
        choiceFood = JOptionPane.showInputDialog("Welcome to BeBe's Best BreakfastnChoose a Breakfast Item:n1: Bagel @ "+formatter.format(dcostBagel) + "n2: Donut @ "+formatter.format(dcostDonut) + "n3: Croissant @ "+formatter.format(dcostCrois));
        ichoiceFood = Integer.parseInt(choiceFood);
        choiceBev = JOptionPane.showInputDialog("Choose one of the following beverages:nEnter:n1: Latte @ "+formatter.format(dcostLatte)+"n2: Coffee @ "+formatter.format(dcostCoffee)+"n3: Milk @ "+formatter.format(dcostMilk)+"n4: Tea @ "+formatter.format(dcostTea));
        ichoiceBev = Integer.parseInt(choiceBev);
        //If Elses for Food
        if(ichoiceFood == 1)
        {
            finChoiceFood = "Bagel";
            dfoodChoiceCost = dcostBagel;
        }
        else if(ichoiceFood == 2)
        {
            finChoiceFood = "Donut";
            dfoodChoiceCost = dcostDonut;
        }
        else
        {
            finChoiceFood = "Croissant";
            dfoodChoiceCost = dcostCrois;
        }
        //If Elses for Beverages
        if(ichoiceBev == 1)
        {
            finChoiceBev = "Latte";
            dbevChoiceCost = dcostLatte;
        }
        else if(ichoiceBev == 2)
        {
            finChoiceBev = "Coffee";
            dbevChoiceCost = dcostCoffee;
        }
        else if(ichoiceBev == 3)
        {
            finChoiceBev = "Milk";
            dbevChoiceCost = dcostMilk;
        }
        else
        {
            finChoiceBev = "Tea";
            dbevChoiceCost = dcostTea;
        }
        /
        //Retreive num ordered
        System.out.println("How many items would you like?(1 to 20");
        inumOrdered = sc.nextInt();
        //Calculations
        dtotCost = dbevChoiceCost + dfoodChoiceCost;
        dtotDue = dtotCost * inumOrdered;
        //Integer.toString(inumOrdered);
        //Final Output
        JOptionPane.showMessageDialog("Breakfast ordered:n"+finChoiceFood+" @ "+formatter.format(dfoodChoiceCost)+"nnBeverage ordered:n"+finChoiceBev+" @ "+formatter.format(dbevChoiceCost)+"nnTotal cost: "+formatter.format(dtotCost)+"nNumber ordered: "+inumOrdered,"Your Bill");
    }
}

Solution

  • Your code is not matching any method signatures of the showMethodDialogue(...) function(s). Take a look at the JOptionPane Javadoc:

    static void showMessageDialog(Component parentComponent, Object message): Brings up an information-message dialog titled "Message".

    static void showMessageDialog(Component parentComponent, Object message, String title, int messageType): Brings up a dialog that displays a message using a default icon determined by the messageType parameter.

    static void showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon): Brings up a dialog displaying a message, specifying all parameters.

    The parentComponent is defined as "determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used".