Java Coin Counter Program: Counting Change with Syntax Errors Help

  • Context: Java 
  • Thread starter Thread starter JaysFan31
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around a Java program intended to count different denominations of coins (pennies, nickels, dimes, quarters). Participants are addressing syntax errors encountered during compilation, with a focus on correcting these issues and understanding compiler messages.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the variable for dimes should be initialized to 0 instead of 0.0.
  • Another participant suggests that it would be helpful to see the specific compiler errors to assist in identifying mistakes.
  • A participant points out a syntax error involving a semicolon instead of a comma in the variable declaration line.
  • A later reply mentions multiple changes made to the code, including correcting the initialization of dimes and changing the input type for pennies.
  • One participant reports a persistent compiler error related to the syntax of a print statement, specifically regarding string concatenation with a variable.
  • Another participant advises that the string needs to be concatenated with the variable total to resolve the syntax error.
  • A further comment suggests that the error might be due to a misplaced syntax in the print statement, indicating a potential misunderstanding of how to format the output correctly.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct syntax errors, but there is no consensus on the best approach to resolve the specific compiler error related to the print statement.

Contextual Notes

Participants express uncertainty regarding the exact nature of the compiler errors and the implications of their changes, indicating a reliance on trial and error to identify solutions.

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
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?
 
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:
There is a ; instead of a , on the following line:

int nickels = 0; pennies = 0;
 
Thank you for the responses. I'll make the changes and re-compile.
 
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);
}
}
 
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.
 
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.
 

Similar threads

Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K