How does this java program look?

Status
Not open for further replies.

mypcbroke

Baseband Member
Messages
49
Hey guys I'm just getting my feet wet with java and I made this program that asks you how much change you have and then adds them up and tells you how much money you have in dollars. Any tips on the style or in general on this? It runs fine but I don't know if it's coded correctly.
Code:
import java.util.Scanner;

class apples{
	public static void main(String[] args){
		Scanner change = new Scanner(System.in);
		
		
		double quarters;
		double dimes;
		double nickels;
		double pennies;
		double total;
		
		System.out.println("How many quarters do you have?");
		quarters = .25 * change.nextInt();
		
		System.out.println("How many dimes do you have?");
		dimes = .1 * change.nextInt();
		
		System.out.println("How many nickles do you have?");
		nickels = .05 * change.nextInt();
		
		System.out.println("How many pennies do you have?");
		pennies = .01 * change.nextInt();
		
		
		total = (quarters + dimes + nickels + pennies)/1.;
		System.out.println("You have $" +total);
			
			
		}
		
	
	
			
		}
 
the program looks pretty good. I have taken a few java classes and written a good number of programs in java. Your program is neat and styled well. I can't see anything I would change personally, but as I have noticed many people have there own "method" of how things "must be styled." I don't.
 
You might want to add some error checking. What would happen if someone typed in the word TEN for any of the inputs instead of the number 10?
 
You might want to add some error checking. What would happen if someone typed in the word TEN for any of the inputs instead of the number 10?
Haha, well...I have no idea. How would I go about doing that? I'm really new to this stuff.
 
There's no point in listing each of your double variable declarations separately, it's just as easy to understand this
double quarters, nickels, pennys, etc;

To added error checking you need some try/catch lines, explained well with a few examples here

Also, to make this more fun, you could use an array instead of so many doubles. And put it in a loop, why not :p

Code:
static String[] strArray = {"quarters","dimes","nickels","pennies"};
int[] intArray = new int[4];
for (int x = 0; x < intArray.length; x++)
{
                System.out.println("How many " strArray[x] " do you have?");
		intArray[x] = .25 * change.nextInt();
}
for (int i=0; i < intArray.length; i++)
                int total += intArray[i];
out.println("You have $" total);

with error checking

Code:
static String[] strArray = {"quarters","dimes","nickels","pennies"};
int[] intArray = new int[4];
try
{
     for (int x = 0; x < intArray.length; x++)
     {
                System.out.println("How many " strArray[x] " do you have?");
                intArray[x] = .25 * change.nextInt();
     }
} catch ( putexceptionnamehere randomVariable) {
                    out.println("Whoops error lol");
                    }

for (int i=0; i < intArray.length; i++)
                int total += intArray[i];
out.println("You have $" total);
 
Overall it's really good, I really wouldn't bother with error checking at this stage but apples should be Apples. You could also get rid of 4 out of your 6 variables with something like this.
Code:
/*
 * Total monetary value in pennies.
 */
int total = 0;

System.out.println("How many quarters do you have?");
total += (25 * change.nextInt());

System.out.println("How many dimes do you have?");
total += (10 * change.nextInt());

System.out.println("How many nickles do you have?");
total += (5 * change.nextInt());

System.out.println("How many pennies do you have?");
total += (change.nextInt());
 
The scanner has methods built-in for input validation so combining kmote's code with that you get something like this:

Code:
/*
 * Total monetary value in pennies.
 */
int total = 0;


System.out.println("How many quarters do you have?");

while (!change.hasNextInt()) {
   System.out.println("Must be a number!");
   change.next(); 
   }

total += (25 * change.nextInt());

System.out.println("How many dimes do you have?");

while (!change.hasNextInt()) {
   System.out.println("Must be a number!");
   change.next(); 
   }

total += (10 * change.nextInt());

System.out.println("How many nickles do you have?");

while (!change.hasNextInt()) {
   System.out.println("Must be a number!");
   change.next(); 
   }

total += (5 * change.nextInt());

System.out.println("How many pennies do you have?");

while (!change.hasNextInt()) {
   System.out.println("Must be a number!");
   change.next(); 
   }

total += (change.nextInt());

BTW, SOULphIRE's example has a "logical error" in it so that it doesn't calculate the total correctly. Might be a good exercise for the OP to figure out what it is.
 
Ah yeah spotted it :p lol and also it's 12:15AM here and for some reason I'm at a birthday party and for some reason I'm coding and checking tech forums lol
 
That's what happens when you try to do too many things at once!

Personally, I never make mistakes. I thought I did once but I was wrong. :)
 
Status
Not open for further replies.
Back
Top Bottom