Java Coin Counter Program: Counting Change with Syntax Errors Help

  • Java
  • Thread starter JaysFan31
  • Start date
  • Tags
    Java
That is, the compiler is telling you that it is expecting a close parenthesis, but it doesn't see one. It's just trying to say that there's a syntax error, and the error is likely to be in someplace that the compiler didn't expect.In summary, the conversation is about a person asking for help creating a program to count change in Java, and others providing guidance and suggestions for syntax errors in the code. The person making the program is new to Java and learning through a course at a local university.
  • #1
JaysFan31
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);
}
}
 
Technology news on Phys.org
  • #2
JaysFan31 said:
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;

dimes should be initialized to 0, not 0.0, right?
 
  • #3
JaysFan31 said:
Would someone mind helping me with some of the syntax errors I have made?
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:
  • #4
There is a ; instead of a , on the following line:

int nickels = 0; pennies = 0;
 
  • #5
Thank you for the responses. I'll make the changes and re-compile.
 
  • #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);
}
}
 
  • #7
JaysFan31 said:
Code:
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?
You need to concatenate the string with the variable total.
 
  • #8
JaysFan31 said:
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?
[snip]
System.out.println("And they are worth in cents: " total);
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.
 

1. How can I fix syntax errors in my Java Coin Counter Program?

To fix syntax errors in your Java Coin Counter Program, you should carefully review the code and look for any missing punctuation, incorrect variable names, or other mistakes. You can also use a debugger or an online code editor to help identify and fix the errors.

2. What is the purpose of a Java Coin Counter Program?

The purpose of a Java Coin Counter Program is to accurately count and calculate the total value of a given amount of change in various denominations. This can be helpful in a variety of situations, such as when counting money for a cash register or when teaching basic math skills.

3. How do I input values into the Java Coin Counter Program?

To input values into the Java Coin Counter Program, you can use the Scanner class to prompt the user for input or you can manually assign values to the variables in the code. It is important to make sure the input values are in the correct format and match the expected data type.

4. What are the potential sources of errors in a Java Coin Counter Program?

There are several potential sources of errors in a Java Coin Counter Program, including typos, incorrect syntax, incorrect data types, and logical errors in the code. It is important to carefully review and test the code to identify and fix any potential errors.

5. Can I customize the Java Coin Counter Program for different currencies?

Yes, the Java Coin Counter Program can be customized for different currencies by adjusting the denominations and values in the code. You may also need to make adjustments to the output format to display the currency symbol or use different formatting for the total value.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top