Results 1 to 8 of 8
  1. #1
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Getting an error " variable may not have been initialized'

    Hey guys, I know what the error means but I don't think initializing the variable will make my code work as intended so I'm having a little dilemna here... here's the code and I'll highlight the part that is said to be not initialized:

    Java Code:
    import java.util.Scanner;
    
       public class ItemCost {
        public static void main (String []args){
        int i=1,item=1,e=1, f=1, g=1;
        int items,  d ;
        double gst, qst, subt, Tot = 1, PriceItems ;
        Scanner x = new Scanner(System.in);
        Scanner y = new Scanner(System.in);
        Scanner z = new Scanner(System.in);
        Scanner w = new Scanner(System.in);
    
    
        System.out.println("Please input the amount of items bought");
        items = x.nextInt();
    
        while ( items < 1 || items > 10) 
    
        {
            System.out.print("Sorry the input was not correct, please try again\n");
            items = x.nextInt();
            i++;
        } 
    
      do 
      {
         System.out.print("Please input the price of the item  "+item   +         " " );
          PriceItems = y.nextDouble();
        [B][SIZE=4]  subt += PriceItems;[/SIZE][/B]
           if 
           ( PriceItems < 1 || PriceItems > 1000 )
               e++;
           else
          item++;
      }while (item <= items);
    
    
    
       System.out.println("Please input the rate of GST in %");
       gst = z.nextDouble();
    
       while (gst < 0 || gst > 22)
       { 
           System.out.print("Sorry, this was incorrect, please try     again");
           gst = z.nextDouble();
           f++;
       } 
    
    
    
       System.out.println("Please input the rate of QST in %");
       qst = w.nextDouble();
    
       while (qst < 0 || qst > 18)
       { 
           System.out.print("Sorry, this was incorrect, please try again");
           qst = z.nextDouble();
           g++;
       }
    }
      }
    So I'm supposed to get the following output :

    Java Code:
     Please input the amount of items bought
    2
    Please input the price of the item  1 1
    Please input the price of the item  2 2
    Please input the rate of GST in % 20
    
    Please input the rate of QST in % 18
    HOWEVER my program doesn't seem to add input of item 1, and 2 if I initialize subt= 0 initially. It'll only take the last value inputted in the loop. By the way, the increments are counters to count the amount of errors the user might input by accident ( or whatever). Some people have been pointing it out as useless but that's the only way I found it to work.

    THanks for the help in advance guys!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Getting an error " variable may not have been initialized'

    What is the program's current output?

    Note: Single letter variable names make the code harder to read and understand. Use names that describe what the variable contains.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Getting an error " variable may not have been initialized'

    Sorry, this is like my third program, trying to get use to it. I'll change that. The current output would be something like this :


    Java Code:
    Please input the amount of items bought
    2
    Please input the price of the item  1 1
    Please input the price of the item  2 2
    Please input the rate of GST in %
    10
    Please input the rate of QST in %
    9
    2.0
    Also can I ask one other thing, for variables, do you usually capitalize them as a convention?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Getting an error " variable may not have been initialized'

    current output would be
    I was asking what the current out actually was.
    What happens when you compile and execute the program?

    Java coding conventions says variable names should start with a lowercase letter.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Getting an error " variable may not have been initialized'

    subt += PriceItems;
    The compiler is complaining that subt has not been given a value, so incrementing its value doesn't make sense.

    [Edit]

    HOWEVER my program doesn't seem to add input of item 1, and 2 if I initialize subt= 0 initially. It'll only take the last value inputted in the loop.
    Only just read this...

    subt does have to be initialised. I"m not sure what you mean by the program "taking" values. If you find some variable has got a strange value after the do loop, use System.out.println() to print its value then post the whole output together with what you expected the value would be.
    Last edited by pbrockway2; 02-22-2015 at 01:42 AM.

  6. #6
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Getting an error " variable may not have been initialized'

    Java Code:
    Please input the amount of items bought
    2
    Please input the price of the item  1 1
    Please input the price of the item  2 2
    Please input the rate of GST in %
    10
    Please input the rate of QST in %
    9
    2.0
    where 2.0 is subt =+ PriceItems ( if I do subt += PriceItems then it says variable hasn't been initialized)

    The answer should be the price of (in this case) Item 1 + item 2 = 3

    Now, I know I don't know anything about java. I just feel that the program doesn't keep in memory the previous input of the user ( in the do-while loop where they are ask to input the price --> PriceItems). In anyway, I don't know how to remediate to that problem

    EDIT**** hum.... now that I initialized it to 0 ( Eclipse "suggested it"), the program worked, any idea why?
    Last edited by TheRockIng; 02-22-2015 at 03:36 AM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Getting an error " variable may not have been initialized'

    When you do the following:

    Java Code:
    subt =+ PriceItems;
    All you are doing is initializing subt to PriceItems. You don't need the + in that case.

    Java Code:
    subt += PriceItems;
    This uses the compound assignment operator which is a short cut for doing

    Java Code:
    subt = subt + PriceItems;
    Since you are adding something to an existing variable, the variable needs to be initialized (as has been already explained in
    previous posts).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Getting an error " variable may not have been initialized'

    Quote Originally Posted by jim829 View Post
    the variable needs to be initialized
    ... before the loop; not inside it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 06-26-2014, 06:54 AM
  2. Replies: 4
    Last Post: 06-29-2013, 03:57 AM
  3. Need help on "variable might not have been initialized"
    By CrushingJava in forum New To Java
    Replies: 6
    Last Post: 06-28-2013, 11:35 AM
  4. Help needed with "Variable is not public in component" error.
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 05-29-2013, 09:27 PM
  5. Replies: 4
    Last Post: 06-29-2012, 10:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •