Ok, here is the problem. I have to ask a group of people for donations and get the total average and so on. The only problem I have is trying to get the lowest donation from the inputted numbers. I can't seem to get the actual lowest number. It just keeps getting the last "lowest" number. Any help would be appreciated.

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.lang.*;
public class prog2 
{
    public static void main(String[] args)
    {
    	DecimalFormat df2 = new DecimalFormat( "$#,###.00" );
    	DecimalFormat df = new DecimalFormat( "#,###.00" );
    	int donor = 0; //single donor
    	int totalD = 0; //total donors 
    	double money2 = 0; //Temp variable for saving the previous "money" input
    	double totalM = 0; //Money Total
    	double moneyHighest = 0; //Money Highest
    	double moneyLowest = 0; //Money Lowest
    	double money = 0; //original money
    	System.out.printf("\t%5s%20s%20s\n", "Name", "Amount", "Branch"); //print table names
    	while(donor != -1)
    	{
    		String name = JOptionPane.showInputDialog( null, "Enter your last name (Maximum of 20 letters)"); //name of donor
    		if (name.length() >= 20) //making sure lenght of name is not too long
    		{
    			JOptionPane.showMessageDialog(null, "Too long");
    			break;
    		}
    		else
    		{
    			money2 = money; //storing the previous money value
    			String moneyString = JOptionPane.showInputDialog( null, "Enter amount of money given"); //money input
    			money = Double.parseDouble(moneyString); //money
    			double moneyL = Math.min(money, money2);
    			moneyLowest = Math.min(moneyL, money);
    			if(moneyHighest < money)
    			{
    				moneyHighest = money;
    			}
    		}
    		String branchString = JOptionPane.showInputDialog( null, "Enter your branch (between 1 and 150)"); //branch input
    		int branch = Integer.parseInt(branchString); //branch
    		if (branch < 0 || branch > 151) //makes sure branch input is between 1 and 150
    		{
    			JOptionPane.showMessageDialog(null, "Wrong Input");
    			break;
    		}
    		else    		
    		{
    			System.out.printf("%-20s%s%-25s%-1s\n", name, "$", df.format(money), branch); //print out name, amount, and branch code
    			String donorS = JOptionPane.showInputDialog(null, "Enter 1 for another donor, or -1 to end the program"); //1 to keep going, -1 to end
    			donor = Integer.parseInt(donorS); //donors
    			totalD++;  //total donors
    			totalM += money; // get total money from the money added together		
    		}
    	}
    	double average = (totalM / totalD); // average of the Money divided by the number of donors
    	System.out.println(":");
    	System.out.println(":");
    	System.out.println("There are " + totalD + " donors.");//print total donors
    	System.out.println("The average is " + df2.format(average)+".");//print money average
    	System.out.println("The total is " + df2.format(totalM)+ ".");//print total money
    	System.out.println("The lowest is " + df2.format(moneyLowest) + ".");//print Lowest money amount only if there is more than 1 input
    	System.out.println("The highest is " + df2.format(moneyHighest) + "."); //print Highest money amount, if only one input, it is automatically highest
    }
}

It just keeps getting the last "lowest" number.

What does that mean? From what I see there are no issues...

Say they input is:


$20
$5
$200
$10

It would print $10 as the lowest number.

It need some sort of code after the

if(moneyHighest < money)
    			{
    				moneyHighest = money;
    			}

(This gives highest input)


I took out the waste that was causing the problem but now I'm stuck what to put in to actually receive the lowest number since the variable of "money" is constantly changing each time around in the loop.

ok, I think I see the issue:

money2 = money; //storing the previous money value
String moneyString = JOptionPane.showInputDialog( null, "Enter amount of money given"); //money input
money = Double.parseDouble(moneyString); //money
double moneyL = Math.min(money, money2);
moneyLowest = Math.min(moneyL, money);

why are you doing Math.min here and not using what you do for highest? there is actually nothing wrong with using Math.min, but this code is making it impossible for moneyLowest to be anything other than the current user imput (see if you can see why).

I believe this is what is causing it

String moneyString = JOptionPane.showInputDialog( null, "Enter amount of money given"); //money input
money = Double.parseDouble(moneyString);

But I have no idea how to store the input in separate variables, or what I even need to do to prevent this.
I went ahead and put in what I had for the highest but the still gives the same results just replacing the moneyHighest with moneyLowest.
I also took out the Math.min.

no, that is not what is causing your troubles. what you are trying to do is exactly the same as storing the highest value.

i.e.

if(moneyLowest > money) {
  moneyLowest = money;
}

but simply copy & pasting this code will not work.

Well this puts me back at square one again.
Of course moneyLowest = money is going to set moneyLowest at the current input but how do I go back to the previous numbers from the earlier inputs. I can't find any possible way to do this.
Maybe running 2 more if statements saying if it is > or < but I will still be stuck with the "money" variable.

how did you work out how to do the highest? it is exactly the same...

surely the condition of moneyLowest > money stops moneyLowest to be the current user input (unless infact it is the lowest). one condition is enough to make this work.

"how do I go back to the previous numbers from the earlier inputs" do you do this for moneyHighest? if yes/no how do you make lowest do the same?

Oh, well I give up. I'll take the 95% Its due tomorrow. Thanks for your help though. Ive got to turn in for the day.

... if you initialise your moneyLowest to a large number (just choose some max), can you simply use the code I posted earlier.

e.g. moneyLowest = 1000

the user then start donating money, and so this 1000 should get overwritten the first time a deposit is made (since this is a MAX value). with the condition you introduce, only smaller subsequent values are stored and so there is no need to "remember" any previous value...

as i said, this is exactly what you do with highest, with a greater than symbol, not less than...

This article has been dead for over six months. Start a new discussion instead.