Results 1 to 15 of 15
  1. #1
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Variable not initialized.

    Hello, I am compiling a code for class to enter donor's information (name, address, donations, etc) I am to modify the code so if a negative number is entered, it prompts the user to enter a positive value. Also its supposed to request info for 5 ppl using counter-controlled repetition. Attached is what I have so far.


    ** The error I receive is that variable number3 might not have been initialized. PLEASE HELP!!!
    Last edited by miss_peaches; 04-27-2011 at 04:41 AM. Reason: code tags

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Would you mind wrapping your code in code tags?
    [ code] <-omit space
    YOUR CODE HERE
    [/code]

    Also, post the exact stack trace, copy and paste it. And highlight the line of code which threw it.
    Last edited by sunde887; 04-27-2011 at 04:37 AM.

  3. #3
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    @Sunde887- Sure! But I don't know what that is or how to do it! :(

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Type [ code] but don't include the leading space, then copy and paste your code on the next line, then put [/code] at the end of your code.

  5. #5
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    How about I'll just delete it and post as attachment because I'm still unsure of exactly what you mean.

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

    Default

    What he means is when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    I hope you have understood what Pete has said above about using the code tags.

    Quote Originally Posted by miss_peaches View Post
    ** The error I receive is that variable number3 might not have been initialized. PLEASE HELP!!!
    This message says that you have declared some local variable number3 somewhere in your method and you are trying to use it without initializing it.

    Have a look here : Local Variables Initialization

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Here is the code again, this time submitted as an attachment. Thanks in advance for your time
    Attached Files Attached Files

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

    Default

    Please if there's something you don't understand about using code tags then ask, but many more will read your code (including me) if you post it as requested using code tags.

  10. #10
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Java Code:
    /**
     * The AddressBookApp1 class implements an application that
     * simply prints address and charitable donations for individuals.
     */
    class AddressBookApp1 {
    	// main method begins program execution
        	public static void main(String[] args) {
    
            //create scanner to obtain input from command window
    	java.util.Scanner input = new java.util.Scanner (System.in);
    
    	String name; 	// Donor's name
    	String address;	// Donor's address
    	String city;	// City where donor lives
    	String state;	// State where donor lives
    	int zipcode;	// Zipcode of owner's address
    	int number1;	// Number of donations
    	int number2;	// Dollar amount of donations
    	double number3;	// Total amount of donations this year
    	int counter=0;	// Declare and initialize counter
    
    	while ( ++counter <= 5) // loop 5 times
    		System.out.print ("Enter the donor's name:"); //prompt
    		name = input.nextLine(); // read donor's name from user input
    		counter++;
    
    		System.out.print ("Enter donor's address:"); //prompt
    		address = input.nextLine(); // read address from user input
    	
    		System.out.print ("Enter donor's city:"); //prompt
    		city = input.nextLine(); // read city from user input
    
    		System.out.print ("Enter donor's state:"); // prompt
    		state = input.nextLine(); // read state from user input
    	
    		System.out.print ("Enter donor's zipcode:"); //prompt
    		zipcode = input.nextInt(); // read zipcode from user input
    
    		System.out.print ("Enter the number of donations this year:"); 
    		number1= input.nextInt(); // read number of donations from user 
    			if (number1<= -1) {
    			   System.out.print ("Please enter a positive number:"); 
    			} //end if
    			else
    		System.out.print ("Enter dollar amount of donations:"); //prompt
    		number2= input.nextInt(); // read donation amount from user 
    			if (number2 <= -1) {
    			   System.out.print ("Please enter a positive amount:"); 
    			} //end if
    			else
    	number3 = number1 * number2;
    
    	System.out.print ("The donor, whose name is"+ name);
    	System.out.print ("and resides at"+ address);
    	System.out.print ("in the city of"+ city);
    	System.out.print ("in the state of"+ state);
    	System.out.print ("with a zipcode of"+ zipcode);
    	System.out.printf ("has donated $%d\n" + number3);
    	System.out.println ("this year!");
    	
    
        } // end method main
    } // end class AddressBookApp1

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

    Default

    As per Goldest's link, you should give the variable not initialized an initial value. For instance I'd declare the number3 double variable to be initially = to 0.

  12. #12
    miss_peaches is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks so much and that did do the trick. The code has created a .class file. I was under the impression that I didn't have to assign number3 a value since that would be determined by number1 and number2, but oh well! Thanks again, and sorry for being kinda "slow" on the lingo on here. :)

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Haha, no problem, glad you figured out your problem, when you post code with errors in the future it's helpful if you provide the stack trace as well, and wrap it in code tags as well.

    Also, please mark the thread solved with the thread tools at the top of the page.

  14. #14
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    You should use eclipse or neatbeans as your editor, they automatically detect that error

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by RichersooN View Post
    You should use eclipse or neatbeans as your editor, they automatically detect that error
    I don't know if I agree with this statement. An IDE is absolutely helpful, however; command line compiling with a simple text editor can be educational as well. There is a huge discussion in the sticky "what do you write your code in", to see arguments for both sides. In the end, it comes down to what you like(at least for learning, I'd imagine an IDE is invaluable as you begin coding large programs).

Similar Threads

  1. Variables might not have been initialized
    By tnixon22 in forum New To Java
    Replies: 5
    Last Post: 01-17-2011, 12:47 AM
  2. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 03:27 PM
  3. Replies: 0
    Last Post: 04-20-2009, 11:08 AM
  4. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 AM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09: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
  •