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: variable might not have been initialized...help appreciated

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

    Default variable might not have been initialized...help appreciated

    hey guys ive been working on the project and i keep gettin an error here is thy code....
    public class Tickets
    {
    public static void main(String[] args)
     
    {
     
     
     DecimalFormat df = new DecimalFormat("#,000.00");
     
     
     
    Scanner s = new Scanner(System.in); // create a Scanner object
     String name;
     String input;	// read in user input as a string
     
     
      System.out.println("Welcome To Royals Stadium! Please select your ticket tier then enter quanity!");
    System.out.println("(A)Tier 1 $50.00 ");
    System.out.println("(B)Tier 2 $40.00 ");
    System.out.println("(C)Tier 3 $30.00 ");
    System.out.println("Please enter ticket tier A, B, or C: "); 
    	char tick1 = (s.nextLine()).charAt(0);
     
    System.out.println("Please enter quanity: "); 
    int hours1 = Integer.parseInt(s.nextLine());
    	int points1;
     
     if(tick1 == 'A' || tick1 == 'a'){
    		points1 = 50;
    	} // end 'A"
    	else if(tick1 =='B' || tick1 == 'b'){
    		points1 = 40;
    	} // end 'B'
    	else if(tick1 =='C' || tick1 == 'c'){
    		points1 = 30;
    	} // end 'C'
     
     System.out.println("Would you like to add Parking to the Ticket ");	// user prompt
      System.out.println("(Y for Yes, N for No) ");	// user prompt
     input = s.nextLine();
     
     double totalcredits = points1*hours1;   (this is where the error occurs)	
    double totalhours = hours1;
    	double gpa = (double) totalcredits/totalhours;
    	System.out.print("Your Grand ticket total is" + df.format(gpa));





    here is the error

    Tickets.java:55: variable points1 might not have been initialized
    double totalcredits = points1*hours1;
    ^
    1 error
    i just cant figure out why its not goin through
    thanks for your help!
    Last edited by helloworld922; February 3rd, 2012 at 06:25 PM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: variable might not have been initialized...help appreciated

    Can't you just understand the error? It's plain english, saying your variables points1 has not been initialized. Initialize points1 where you have declared it.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: variable might not have been initialized...help appreciated

    Lighten up. Yes i can read the error code. I thought I already declared and initialized points1.
    int points1; //(declared)
     
    if(tick1 == 'A' || tick1 == 'a'){
    		points1 = 50;      //( initialed a value)
    	} // end 'A"
    	else if(tick1 =='B' || tick1 == 'b'){
    		points1 = 40;      // ( initialed a value)
    	} // end 'B'
    	else if(tick1 =='C' || tick1 == 'c'){
    		points1 = 30;     // ( initialed a value)
    	} // end 'C'
    Thanks for your help!
    Last edited by helloworld922; February 3rd, 2012 at 06:24 PM.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: variable might not have been initialized...help appreciated

    It looks like you have declared and initialized it properly but thats not true. You have declared it correctly but all initialization is happening in conditional loop so there might be a situation where programs flow doesn't pass through any of the loop then no initialization will happen and compiler always try to avoid that situation. This error is pointing to that situation.

    Just add this extra line
    else
    {
    points1 = 30;
    }


    Error will be gone. or you initilize it where you have declared it.
    Always remember local member don't get default value as instance variable.

    Hope this help. For instance or local variable look here URL

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: variable might not have been initialized...help appreciated

    Notice that the Java compiler does not (cannot?) do any complicated analysis to see if one or other of the if branches - or corresponding case parts of a switch statement - *must* be reached.

    The logic of your program might imply that one of A, B or C hold, but you still have to help the compiler by covering all cases including that where none of the conditions hold. If no assignment makes sense you have the option of throwing an exception.

Similar Threads

  1. Variable might not have been initialized
    By JavaGirl9001 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2011, 11:24 AM
  2. variable might not be initialized problem. Can someone tell me whats wrong?
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 09:25 AM
  3. Replies: 4
    Last Post: October 20th, 2011, 11:26 AM
  4. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  5. Variable might not have been initialized?
    By n56 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 03:03 PM