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 8 of 8

Thread: Trying to learn Java programming

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to learn Java programming

    I am trying to learn Java programming and I am trying to figure out how to call a class to write a larger program. I have included the small part of the program that I am trying to get to work but I keep getting a compiler error. Can anyone help me figure this out. I have also copied in the error that I am getting from TextPad when I try to compile this.

    ************************************************** ************************************************** **************************

    import javax.swing.JOptionPane;
    import java.text.NumberFormat;

    public class Chapter7Prog1RandyHaupert
    {

    public static void main (String [] args)

    {

    int quarters, dimes, nickels, pennies;
    double totalamount = 0;

    String input = JOptionPane.showInputDialog (null, "How many quaters do you have. " );
    quarters = Integer.parseInt (input);

    quarters = Coins.getQuarters (dimes);

    }

    }

    import java.text.DecimalFormat;

    public class Coins

    {
    public final DecimalFormat MONEY = new DecimalFormat( "$#,##0.00" );

    public Coins( int newQuarters, int newDimes, int newNickels, int newPennies )
    {

    setQuarters( newQuarters );
    setDimes( newDimes );
    setNickels( newNickels );
    setPennies( newPennies );
    }
    /** getQuarters method
    * @return number of dimes
    */
    public int getQuarters( )
    {
    if (Quarters == 0)
    {
    System.out.print ("You have no quarters.\n" );
    }
    else
    {
    return Quarters;
    }
    }

    }

    ************************************************** ************************************************** *************************

    C:\Users\Randy\Documents\Trine University\CS113 Java Programming\Chapter7Prog1RandyHaupert.java:26: error: class, interface, or enum expected
    import java.text.DecimalFormat;
    ^
    1 error

    Tool completed with exit code 1

    ************************************************** ************************************************** *************************


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to learn Java programming

    You need to put all of the import statements together at the beginning of the file.

    Each public class must go in its own file with a filename the same as the classname.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to learn Java programming

    Norm,

    Thank you for your reply. I understand about moving the import statement but I don't understand about the class and it's file name.

    Randy

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to learn Java programming

    I don't understand about the class and it's file name.
    What part? The names must be the same for a public class.
    do you know what a class name is?
    do you know what a filename is?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to learn Java programming

    I thought that the class name was the name for program like "Chapter7Prog1RandyHaupert" for the main program and "Coins" for what I would call my function. I guess I do not understand what the filename is though.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to learn Java programming

    I do not understand what the filename is
    I'm sure you know what a filename is.

    I'm taking about the name the OS gives to a file. What you see when you look at the contents of a directory.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to learn Java programming

    Ok, I think the light bulb just went on dimly, so does the main program look in the .java directory when I tell it to call the (function)(class) "Coins"

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to learn Java programming

    Your concepts seem confused.
    The code in a method (all methods are in a class) can call another method that can be in another class or in the same class.
    That has nothing to do with a .java directory.

    .java looks more like a file's extension than the name of a directory.

Similar Threads

  1. Help me to learn java
    By leecher in forum Member Introductions
    Replies: 2
    Last Post: March 17th, 2022, 03:42 AM
  2. Best Book to learn Java
    By ShadowKing98 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 1st, 2013, 06:57 AM
  3. Hello. I want to learn Java GUI.
    By Java95 in forum Member Introductions
    Replies: 2
    Last Post: January 22nd, 2012, 04:58 PM
  4. Newbie wants to learn Java
    By ahmmyreal in forum The Cafe
    Replies: 4
    Last Post: December 13th, 2011, 08:42 AM
  5. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM