Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

New to java

  1. May 7, 2007 #1
    Hi. I'm completely new to Java, but I'm taking a course at a local university. I was wondering if someone could help me with this.

    I need to create a program that counts change (pennies, nickels, dimes, quarters).

    I've only been taking the class for about a week and am still very inexperienced when it comes to programming (and more importantly spotting errors). I modeled this program after a very similar one in my textbook. However, I cannot compile it using Dr. Java. Would someone mind helping me with some of the syntax errors I have made? Any help would be appreciated.


    import java.util.Scanner;

    public class CoinCounter {

    public static void main(String[] args) {

    // we define the variables that we need for our calculations
    int quarters = 0, dimes = 0.0;
    int nickels = 0; pennies = 0;
    int total, num;

    // we read in the values
    Scanner scan = new Scanner(System.in);

    System.out.println("How many quarters do you have:");
    int quarters = scan.nextInt();
    System.out.println("How many dimes do you have:");
    dimes = scan.nextInt();
    System.out.println("How many nickels do you have:");
    nickels = scan.nextInt();
    System.out.println("How many pennies do you have:");
    pennies = scan.nextDouble();

    // we perform the calculations
    quarters + dimes + nickels + pennies = num;

    total = 25 * quarters;
    total = total + 10 * dimes;
    total = nickels * 5 + total;
    total = total + pennies;

    // print the result
    System.out.println("The number of coins you have is "
    + num);
    System.out.println("And they are worth in cents: " total);
    }
    }
     
  2. jcsd
  3. May 7, 2007 #2

    nrqed

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    dimes should be initialized to 0, not 0.0, right?
     
  4. May 7, 2007 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It would help if you posted the errors. We could compile it ourselves and see what our compilers say and point out your mistakes, but that doesn't help us help you learn how to read compiler output messages!
     
    Last edited: May 7, 2007
  5. May 7, 2007 #4
    There is a ; instead of a , on the following line:

    int nickels = 0; pennies = 0;
     
  6. May 7, 2007 #5
    Thank you for the responses. I'll make the changes and re-compile.
     
  7. May 7, 2007 #6
    OK. I made 4 changes.
    I changed the dimes to just 0
    I changed the ; after nickels = 0 to ,
    I changed the scan.nextDouble for pennies to scan.nextInt
    I changed quarters + dimes + nickels + pennies = num; to num = quarters + dimes + nickels + pennies;

    I still get the following compiler error however:
    Line 35 "System.out.println("And they are worth in cents: " total);
    Error: ')' expected
    Could someone explain?


    import java.util.Scanner;

    public class CoinCounter {

    public static void main(String[] args) {

    // we define the variables that we need for our calculations
    int quarters = 0, dimes = 0;
    int nickels = 0, pennies = 0;
    int total, num;

    // we read in the values
    Scanner scan = new Scanner(System.in);

    System.out.println("How many quarters do you have:");
    int quarters = scan.nextInt();
    System.out.println("How many dimes do you have:");
    dimes = scan.nextInt();
    System.out.println("How many nickels do you have:");
    nickels = scan.nextInt();
    System.out.println("How many pennies do you have:");
    pennies = scan.nextInt();

    // we perform the calculations
    num = quarters + dimes + nickels + pennies;

    total = 25 * quarters;
    total = total + 10 * dimes;
    total = nickels * 5 + total;
    total = total + pennies;

    // print the result
    System.out.println("The number of coins you have is "
    + num);
    System.out.println("And they are worth in cents: " total);
    }
    }
     
  8. May 7, 2007 #7
    You need to concatenate the string with the variable total.
     
  9. May 7, 2007 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    One likely elaboration is that somewhere in this line, there is a syntax error, and the error occurs someplace where it wouldn't be too unreasonable to expect a close parenthesis.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: New to java
  1. JAVA GridLayout (Replies: 1)

  2. Java interpreter (Replies: 8)

  3. I'm new to Java (Replies: 1)

  4. Java Cylinders (Replies: 2)

Loading...