Hey Guys, Im new to learning java and I am writing a few programs for practice. I cant figure out whats wrong with my code. When i try to compile i get an "error . class expected" Any advice is appreciated, thank you.

import javax.swing.JOptionPane;

public class TuitionCost
{
    public static void main(String [] args)
    {
        JOptionPane.showMessageDialog(null,"School Tuition Cost Calculation Program","Tuition Costs at School",JOptionPane.INFORMATION_MESSAGE);
        String ResidencyInput = JOptionPane.showInputDialog("Are you a:" +
                                            "\n1 - College District Resident" +
                                            "\n2 - Non-resident of College District" +
                                            "\n3 - Out-of-state or International Student" +
                                            "\n\n Enter a 1,2, or 3:", JOptionPane.QUESTION_MESSAGE);

        int Residency = Integer.parseInt(ResidencyInput); 

        if (Residency < 1 || Residency > 4)
            JOptionPane.showMessageDialog(null,"\n\nYou must enter a 1,2, or 3" +
                                                  "\nPlease run the program again", 
                                                  "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
            System.exit(0);

        if (Residency == 1)
                double CreditHour = 71.40;  
        else if (Residency == 2)
                 double CreditHour = 125.30;

    }
}

Can you post the full error message that you are getting?

Well ive made progress this is what my code looks like now.

import javax.swing.JOptionPane;

    public class TuitionCost
    {
        public static void main(String [] args)
        {
            JOptionPane.showMessageDialog(null,"School Tuition Cost Calculation Program","Tuition Costs at School",JOptionPane.INFORMATION_MESSAGE);
            String ResidencyInput = JOptionPane.showInputDialog("Are you a:" +
                                                "\n1 - College District Resident" +
                                                "\n2 - Non-resident of College District" +
                                                "\n3 - Out-of-state or International Student" +
                                                "\n\n Enter a 1,2, or 3:", JOptionPane.QUESTION_MESSAGE);

            int Residency = Integer.parseInt(ResidencyInput);
            if (Residency < 1 || Residency > 3)
                {
                JOptionPane.showMessageDialog(null,"You must enter a 1,2, or 3" +
                                                      "\nPlease run the program again", 
                                                      "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
                System.exit(0);
                }

            else
            {
                double CreditHourCost;
                if (Residency == 1)
                        CreditHourCost = 71.40;
                else if (Residency == 2)
                        CreditHourCost = 125.30;
                else if (Residency == 3)
                        CreditHourCost = 175.80;        
            }

            String CreditHoursInput = JOptionPane.showInputDialog("How Many Credit Hours are You Taking?", JOptionPane.QUESTION_MESSAGE);
            int CreditHours = Integer.parseInt(CreditHoursInput);
            if (CreditHours < 1)
                JOptionPane.showMessageDialog(null,"You Must Enter A Value Of 1 or More" +
                                                        "\nPlease Run The Program Again",
                                                        "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
            else
            {
                double TotalTuition = CreditHourCost * CreditHours;
                JOptionPane.showMessageDialog(null, CreditHours + "Credit Hours" +
                                                        "at" + CreditHourCost + "yields a tuition of " +
                                                        TotalTuition);
            }

        }
    }

And now I am getting this error

`TuitionCost.java:42: cannot find symbol
symbol  : variable CreditHourCost
location: class TuitionCost
            double TotalTuition = CreditHourCost * CreditHours;
                                  ^
TuitionCost.java:44: cannot find symbol
symbol  : variable CreditHourCost
location: class TuitionCost
`

Really dont understand why my variable is not "intialized" when i clearly declared what it should be in my first if-else statement. Any ideas?

You should declare your CreditHourCost outside your if else statement if you want it to be seen from the other part of your code. And proper way of declaring variable should be in camel case. Thats the standard. example:

private String variableName;

:)

Haha, then the standard it is. However, I failed to implement your suggestions successfully. I get this error code now

TuitionCost.java:15: illegal start of expression
        private double creditHourCost;
        ^
TuitionCost.java:36: illegal start of expression
        private double totalTuition;
        ^

And the code now reads as follows

import javax.swing.JOptionPane;

public class TuitionCost
{
    public static void main(String [] args)
    {
        JOptionPane.showMessageDialog(null,"OCC Tuition Cost Calculation Program","Tuition Costs at OCC",JOptionPane.INFORMATION_MESSAGE);
        String residencyInput = JOptionPane.showInputDialog("Are you a:" +
                                            "\n1 - College District Resident" +
                                            "\n2 - Non-resident of College District" +
                                            "\n3 - Out-of-state or International Student" +
                                            "\n\n Enter a 1,2, or 3:", JOptionPane.QUESTION_MESSAGE);

        int residency = Integer.parseInt(residencyInput);
        private double creditHourCost;
        if (residency < 1 || residency > 3)
            {
            JOptionPane.showMessageDialog(null,"You must enter a 1,2, or 3" +
                                                  "\nPlease run the program again", 
                                                  "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
            }

        else
        {
            if (residency == 1)
                    creditHourCost = 71.40;
            else if (residency == 2)
                    creditHourCost = 125.30;
            else if (residency == 3)
                    CreditHourCost = 175.80;        
        }

        String creditHoursInput = JOptionPane.showInputDialog("How Many Credit Hours are You Taking?", JOptionPane.QUESTION_MESSAGE);
        int creditHours = Integer.parseInt(CreditHoursInput);
        private double totalTuition;
        if (creditHours < 1)
            JOptionPane.showMessageDialog(null,"You Must Enter A Value Of 1 or More" +
                                                    "\nPlease Run The Program Again",
                                                    "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
        else
        {
            totalTuition = creditHourCost * creditHours;
            JOptionPane.showMessageDialog(null, creditHours + "Credit Hours" +
                                                    "at" + creditHourCost + "yields a tuition of " +
                                                    totalTuition);
        }

    }
}

you can't declare a private variable inside a method. Try removing your private modifier in your line 15 and 36 and see if it works. :P

Thanks for your help dimasalang, would you mind posting a working example. When i tried what you said i got this error

TuitionCost.java:29: non-static variable creditHourCost cannot be referenced from a static context
                     creditHourCost = 71.40;

And here is my code

import javax.swing.JOptionPane;

public class TuitionCost
{
    private double creditHourCost;

    public static void main(String [] args)
    {
        JOptionPane.showMessageDialog(null,"OCC Tuition Cost Calculation Program","Tuition Costs at OCC",JOptionPane.INFORMATION_MESSAGE);
        String residencyInput = JOptionPane.showInputDialog("Are you a:" +
                                            "\n1 - College District Resident" +
                                            "\n2 - Non-resident of College District" +
                                            "\n3 - Out-of-state or International Student" +
                                            "\n\n Enter a 1,2, or 3:", JOptionPane.QUESTION_MESSAGE);

        int residency = Integer.parseInt(residencyInput);

        if (residency < 1 || residency > 3)
            {
            JOptionPane.showMessageDialog(null,"You must enter a 1,2, or 3" +
                                                  "\nPlease run the program again", 
                                                  "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
            }

        else
        {
            if (residency == 1)
                     creditHourCost = 71.40;
            else if (residency == 2)
                     creditHourCost = 125.30;
            else if (residency == 3)
                     creditHourCost = 175.80;   
            JOptionPane.showMessageDialog(null, creditHourCost);    
        }

        String creditHoursInput = JOptionPane.showInputDialog("How Many Credit Hours are You Taking?", JOptionPane.QUESTION_MESSAGE);
        int creditHours = Integer.parseInt(creditHoursInput);
        double totalTuition;
        if (creditHours < 1)
            JOptionPane.showMessageDialog(null,"You Must Enter A Value Of 1 or More" +
                                                    "\nPlease Run The Program Again",
                                                    "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
        else
        {
            totalTuition = creditHourCost * creditHours;
            JOptionPane.showMessageDialog(null, creditHours + "Credit Hours" +
                                                    "at" + creditHourCost + "yields a tuition of " +
                                                    totalTuition);
        }


    }
}

Sir don't declare creditHourCost outside your main method. declare it inside the main method like this:

double creditHourCost = 0.0;

or make it a static variable if you want it outside the main:

private static double creditHourCost;

:)

Here's a working example for you

import javax.swing.JOptionPane;
public class TuitionCost
{
 //   private static double creditHourCost;
    public static void main(String [] args)
    {
        double creditHourCost = 0.0
        JOptionPane.showMessageDialog(null,"OCC Tuition Cost Calculation Program","Tuition Costs at OCC",JOptionPane.INFORMATION_MESSAGE);
        String residencyInput = JOptionPane.showInputDialog("Are you a:" +
                                            "\n1 - College District Resident" +
                                            "\n2 - Non-resident of College District" +
                                            "\n3 - Out-of-state or International Student" +
                                            "\n\n Enter a 1,2, or 3:", JOptionPane.QUESTION_MESSAGE);
        int residency = Integer.parseInt(residencyInput);
        if (residency < 1 || residency > 3)
            {
            JOptionPane.showMessageDialog(null,"You must enter a 1,2, or 3" +
                                                  "\nPlease run the program again", 
                                                  "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
            }
        else
        {
            if (residency == 1)
                     creditHourCost = 71.40;
            else if (residency == 2)
                     creditHourCost = 125.30;
            else if (residency == 3)
                     creditHourCost = 175.80;   
            JOptionPane.showMessageDialog(null, creditHourCost);    
        }
        String creditHoursInput = JOptionPane.showInputDialog("How Many Credit Hours are You Taking?", JOptionPane.QUESTION_MESSAGE);
        int creditHours = Integer.parseInt(creditHoursInput);
        double totalTuition;
        if (creditHours < 1)
            JOptionPane.showMessageDialog(null,"You Must Enter A Value Of 1 or More" +
                                                    "\nPlease Run The Program Again",
                                                    "ERROR! - INVALID INPUT", JOptionPane.WARNING_MESSAGE);
        else
        {
            totalTuition = creditHourCost * creditHours;
            JOptionPane.showMessageDialog(null, creditHours + "Credit Hours" +
                                                    "at" + creditHourCost + "yields a tuition of " +
                                                    totalTuition);
        }
    }
}

really appreciate the help bud. Thanks a million. My problem was that i wasnt setting the double to 0.0 before my if-else statement. Thanks agian

My pleasure! cheers. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.