Results 1 to 3 of 3
  1. #1
    seegee is offline Member
    Join Date
    Jul 2011
    Posts
    36
    Rep Power
    0

    Default Variable not initialized?

    So I just started Java yesterday. I have been reading a book about it. So far I have made this code and run into a problem. It says " The variable itemcost might have not been initialized but it is clearly initialized in the if statement. I really have no idea what to do, and yes, I have searched.




    import java.util.Scanner;
    public class IDKKKKK
    {

    public static void main(String args[]) {
    Scanner myScanner= new Scanner (System.in);
    int PayPerHour;
    int HoursADay;
    int DaysAWeek;
    int PerDay;
    int PerWeek;
    int PerYear;
    int itemcost;
    int time;
    char reply;


    System.out.print("How much are you paid an hour?");
    PayPerHour= myScanner.nextInt();
    System.out.print("How many hours do you work a day?");
    HoursADay= myScanner.nextInt();
    System.out.print("How many days a week do you work?");
    DaysAWeek= myScanner.nextInt();
    PerDay= PayPerHour*HoursADay;
    PerWeek= PerDay*DaysAWeek;
    PerYear= PerWeek*52;
    time= itemcost/PerWeek;
    System.out.println("You make ");
    System.out.print( PerDay);
    System.out.println(" dollars per day.");
    System.out.print(" You make ");
    System.out.print(PerWeek);
    System.out.println(" dollars per week.");
    System.out.print(" You Make ");
    System.out.print(PerYear);
    System.out.println(" dollars per year.");
    System.out.println("Is there anything you want to save up for? (Y/N)");
    reply= myScanner.next().charAt(0);


    if (reply== 'Y' || reply=='y'){
    System.out.print ("How much does your item cost?");
    itemcost= myScanner.nextInt();
    System.out.print("It will take you ");
    System.out.print(time );
    System.out.print("weeks of work to get enough money to purchase your item");
    }





    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by seegee View Post
    So I just started Java yesterday. I have been reading a book about it. So far I have made this code and run into a problem. It says " The variable itemcost might have not been initialized but it is clearly initialized in the if statement. I really have no idea what to do, and yes, I have searched.
    1) If statements are not guaranteed to happen (that's why they're called "if" statements). Your best off initializing the variable to some default value when you first declare it.
    2) Regardless, are you sure that you assign something to itemcost before you try to use it? I don't think so. Again, initialize it at declaration, and don't use it until you have assigned something logical to it.

  3. #3
    seegee is offline Member
    Join Date
    Jul 2011
    Posts
    36
    Rep Power
    0

    Default

    Ok thanks, at first it didn't work but that was because it was dividing itemcost/perweek before the person even typed itemcost so it was forced to use the value i declared as a place holder. Now i moved the itemcost/perweek so it is after the person types the item cost.

Similar Threads

  1. Variable not initialized.
    By miss_peaches in forum New To Java
    Replies: 14
    Last Post: 04-27-2011, 10:28 AM
  2. Variables might not have been initialized
    By tnixon22 in forum New To Java
    Replies: 5
    Last Post: 01-17-2011, 01:47 AM
  3. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 04:27 PM
  4. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 06:00 AM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 10:02 AM

Posting Permissions

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