3 Replies - 706 Views - Last Post: 21 January 2010 - 05:34 PM Rate Topic: -----

#1 UrbanTwitch   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 233
  • Joined: 27-September 09

Java Methods Incorrect.

Posted 21 January 2010 - 04:06 PM

OK so My code works. But after the first popup box. I press OK and then it keeps poping up. It's suppose to put another pop up box.

/**
Program name: Comissions Calculator
Programmer: ************
Date: January 21, 2010
**/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class comCalc
{

		public static double getSales()
		{
			double sales = 0.0;
			boolean done = false;


			while(!done)
					{
			String answer = JOptionPane.showInputDialog(null, "Enter the sales amount\n(do not use commas or dollar signs)\n or  click Cancel to exit:");

			if (answer == null) finish();


			try
			{
				sales = Double.parseDouble(answer);
				if (sales <= 0) throw new NumberFormatException();
			}
			catch (NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.", "Error!!", JOptionPane.INFORMATION_MESSAGE);
			}
					}
			return sales;
	}

	public static void finish()
	{
		System.out.println("Goodbye!");
			System.exit(0);
	}

	public static void output(double comission, double sales)
	{
		DecimalFormat twoDigits = new DecimalFormat("$#,000.00");

		JOptionPane.showMessageDialog(null, "Your Comission on sales of "+ twoDigits.format(sales) + " is " + twoDigits.format(comission),"Comission Totals", JOptionPane.INFORMATION_MESSAGE);
	}


	public static double getComm(double employeeSales, int employeeCode)
	{
		double comission = 0.0;

		switch(employeeCode)
		{
			case 1:
			comission = .10 * employeeSales;
			break;

			case 2:
			comission = .14 * employeeSales;
			break;

			case 3:
			comission = .18 * employeeSales;
			break;
		}
		return comission;
	}



	public static int getCode()
	{
		int code = 0;
		boolean done = false;


			try
			{
				String message = "Enter the Comission code:\n\n1) Telephone Sales \n2) In-Store Sales \n3) Outside Sales\n\n";

				code = Integer.parseInt(JOptionPane.showInputDialog(null,message));

				if (code < 1 || code > 3) throw new NumberFormatException();
				else done = true;
			}
			catch (NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null,"Please enter 1, 2, or 3.", " Error", JOptionPane.INFORMATION_MESSAGE);
			}

		return code;
	}

		public static void main(String[] args)
		{
			double dollars, answer;
			int empCode;

			dollars = getSales();
			empCode = getCode();
			answer = getComm(dollars,empCode);
			output(answer,dollars);
			finish();

	}
}


What is wrong with my code?

Thanks for reading!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Methods Incorrect.

#2 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java Methods Incorrect.

Posted 21 January 2010 - 04:10 PM

Since answer is a String, you might want to try using answer.equals(null) instead of answer == null as .equals() checks for equality, == checks for same location in memory

edit: nevermind, I didn't think that one through too well :)

This post has been edited by erik.price: 21 January 2010 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Java Methods Incorrect.

Posted 21 January 2010 - 04:13 PM

You shouldn't be throwing an exception in a try block, especially when the catch block will catch it. This is bad practice. Instead, let's focus more on our loop:
String input = "";
boolean done = false;
double x = -1.0; //because we don't want numbers < 0, so it will default to wrong input

do{
	input = JOptionPane.showInputDialog("Enter a number");
	done = (input != null); //validate that input isn't null

	if(input == null) finish();

	try{
		x = Double.parseDouble(input);
	}	
//flag that you have invalid input if parse fails
	catch(NumberFormatException e){done = false;} 

	if(x < 0) done = false; //or if number is < 0

}while(!done);



@Erik.price: Actually, the == operator will work fine (and is preferred) when testing Objects to see if they are null pointers.

This post has been edited by macosxnerd101: 21 January 2010 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 UrbanTwitch   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 233
  • Joined: 27-September 09

Re: Java Methods Incorrect.

Posted 21 January 2010 - 05:34 PM

I just figured it out. During the while(!done) loop. I never change done to true, so I stay in that loop forever. So I add "done = true" above "return sales;" and it works! :)

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1