Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Declaring floating point number depending on an if statement

  1. #1
    Junior Member Keys767's Avatar
    Join Date
    Aug 2011
    Location
    Surbiton, London, UK
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Declaring floating point number depending on an if statement

    Hello,
    I'm trying some basic Java exercises, but I'm stuck on one of them.

    My code is:

    import javax.swing.JOptionPane;
    public class PrintingDiscount
     
    {
    	public static void main(String[] args)
     
    	{
    		String numberOfFlyersInput = JOptionPane.showInputDialog("How many flyers?");
    		String numberOfPostersInput = JOptionPane.showInputDialog("How many posters?");
     
    		int numberOfFlyers = Integer.parseInt(numberOfFlyersInput);
    		int numberOfPosters = Integer.parseInt(numberOfPostersInput);
     
    		double flyerTotal = 0;
    		double posterTotal = 0;
    		double grandTotal = 0;
    		double costOfPoster = 2.00;
     
    /*
    				if (numberOfFlyers >= 500)
    					{
    						double costOfFlyer = 0.005;
    					}
    				else
    					{
    						double costOfFlyer = 0.01;
    					}
    */
     
    		flyerTotal = (costOfFlyer * numberOfFlyers);
    		posterTotal = (costOfPoster * numberOfPosters);
     
    		grandTotal = (flyerTotal + posterTotal);
     
    		JOptionPane.showMessageDialog(null, "Your flyers will cost £" + flyerTotal + "\nYour posters will cost £" + posterTotal + "\nIn total, this will cost £" + grandTotal);
        }
    }

    The part commented out is my problem. I basically want to declare costOfFlyer as 0.01, unless the number entered by the user (numberOfFlyers) is 500 or more, in which case I want it to be 0.005.

    So, I've roughly coded what I thought might achieve this, but it is obviously incorrect since the compiler now cant find the variable costOfFlyer.

    Any tips would be appreciated. Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Declaring floating point number depending on an if statement

    You have defined that variable inside of a pair of {}s. The {}s define the scope or definitional visibility of the variable. The variable is not defined outside of the {}s.
    You need to move the definition of the variable to the same level of enclosing {}s that you want other code to be able to see it.
    You should put its definition with the other doubles you are defining.

  3. The Following 2 Users Say Thank You to Norm For This Useful Post:

    Keys767 (August 11th, 2011), Sean4u (August 10th, 2011)

  4. #3
    Junior Member Keys767's Avatar
    Join Date
    Aug 2011
    Location
    Surbiton, London, UK
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declaring floating point number depending on an if statement

    Moved some stuff around and got it to work (shouldn't have been using 'double' inside the if statement ). Thanks for the push in the right direction.

Similar Threads

  1. Declaring your own exceptions
    By weakprogrammer in forum Exceptions
    Replies: 4
    Last Post: June 30th, 2011, 08:19 AM
  2. Rounding long or floating numbers, or longs for that matter.
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2011, 12:32 AM
  3. Calling the actionlistener class depending on variables
    By zidsal in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2011, 08:52 AM
  4. using Eclipse and every program gives the errorr with floating numbers
    By Neera in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2010, 12:11 PM
  5. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM