It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

Java Variable Not Initialized.

page: 1
2

log in

join
share:

posted on Sep, 21 2014 @ 01:57 PM
link   
So I was practicing for class and in our book there was an exersize about making an application that would add up your pennies nickel dimes, etc. Now (in what you are about to see) I am on dimes and am JUST NOW getting an error about the variable not being ititialized. The process worked for the coins up to dimes.

Also, at the end, how would I go about adding up all the input variables to give the final amount? Am I even doing this correctly?
Thank you.

PS this is not the homework assignment I swear lol. The homework was about finding the surface area of a circle with a radius. I already did that. This is just practice from the book that didn't give the answers -_-.

Thanks.





import java.math.MathContext;
import java.util.Scanner;


public class CoinsInAJar [

/**
* @param args
*/
public static void main(String[] args) [
System.out.println("Welcoe to this simple little application that will easily calculate the value of your coins you have."
+ "\nSimply follow through the prompts to get the total value of coins."
+ "\nJust a small tip to make things even easier. Sort your coins in different piles, pennies, nickles, dimes, and quarters, respectively.");
double pennies;
double nickel;
double dime;
double quarter;

System.out.println("Enter the amount of pennies you have ");
Scanner userPenny = new Scanner(System.in);
pennies = userPenny.nextInt();
pennies = pennies * .01;
System.out.println("Your total value of pennies you have " + pennies);

System.out.println();
System.out.println("Enter how many nickels you have: ");
Scanner userNickel = new Scanner(System.in);
nickel = userNickel.nextInt();
nickel = nickel * .05;
System.out.println("The total amount of nickels you have is " + nickel);

System.out.println("Please enter how many dimes you have ");
Scanner userDimes = new Scanner(System.in);
dime = (dime * .10);
System.out.println("Your total amount in dimes is " + dime);





]

]




posted on Sep, 21 2014 @ 02:06 PM
link   
dime = userDimes.nextInt();
is missing



posted on Sep, 21 2014 @ 02:37 PM
link   
I'm no coding expert. Just observing what you have shown us. But besides chefslug's note (which makes sense to me), I'm wondering about this:
You have
pennies = pennies * .01;

nickel = nickel * .05;


But for dimes, you have

dime = (dime * .10);


So, two queries. If you're using pennies *, shouldn't you also use nickels * (ie, not nickel *)? And if you're using the same process throughout, that line for dimes [ after dime = userDimes.nextInt(); ] maybe should be
dime = dimes * .10;

Don't shoot the messenger! Just asking!


ETA: BTW, you've got a typo. It obviously won't affect the program running (I know that much, at least!
) but naturally you'll want to avoid typos.
This line near the start:
System.out.println("Welcoe to this simple little application that will easily calculate the value of your coins you have."

Fix Welcoe to Welcome.


If you don't mind a couple of technical corrections, "the value of your coins you have" is basically a tautology. This is because "your coins" are the coins you have. So, it would read better if you either said "the value of the coins you have" or (simpler and cleaner), just wrote "the value of your coins".

The second technical point is that you have
"Enter the amount of pennies you have "
"Enter how many nickels you have: "
"Please enter how many dimes you have "

Okay, you can vary the message. You're writing it, after all. But I'd ditch the colon in the "nickels" instruction. Either that or add it into the other ones.

Then (last point) :
"Your total value of pennies you have "
"The total amount of nickels you have is "
"Your total amount in dimes is "

Again, it's just the variation. If you prefer it, cool. Not mentioning it to annoy you. Just pointing it out in case you want to revise it. If you don't, it's no biggie.


edit on 21/9/14 by JustMike because: (no reason given)



posted on Sep, 21 2014 @ 02:53 PM
link   
Ah I feel like an idiot now lol. I didn't realize I forgot that line of code lol. We are about a month into the class lol. I'm not sure if I understand it or not yet. Like I understand it to read it, but to sit down and program it I get all mixed up and forgetful. Others seem to have no problem. -_-. Kinda frustrating.

Yeeeaaahh, lol I just noticed the typos as well, thank you for helping me with them. After the program is finished I am going to go through and fix those typos and make the variables more consistent. Thank you for pointing those out for me
. You guys are the best!!!



posted on Sep, 21 2014 @ 02:59 PM
link   
a reply to: wwe9112
No worries. Glad to contribute something that might help out a person who's making an effort to learn stuff. You're doing something positive and it's great to see.

More power to you.



new topics

top topics
 
2

log in

join