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 5 of 5

Thread: Help with Java program: Software Sales

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Help with Java program: Software Sales

     import java.text.DecimalFormat;
    import java.util.Scanner;
     
    public class SoftwareSales
    {
       public static void main(String[] args)
       {
          DecimalFormat money = new DecimalFormat("#,###.00");
          final double unitPrice = 99.00;
          int units;
          double discountPercent, discount, cost;
     
          Scanner keyboard = new Scanner(System.in);
     
          //ask user enter units
          System.out.print("Enter the amount of units sold: ");
             units = keyboard.nextInt();
     
          //algorithm   
          if (units >= 100)
             {discountPercent = 0.5;}
          else if (units >= 50 && units <= 99)
             {discountPercent = 0.4;}
          else if (units >= 20 && units <= 49)
             {discountPercent = 0.3;}
          else if (units >= 10 && units <= 19)
             {discountPercent = 0.2;}
     
          discount = units * unitPrice * discountPercent;
          cost = (units * unitPrice) - discount;
     
          //display
          System.out.println("Units sold: " + units);
          System.out.println("Discount $: " + money.format(discount));
          System.out.println("Cost $: " + money.format(cost));
       }
    }

    I keep getting this error message when I compile the program:
     SoftwareSales.java:31: error: variable discountPercent might not have been initialized
          discount = units * unitPrice * discountPercent;
                                         ^
    1 error


  2. #2
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Help with Java program: Internet Service Provider

    Hi rosiems, your code looks okay to me. It likely, whenever you run it, your input is always below the minimum i.e. 10. So, I suggest (and this is the proper thing to do) you add a last else statement that initializes discountPercent to zero or something of your choosing.
    Who holds the KEY to all knowledge?

  3. The Following User Says Thank You to Mugambbo For This Useful Post:

    rosiems95 (February 22nd, 2014)

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

    Default Re: Help with Java program: Internet Service Provider

    variable discountPercent might not have been initialized
    The compiler does not go through all the if conditions to see what happens there. It can not see that the variable will always be given a value. Best to give local variables values when they are defined so the compiler is satisfied.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    rosiems95 (February 22nd, 2014)

  6. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program: Internet Service Provider

    It worked! Thank you so much.

  7. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Help with Java program: Internet Service Provider

    If you are curious, the problem would have arised when units would have been a negative integer. In this case the variable discountPercent would not have been initialized.
    Simply giving it a default value when defining the variable, or adding another "else" statement at the end of your if-then-else block would solve the problem.

Similar Threads

  1. JAVA help on file: Internet Service Provider
    By Plural in forum What's Wrong With My Code?
    Replies: 26
    Last Post: March 30th, 2014, 11:03 PM
  2. Internet Service Provider
    By gardypr12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2013, 02:00 PM
  3. TimeLag between private internet provider to my EST timezone server
    By bhanuvijay10 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 11th, 2012, 05:31 PM
  4. Replies: 1
    Last Post: November 21st, 2011, 06:22 AM
  5. Web Service - Client - WORK OVER INTERNET
    By nikos in forum Java Networking
    Replies: 7
    Last Post: October 19th, 2010, 10:44 AM