The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Setting a variable to a maximum value
Discuss Setting a variable to a maximum value in the Java Help forum on Dev Shed. Setting a variable to a maximum value Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 9th, 2008, 09:38 PM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 7
Time spent in forums: 2 h 41 m 22 sec
Reputation Power: 0
|
|
|
Setting a variable to a maximum value
Hello everyone,
I am having trouble setting a accumulated int total to not breech a particular number. I attempted to search this out on the Forums first, but I cannot come up with anything helpful...probably due to not wording it correctly no doubt!
My example:
My program asks for the user to enter a dollar amount that is to be donated toward a local charity. If the doner is a from a business organization, the donation has no limit. However, if the doner is not from a business organization, the limit he/she can donate is $50.
Here is where I am:
Code:
// Charity Donations
import java.util.Scanner;
import java.text.DecimalFormat;
public class Charity
{
public static void main(String[] args)
{
int entry;
final int max = 50;
double donation = 0.00, totDonation;
char choice;
String doner = new String();
Scanner Keyboard = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("$#.00");
do
{
System.out.println("\tCharity Donation\n\n");
System.out.println("");
System.out.print("1 - Donate: ");
System.out.print("0 - Quit: ");
entry = Keyboard.nextInt();
if (entry == 1)
{
System.out.print("\t\nEnter Doner's Name: ");
doner = Keyboard.next();
System.out.print("\t\nEnter Donation Amount: ");
donation = Keyboard.nextInt();
totDonation += donation;
System.out.print("Is Donation from a business organization? (Y/N)");
choice = Keyboard.next();
if (choice == 'N' || 'n')
{
if (donation > max)
{
donation == max;
}
}
}
else if (entry < 1 && entry > 1)
{
System.out.println("You have entered 0 to quit, have a Good Day...");
}
System.out.println(doner + " donated " + df.format(donation));
}
while (entry == 1);
System.out.println("\n Finishing Totals");
System.out.println("\n\nTotal donated dollars: " + df.format(totDonation));
}
}
...and then my error message...
Code:
~LOCATION~\Charity.java:48: not a statement
donation == max;
^
1 error
Tool completed with exit code 1
I had several errors with the code that I've worked out over the last couple of days and now I'm down to this one, and I'm not getting it...
Your thoughts would be appreciated. 
|

October 9th, 2008, 10:59 PM
|
|
|
The = and == operators are not the same. The == is a conditional/boolean operator used to compare two values. The = operator is used to declare a variable.
Example:
Code:
//Operators example
if(x == 1){
x = 2;
}
|

October 10th, 2008, 12:18 AM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 7
Time spent in forums: 2 h 41 m 22 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by miloshtw The = and == operators are not the same. The == is a conditional/boolean operator used to compare two values. The = operator is used to declare a variable.
Example:
Code:
//Operators example
if(x == 1){
x = 2;
}
|
Thank you for your response and advice. However, I'm still struggling on just how to express that if the person donating is indeed not from a company, how can I write that they can only, then, donate no more than $50.
Thanks again,
Trellotman
|

October 10th, 2008, 02:24 AM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 7
Time spent in forums: 2 h 41 m 22 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Trellotman Thank you for your response and advice. However, I'm still struggling on just how to express that if the person donating is indeed not from a company, how can I write that they can only, then, donate no more than $50.
Thanks again,
Trellotman |
Whew! Finally figured it out!
if (donation >= max)
totDonation = max;
this is what I needed. I'm not a math wiz, lol! There were other errors my posted version, but I'll post the working code tomorrow...it's late and I'm tired
Trellotman
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|