PDA

View Full Version : I admit Defeat...




Hylekmi
Jan 19, 2012, 01:13 AM
I am trying to write a simple currency conversion program that converts a US dollar to a Euro,pound,and peso. For the life of me I cannot get the US dollar Amount to multiply to a pound(or anything else for that matter.:mad: HELP!!!


import java.util.Scanner;

public class CurrencyConverter
{
public static void main (String[]args)
{
Scanner console = new Scanner (System.in);

int dollar;
boolean poundAnswer;
double number;
double pound;
//double poundAnswer;

dollar = 1;
pound = .65;
poundAnswer = (pound*dollar);




System.out.println (" Please enter a United States Dollar amount to be converted.");
poundanswer= console.nextboolean();

if (true)
{
System.out.println (" The dollar equivalent of "+dollar+" is equal to:");
System.out.println( + poundAnswer + "pounds");
}//end dollar
}//end main
}//end Public Class.



chrono1081
Jan 19, 2012, 01:53 AM
I am trying to write a simple currency conversion program that converts a US dollar to a Euro,pound,and peso. For the life of me I cannot get the US dollar Amount to multiply to a pound(or anything else for that matter.:mad: HELP!!!


import java.util.Scanner;

public class CurrencyConverter
{
public static void main (String[]args)
{
Scanner console = new Scanner (System.in);

int dollar;
boolean poundAnswer;
double number;
double pound;
//double poundAnswer;

dollar = 1;
pound = .65;
poundAnswer = (pound*dollar);




System.out.println (" Please enter a United States Dollar amount to be converted.");
poundanswer= console.nextboolean();

if (true)
{
System.out.println (" The dollar equivalent of "+dollar+" is equal to:");
System.out.println( + poundAnswer + "pounds");
}//end dollar
}//end main
}//end Public Class.

Um...I don't know Java but your logic looks completely wrong. I'll write some pseudocode for you that hopefully you can convert into Java.

//Declare variables
int dollar;
boolean poundAnswer;
double number;
double pound;
double poundAnswer; //This should not be commented out since you are using it below.

//Assign variables a value
dollar = 1;
pound = 0.65;
poundAnswer = (pound*dollar);

//Get input from user (this is the part that looks VERY wrong!)
System.out.println (" Please enter a United States Dollar amount to be converted.");
poundanswer= console.nextboolean();

//I'd do something like this:
("Please enter a United States Dollar amount to be converted:");
dollar = console input; //I'm not sure how this is in Java, but it assigns whatever number the user enters to dollar;

//Output the answer to the screen:
("Your ___ dollars equals ____ pounds") //The first blank you display your dollar variable, the second one you display the poundAnswer variable.)

Hopefully someone with Java experience can chime in.

dylanryan
Jan 19, 2012, 02:05 AM
There are several issues.

Your main issue besides logic (as pointed out above) is that you define poundAnswer as a boolean. This means it can hold only values of true or false. Then you try to store other values to it.

Like this line:


poundAnswer = (pound*dollar);


I forget how java works, but that probably will set poundAnswer to true. Whereas you probably want it to equal something else.


Later, you have this line:

poundanswer= console.nextboolean();

First off: You called the variable poundAnswer (with a capital A). That matters, poundanswer and poundAnswer are not the same variable, so that will probably break compilation.

Second: Why are you reading a boolean? You seem to want to be reading a number (such as a double), not a boolean (true/false only). And... you read the number then print it right back out without doing any multiplication on it. So, you overwrite the previous value (probably true) with a new value and then print the new value unchanged. You probably want to multiply after you read a value (and reading Booleans isn't what you want anyway).

Hylekmi
Jan 19, 2012, 02:10 AM
As you can probably tell, I'm exhausted. this is due at 10am and i still have another program to write. I know that this is a horrible horrible mess, and actually i fixed most of it. I'm just going to give up and hopefully my professor will give me an extension. Thanks for the help!

chrono1081
Jan 19, 2012, 08:01 PM
As you can probably tell, I'm exhausted. this is due at 10am and i still have another program to write. I know that this is a horrible horrible mess, and actually i fixed most of it. I'm just going to give up and hopefully my professor will give me an extension. Thanks for the help!

Do you plan your programs on paper first? If not its a good habit to get in to, even on small programs because it really helps spot logic errors.

Start with something like this:

1. Display message to user
2. Ask for user input

3. Receive user input and store in variable
4. Process user input (this is where you do the dollars to pounds conversion)

5. Display result to user (this is where you display the actual conversion)

Once you break it down in to these steps, then its much easier to code the individual pieces and your program will feel a lot less like guesswork and more like you are designing it to do what you want it to do.

Keep at it though. Programming is hard, especially in the beginning. It'll come though and with practice you will get much better at it.

Ap0ks
Jan 20, 2012, 03:09 AM
Also you need to make sure your method calls are correct, Java is case-sensitive so console.nextboolean() needs to be console.nextBoolean() for it to work.

Although you probably want it to be console.nextDouble() ;)

Mac_Max
Jan 20, 2012, 10:55 AM
Since your assignment has already been collected I don't mine correcting your assignment.


import java.util.Scanner;

public class CurrencyConverter
{
public static void main (String[]args)
{
Scanner console = new Scanner (System.in);

int dollar;
//boolean poundAnswer;
double number;
double pound;
//you had it right in the first place.
double poundAnswer;

dollar = 1;
pound = .65;
//this is too early. You have to do this after you have input, and this logic won't work in the first place anyways
//poundAnswer = (pound*dollar);




System.out.println (" Please enter a United States Dollar amount to be converted.");
//you're inputting a double so you use nextDouble. Also you can do error checking here
if (console.hasNextDouble()){
poundAnswer= console.nextDouble();
} else {
//Print an error message, ask for sane input, and/or exit the program
}

//Now we do our math
poundAnswer = poundAnswer * pound;

//This isn't correct.
//System.out.println (" The dollar equivalent of "+dollar+" is equal to:");
//System.out.println( + poundAnswer + "pounds");


System.out.println (" The dollar equivalent of "+dollar+" is equal to:");
System.out.println( poundAnswer + " pounds");
}//end main
}//end Public Class.


I haven't tried compiling it but it should work. My advice would be to pick up a good beginner Java book and work from that to learn the language. I can't stand textbooks and hate trying to learn from them. You might have better luck with a traditional Java book.

HippieMagic
Jan 20, 2012, 11:37 AM
Check out this guy for some decent "hands on" stuff you can follow along with:

http://www.youtube.com/user/thenewboston?blend=1&ob=4

You won't learn a TON this way alone but it will help get you used to typing it and you will definitely learn the basics which is important. He will most likely move a little too fast.

I have just really started to learn Java as well and it can be pretty frustrating at times. Two books that I actually like are Core Java Volume I and II. They won't teach you how to program but they will teach you pretty much everything there is to know about Java and they have a ton of examples you can type out or download. If you are just learning I think you really should type everything even if you don't have to though.

Hylekmi
Jan 20, 2012, 11:55 AM
Thanks for the help guys. It turns out that i had it right the first time that i did it. I kept getting an error when i compiled it so i figured i had to change it. I know what i did wrong and i fixed it. Thanks!