Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Public class help/error

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Public class help/error

    Here is my code:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package internetserviceproviderpart2;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String inputString;
            char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double totalFeeA = 0;
            double totalFeeB = 0;
            double savingsAB = 0;
            double savingsAC = 0;
            double savingsBC = 0;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            if (packageLetter=='A' && hoursUsed>10)
            {
                additionalHours = hoursUsed - 10;
                totalFeeA = 9.95 + (additionalHours * 2.00);
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFeeA);
            }
            else if (packageLetter=='A' && hoursUsed<=10)
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            else if (packageLetter=='B' && hoursUsed>20)
            {
                additionalHours = hoursUsed - 20;
                totalFeeB = 13.95 + (additionalHours * 1.00);
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFeeB);
            }
            else if (packageLetter=='B' && hoursUsed<=20)
                JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
            else if (packageLetter=='C')
                JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
            else
                JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
     
            if(totalFeeA>totalFeeB)
            {
                savingsAB = totalFeeA - totalFeeB;
                JOptionPane.showMessageDialog(null,"You would save $" + savingsAB +
                        "if you purchase Package B.");
            }
            else if (totalFeeA>19.95)
            {
                savingsAC = totalFeeA - 19.95;
                JOptionPane.showMessageDialog(null,"You would save $" + savingsAC +
                        "if you purchase Package C.");
            }
            else if (totalFeeB>19.95)
            {
                savingsBC = totalFeeB - 19.95;
                JOptionPane.showMessageDialog(null,"You would save $" + savingsBC +
                        "if you purchase Package C.");
            }
            else
     
            System.exit(0);
        }
     
    }

    Next to
    public class Main {

    there is a red circle with an exclamation point inside saying "class Main is public, should be declared in a file named Main.java"

    I'm not too sure what that means.


    Also, when I run the program...this pops out:
    run:
    java.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable source code - class Main is public, should be declared in a file named Main.java
            at internetserviceprovider.Main.<clinit>(Internet Service Provider.java:12)
    Could not find the main class: internetserviceprovider.Main.  Program will exit.
    Exception in thread "main" 
    Exception in thread "main" Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

    How do I fix it so I can run the program correctly?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Public class help/error

    What is the name of your file?

    Since you have included the statement:
    public class Main
    you are saying that the name of your file is Main.java. If not, the compiler will flip out at you. If the name is correct, then my next guess would be that the compiler doesn't like your use of the word Main as the name of your file.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Public class help/error

    Quote Originally Posted by aussiemcgr View Post
    What is the name of your file?

    Since you have included the statement:
    public class Main
    you are saying that the name of your file is Main.java. If not, the compiler will flip out at you. If the name is correct, then my next guess would be that the compiler doesn't like your use of the word Main as the name of your file.
    My file's name is Internet Service Provider Part 2.java
    I changed
    public class Main
    into
    public class internetserviceproviderpart2 {

    There is still a red squiggly line under it saying "internetserviceproviderpart2 = > "internetserviceproviderpart2" is not a known variable in the current context.<"

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Public class help/error

    Quote Originally Posted by Plural View Post
    My file's name is Internet Service Provider Part 2.java
    I changed
    public class Main
    into
    public class internetserviceproviderpart2 {

    There is still a red squiggly line under it saying "internetserviceproviderpart2 = > "internetserviceproviderpart2" is not a known variable in the current context.<"
    Internet Service Provider Part 2.java

    If it's exactly as above, it might be better if you put it as

    InternetServiceProviderPart2

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Public class help/error

    Quote Originally Posted by javapenguin View Post
    Internet Service Provider Part 2.java

    If it's exactly as above, it might be better if you put it as

    InternetServiceProviderPart2
    I changed it just now but it is still the same problem.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Lightbulb Re: Public class help/error

    Quote Originally Posted by Plural View Post
    I changed it just now but it is still the same problem.
    well, maybe it can't recognize your package, which happens to have the same name as the class.

Similar Threads

  1. Keyboard.class error
    By block g raptor in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2010, 01:37 AM
  2. Convert string to private and public key
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 5th, 2010, 08:37 AM
  3. PayCheck class... percision error.. help!
    By ber1023 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 19th, 2009, 03:31 PM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. Truncated class file error
    By Koâk in forum Exceptions
    Replies: 4
    Last Post: June 23rd, 2009, 11:23 AM