8 Replies - 221 Views - Last Post: 14 September 2012 - 03:25 AM Rate Topic: -----

#1 JMurray308  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-September 12

my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 12:23 AM

import javax.swing.JOptionPane;


public class shippingCharges
{
	public static void main(String[] args)
	{
	double pounds, miles, shippingCharges;
	String input;
	//get the weight of package
	input = JOptionPane.showInputDialog("Enter the weight in pounds of the item to be shipped:  ");
	pounds = Double.parseDouble(input);
	
	//Get the miles item is to be shipped
	input = JOptionPane.showInputDialog("Enter the miles item is to be shipped:  ");
	miles = Double.parseDouble(input);
	
	//determine the shippingCharges
	if (pounds <=2)
	{
		if(miles <=500)
		{
			JOptionPane.showMessageDialog(null, "The shipping charge is $1.10");
		}
		else 
		{
			if (pounds<=2)
			{
				if(miles <=1000)
				{
					JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
					}
					else 
					{
					    if (pounds <=2)
						 {
						 	if(miles <=1500)
							{
								JOptionPane.showMessageDialog(null, "The shipping charge is $3.30");
								}
								else 
								{
									if (pounds <=2)
									{
									if (miles <=2000)
									 {
				                   JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
										 }
										 else
										 {
										    if (pounds  <=6)
											 {
											 	if(miles <=500)
												{
													JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
													}
													else
													{
														if (pounds <=6)
														{
															if (miles <=1000)
															{
																JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
																}
																else 
																{
																	if (pounds <=6)
																	{
																		if (miles <=1500)
																		{
																		JOptionPane.showMessageDialog(null, "The shipping charge is $8.80");
																		}
																		else
																		{
																			if (pounds <=6)
																			{
																				if (miles <=2000)
																				{
																				JOptionPane.showMessageDialog(null, "The shipping charge is $13.20");
																				}
																				else
																				{
																					if (pounds <=10)
																					{
																						if (miles <=500)
																						{
																						JOptionPane.showMessageDialog(null, "The shipping charge is $3.70");
																						}
																						else
																						{
																							if (pounds <=10)
																							{
																								if (miles <=1000)
																								{
																								JOptionPane.showMessageDialog(null, "The shipping charge is $7.40");
																								}
																								else
																								{
																									if (pounds <=10)
																									{
																										if (miles <=1500)
																										{
																										JOptionPane.showMessageDialog(null, "The shipping charge is $11.20");
																										}
																										else 
																										{
																										if (pounds > 10)
																											{
																												if (miles <=500)
																												{
																												JOptionPane.showMessageDialog(null, "The shipping charge is $3.80");
																												}
																												else
																												{
																												if (pounds > 10)
																													{
																														if (miles <=1000)
																														{
																														JOptionPane.showMessageDialog(null, "The shipping charge is $7.60");
																														}
																														else
																														{
																														if (pounds > 10)
																														   {
																																if (miles <=1500)
																																{
																																JOptionPane.showMessageDialog(null, "The shipping charge is $11.40");
																																}
																																else
																																{
																																if (pounds > 10)
																																{
																																	if (miles <=2000)
																																	{
																																	JOptionPane.showMessageDialog(null, "The shipping charge is $15.20");
																																	}
																																}
																														}
																													}
																												}
																											}
																										}
																									}
																								}
																							}
																						}
																					}
																				}
																			}
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			
			System.exit(1);
		}
		
	}																						
																												



MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 13 September 2012 - 03:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: my code only partially functions thru first 4 statements, then flops.

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 12:51 AM

JMurray308, do you mind editing your post and put the codes inside code tags? The code is totally unreadable without the proper indentations. What is your program supposed to do and what problems are you facing?
Was This Post Helpful? 0
  • +
  • -

#3 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 201
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 12:58 AM

hello there,

First, use [ code]//Mycode [ /code] (without the space inbetween the brackets) because it is almost unreadable now.

Anyways, I tried formatting your code a little bit and I see this:
import javax.swing.JOptionPane;

public class shippingCharges
{
	public static void main(String[] args)
	{
		double pounds, miles, shippingCharges;
		String input;
		//get the weight of package
		input = JOptionPane.showInputDialog("Enter the weight in pounds of the item to be shipped: ");
		pounds = Double.parseDouble(input);

		//Get the miles item is to be shipped
		input = JOptionPane.showInputDialog("Enter the miles item is to be shipped: ");
		miles = Double.parseDouble(input);

		//determine the shippingCharges
		if (pounds <=2)
		{
			if(miles <=500)
			{
				JOptionPane.showMessageDialog(null, "The shipping charge is $1.10");
			}
			else
			{
				if (pounds<=2)
				{
					if(miles <=1000)
					{
						JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
					}
					else
					{
						if (pounds <=2)
						{
							if(miles <=1500)
							{
							JOptionPane.showMessageDialog(null, "The shipping charge is $3.30");
							}
							else
							{
								if (pounds <=2)
								{
									if (miles <=2000)
									{
										JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
									}
									else
									{
										if (pounds <=6)
										{
											if(miles <=500)
											{
												JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
											}
											
											//This continues for a while 
		//...
		} //Matches with the first if of the <= 2 pounds condition



Can you deduce what happens when the user insert a number higher than 2 pounds? The reason why (it porbably) "the program does not work" is because all the ifs and elses are encapsulated in that single if statement. (Although I am not 100% sure because the formatting is so chaotic without the code blocks)

I suggest making a logical ranking based on either weight or miles. Try drawing it out how the shipping cost should look like when you sort on weight or distance first. Then try to translate it in Java ;)

Spoiler

This post has been edited by karabasf: 13 September 2012 - 01:09 AM

Was This Post Helpful? 1
  • +
  • -

#4 DavsX  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 23
  • Joined: 23-August 12

Re: my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 01:02 AM

First, format your code. Use tabs, intendation.
then, when you post a code, use the [ code ][ /code ] tags
Third, you use toooo many if/else checks. you check the same thing over and over again.
Instead of:
		if(miles < 500){
			if(something < 1000) {
				dothis ;
			}
		}
		if(miles < 500){
			if(something < 2000) {
				dothat ;
			}
		}


use:
		if(miles < 500){
			if(something < 1000) {
				dothis ;
			}
			else{
				if(something < 2000) {
					dothat ;
				}
			}
		}


or even:
		if(miles < 500){
			if(something => 0 && something < 1000) {
				dothis ;
			}
			if(something => 1000 && something < 2000) {
				dothat ;
			}
		}


Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,516
  • Joined: 23-August 08

Re: my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 03:13 AM

Jeez, is it really so hard to use code tags???

:code:
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 02:29 PM

You repeat many many times

if (pounds<=2)

when you are inside the first if(pounds <= 2) you do not need to check that condition again

	//determine the shippingCharges
	if (pounds <=2)
	{
		if(miles <=500)
		{
			JOptionPane.showMessageDialog(null, "The shipping charge is $1.10");
		}
		else
		{
//			if (pounds<=2)
//			{
				if(miles <=1000)
				{
					JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
				}
				else
				{
//					if (pounds <=2)
//					{
						if(miles <=1500)
						{
						   JOptionPane.showMessageDialog(null, "The shipping charge is $3.30");
							}
							else
							{
//								if (pounds <=2)
//								{
									if (miles <=2000)
									{
										JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
									}
									else
									{
										if (pounds <=6)


Was This Post Helpful? 0
  • +
  • -

#7 metalBrylez  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 21-May 12

Re: my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 06:05 PM

im confused, but here you go
import javax.swing.JOptionPane;

public class shippingCharges {
	public static void main(String[] args) {
	double pounds, miles, shippingCharges;
	String input;
	//get the weight of package
	input = JOptionPane.showInputDialog("Enter the weight in pounds of the item to be shipped:  ");
	pounds = Double.parseDouble(input);
	
	//Get the miles item is to be shipped
	input = JOptionPane.showInputDialog("Enter the miles item is to be shipped:  ");
	miles = Double.parseDouble(input);
	
	//determine the shippingCharges
	if(pounds <= 2) {
		if(miles <=	500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $1.10");
		} else if(miles <=	1000){
			JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
		} else if(miles <=	1500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $3.30");
		} else(miles <=	2000) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
		}
	}
		
	} else if(pounds <= 6) {
		if(miles <= 500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
		} else if(miles <= 1000) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
		} else if(miles <=1500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $8.80");
		} else if(miles <=2000) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $13.20");
		}
	}
																			
	} else if(pounds <=10) {
		if (miles <=500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $3.70");
		} else if(miles <=1000) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $7.40");
		} else(miles <=1500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $11.20");
		}
	}
		
	} else if(pounds > 10) {
		if (miles <=500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $3.80");
		} else if(miles <=1000) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $7.60");
		} else if(miles <=1500) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $11.40");
		} else if(miles <=2000) {
			JOptionPane.showMessageDialog(null, "The shipping charge is $15.20");
		}
	}
	} else {
		System.exit(1);
	}
}	
																												

Was This Post Helpful? 0
  • +
  • -

#8 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 201
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: my code only partially functions thru first 4 statements, then flops.

Posted 13 September 2012 - 11:58 PM

One: don't jsut give code to the OP. You just gave him something to work with without having learned anything at all!

Second: Your piece of code is far from flawless. Just to point out the errors, I added some comments:

//determine the shippingCharges
if(pounds <= 2) {
	if(miles <=	500) {
		JOptionPane.showMessageDialog(null, "The shipping charge is $1.10");
	} else if(miles <=	1000){
		JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
	} else if(miles <=	1500) {
		JOptionPane.showMessageDialog(null, "The shipping charge is $3.30");
	//else (miles <= 2000) is not a valid statement. Either make it a "else if " or leave it "else"
	//If you opt for the first option, then don't forget the "else " statement
	} else(miles <=	2000) { 
		JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
	}
}
	
} else if(pounds <= 6) {
	// Will give a missing "else" statement
	if(miles <= 500) {
		JOptionPane.showMessageDialog(null, "The shipping charge is $2.20");
	} else if(miles <= 1000) {
		JOptionPane.showMessageDialog(null, "The shipping charge is $4.40");
	} else if(miles <=1500) {
		JOptionPane.showMessageDialog(null, "The shipping charge is $8.80");
	} else if(miles <=2000) {
		JOptionPane.showMessageDialog(null, "The shipping charge is $13.20");
	}
}
//... Similar errors in the following blocks	
} else {
	System.exit(1);
}


Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4884
  • View blog
  • Posts: 11,279
  • Joined: 16-October 07

Re: my code only partially functions thru first 4 statements, then flops.

Posted 14 September 2012 - 03:25 AM

I'd consider a method: String getShippingCharges(double pounds, double miles)

This would highlight something else. You should always print something. That is, all choices should return a value.

if (pounds <= 2 && miles <= 500) { 
	return "1.10";
} else if (pounds <= 2 && miles <= 1000) { 
	return "2.20";
// and so on
} else if (pounds <= 2) { 
	// if you should find your way here, pounds meets your criteria,
	// but no value for miles does
	// what do you show?
} else if (pounds <= 4 && miles <= 500) { 
// ...
} else { 
	// if you get here, all prior checks have failed.
}



Also, in a method, you can avoid the else and just stack everything up like so:
if (pounds <= 2 && miles <= 500) { return "1.10"; }
if (pounds <= 2 && miles <= 1000) { return "2.20"; }
// at the end
return "???";



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1