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

Thread: a==b, b not initialized

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default a==b, b not initialized

    well, ignoring the story, my problem is pretty straightforward.
    a==b is not initialized (according to Jcreator)

    public class Project
    {
    	public static void main (String []args)
    	{
    		int a, b, c, d, e, f;
    		System.out.println("Welcome to the game of life!");
    		System.out.println("You are in your fourth year of high school.");
    		System.out.println("There is this one girl whom you like.");
    		System.out.println("One day, you decided to tell her your feelings after school.");
    		System.out.println("She is coming your way. What do you do?");
    		System.out.println("1. Go to her and tell her how you feel.");
    		System.out.println("2. Wait a while.");
    		a=SavitchIn.readLineInt();
     
    		if (a==a)
    		{
    			System.out.print("You told her how you feel.");
    			System.out.println("She suddenly embraced you.");
    			System.out.println("As you pushed her away, a man came and shot her in the head.");
    			System.out.println("She was a zombie. You're lucky I'm here.");
    			System.out.println("My name is Jack. Want to team up with me?");
    			System.out.println("1. Yes / 2. No");
    			b=SavitchIn.readLineInt();
     
    			if (b==a)
    			{
    				System.out.print("He suddenly embraced you.");
    				System.out.println("As you pushed him away, a woman came and shot him in the head.");
    				System.out.println("These zombies are getting smart. Name's Rose.");
    				System.out.println("Wanna team up?");
    				System.out.println("1. Yes / 2. No");
    				c=SavitchIn.readLineInt();
     
    				if (c==a)
    				{
    					System.out.println("Hey, you don't remember me?");
    					System.out.println("You really don't remember me? You confessed to me, didn't you?");
    					System.out.println("I'm not really sure how I feel for you, but I'll give it time.");
    					System.out.println("It's all right, we'll start over. This world needs to change.");
    					System.out.println("We are, after all, the remaining humans left in this world of zombies, aren't we?");
    					System.out.println("THE END");
    				}
    				else if (c==b)
    				{
    					System.out.println("Well, too bad.");
    					System.out.println("Good luck, anyway.");
    					System.out.println("Here, a revolver. You'll need it in this world.");
    					System.out.println("We are, after all, the remaining humans left in this world of zombies, aren't we?");
    					System.out.println("THE END");
    				}
    			}
    			else if (b==b)
    			{
    				System.out.println("Hey, anyway, help me sell these bananas!");
    				System.out.println("He forced you to sell bananas against your will.");
    				System.out.println("You hit him with these bananas, and he falls to the ground.");
    				System.out.println("The police, who shot down zombies before reaching you, handcuffed you and put you in their patrol car.");
    				System.out.println("BAD END");
    				System.out.println("1. to continue / 2. for bananas");
    				d=SavitchIn.readLineInt();
     
    				if (d==a)
    				{
    					System.out.println("You are in a prison cell.");
    					System.out.println("THE END");
     
    				}
    				else if (d==b)
    				{
    					System.out.println("BANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANA");
    					System.out.println("boat.");
    					System.out.println("THE END");
    				}
    			}
    		}
     
    		else if (a==b)
    		{
    			System.out.println("You waited for a while.");
    			System.out.println("She ran onto the road.");
    			System.out.println("She went out of sight.");
    			System.out.println("Will you follow her?");
    			System.out.println("1. Yes / 2. No");
    			e=SavitchIn.readLineInt();
     
    			if (e==a)
    			{
    				System.out.println("You ran after her.");
    				System.out.println("You see her run around a bench, and into a corner.");
    				System.out.println("As you followed her, you find yourself in a magical land of unicorns and rainbows.");
    				System.out.println("You find a unicorn approach you in a friendly manner.");
    				System.out.println("You find a chocolate bar in your pocket.");
    				System.out.println("1. Give / 2. Eat");
    				f=SavitchIn.readLineInt();
     
    				if (f==a)
    				{
    					System.out.println("You give the unicorn your chocolate bar.");
    					System.out.println("It transforms into a giant cactus and gives you the ability to fly.");
    					System.out.println("I have given you this power for now.");
    					System.out.println("Do more good deeds, and you may gain more.");
    					System.out.println("The fate of the world is in your hands.");
    					System.out.println("THE END");
    				}
    				else if (f==b)
    				{
    					System.out.println("You eat the chocolate bar in front of the unicorn.");
    					System.out.println("You wake up at school.");
    					System.out.println("YOu realized that you were dreaming the whole time.");
    					System.out.println("There is this one girl whom you like.");
    					System.out.println("One day, you decided to tell her your feelings after school.");
    					System.out.println("She is coming your way. What do you do?");
    					System.out.println("THE END");
    				}
    			}
    			else if (e==b)
    			{
    				System.out.println("So you're not going to follow her?");
    				System.out.println("Really?");
    				System.out.println("You don't deserve to tell her your feelings.");
    				System.out.println("She could be just waiting for you to tell her how you feel.");
    				System.out.println("If you're a man, act like one.");
    				System.out.println("You don't deserve to continue this game.");
    				System.out.println("THE END");
     
    			}
    		}
    	}
    }

    thanks


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default

    What I assume it means by not initialized is b has no value (that you the programmer is aware.) So your trying to check for equality with a variable that you have not assigned a value to. If you look at your first if statement and if it returns false no value is given to variable int b.

    So maybe you should initialize all your variables at the start of your code. And review your code again. Paying particular attention to the nested-if statements you have going on there.

    int a,b,c,d,e,f = 0;

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

    Default Re: a==b, b not initialized

    well, i also tried using this code

    public class Project
    {
    	public static void main (String []args)
    	{
    		int a, b, c, d, e, f;
    		System.out.println("Welcome to the game of life!");
    		System.out.println("You are in your fourth year of high school.");
    		System.out.println("There is this one girl whom you like.");
    		System.out.println("One day, you decided to tell her your feelings after school.");
    		System.out.println("She is coming your way. What do you do?");
    		System.out.println("1. Go to her and tell her how you feel.");
    		System.out.println("2. Wait a while.");
    		a=SavitchIn.readLineInt();
     
    		if (a==a)
    		{
    			System.out.print("You told her how you feel.");
    			System.out.println("She suddenly embraced you.");
    			System.out.println("As you pushed her away, a man came and shot her in the head.");
    			System.out.println("She was a zombie. You're lucky I'm here.");
    			System.out.println("My name is Jack. Want to team up with me?");
    			System.out.println("1. Yes / 2. No");
    			b=SavitchIn.readLineInt();
     
    			if (b==a)
    			{
    				System.out.print("He suddenly embraced you.");
    				System.out.println("As you pushed him away, a woman came and shot him in the head.");
    				System.out.println("These zombies are getting smart. Name's Rose.");
    				System.out.println("Wanna team up?");
    				System.out.println("1. Yes / 2. No");
    				c=SavitchIn.readLineInt();
     
    				if (c==a)
    				{
    					System.out.println("Hey, you don't remember me?");
    					System.out.println("You really don't remember me? You confessed to me, didn't you?");
    					System.out.println("I'm not really sure how I feel for you, but I'll give it time.");
    					System.out.println("It's all right, we'll start over. This world needs to change.");
    					System.out.println("We are, after all, the remaining humans left in this world of zombies, aren't we?");
    					System.out.println("THE END");
    				}
    				else if (c==b)
    				{
    					System.out.println("Well, too bad.");
    					System.out.println("Good luck, anyway.");
    					System.out.println("Here, a revolver. You'll need it in this world.");
    					System.out.println("We are, after all, the remaining humans left in this world of zombies, aren't we?");
    					System.out.println("THE END");
    				}
    			}
    			else if (b==b)
    			{
    				System.out.println("Hey, anyway, help me sell these bananas!");
    				System.out.println("He forced you to sell bananas against your will.");
    				System.out.println("You hit him with these bananas, and he falls to the ground.");
    				System.out.println("The police, who shot down zombies before reaching you, handcuffed you and put you in their patrol car.");
    				System.out.println("BAD END");
    				System.out.println("1. to continue / 2. for bananas");
    				d=SavitchIn.readLineInt();
     
    				if (d==a)
    				{
    					System.out.println("You are in a prison cell.");
    					System.out.println("THE END");
     
    				}
    				else if (d==b)
    				{
    					System.out.println("BANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANA");
    					System.out.println("boat.");
    					System.out.println("THE END");
    				}
    			}
     
    		else if (a==b)
    		{
    			System.out.println("You waited for a while.");
    			System.out.println("She ran onto the road.");
    			System.out.println("She went out of sight.");
    			System.out.println("Will you follow her?");
    			System.out.println("1. Yes / 2. No");
    			e=SavitchIn.readLineInt();
     
    			if (e==a)
    			{
    				System.out.println("You ran after her.");
    				System.out.println("You see her run around a bench, and into a corner.");
    				System.out.println("As you followed her, you find yourself in a magical land of unicorns and rainbows.");
    				System.out.println("You find a unicorn approach you in a friendly manner.");
    				System.out.println("You find a chocolate bar in your pocket.");
    				System.out.println("1. Give / 2. Eat");
    				f=SavitchIn.readLineInt();
     
    				if (f==a)
    				{
    					System.out.println("You give the unicorn your chocolate bar.");
    					System.out.println("It transforms into a giant cactus and gives you the ability to fly.");
    					System.out.println("I have given you this power for now.");
    					System.out.println("Do more good deeds, and you may gain more.");
    					System.out.println("The fate of the world is in your hands.");
    					System.out.println("THE END");
    				}
    				else if (f==b)
    				{
    					System.out.println("You eat the chocolate bar in front of the unicorn.");
    					System.out.println("You wake up at school.");
    					System.out.println("YOu realized that you were dreaming the whole time.");
    					System.out.println("There is this one girl whom you like.");
    					System.out.println("One day, you decided to tell her your feelings after school.");
    					System.out.println("She is coming your way. What do you do?");
    					System.out.println("THE END");
    				}
    			}
    			else if (e==b)
    			{
    				System.out.println("So you're not going to follow her?");
    				System.out.println("Really?");
    				System.out.println("You don't deserve to tell her your feelings.");
    				System.out.println("She could be just waiting for you to tell her how you feel.");
    				System.out.println("If you're a man, act like one.");
    				System.out.println("You don't deserve to continue this game.");
    				System.out.println("THE END");
    				}
    			}
    		}
    	}
    }
    it worked for the a==a (typing the number 1) , but when i tried the a==b (typing the number 2) , the result came back as if it was a==a

  4. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: a==b, b not initialized

    The expression inside the argument in the first if-statement will always result to True. Meaning the else statement will not run because it is never false (with the options given.) Also you don't need to use the else if statements. You are able to just use else { code }.
    Try this

    //1 being yes and 2 being no
    if(a==1){
    block of code
    }
    if(b==2){
    block of code
    }

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

    StephenCoyle (March 4th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: a==b, b not initialized

    if you just want to have correct solution, I recommend to delete all local variables but 'a' and use allways 'a' to compare it to 1 or 2. But if it's your intention to make funny source then give the binary tree solution a chance :


    static final String[][] SENTENCES = {
     
    			{
    					"Welcome to the game of life!",
    					"You are in your fourth year of high school.",
    					"There is this one girl whom you like.",
    					"One day, you decided to tell her your feelings after school.",
    					"She is coming your way. What do you do?",
    					"1. Go to her and tell her how you feel.",
    					"2. Wait a while." },
    			{
    					"You told her how you feel.",
    					"She suddenly embraced you.",
    					"As you pushed her away, a man came and shot her in the head.",
    					"She was a zombie. You're lucky I'm here.",
    					"My name is Jack. Want to team up with me?",
    					"1. Yes / 2. No" },
    			{
    			"You waited for a while.", "She ran onto the road.",
    					"She went out of sight.", "Will you follow her?",
    					"1. Yes / 2. No" },
    			{
    					"He suddenly embraced you.",
    					"As you pushed him away, a woman came and shot him in the head.",
    					"These zombies are getting smart. Name's Rose.",
    					"Wanna team up?", "1. Yes / 2. No" },
    			{
    					"Hey, anyway, help me sell these bananas!",
    					"He forced you to sell bananas against your will.",
    					"You hit him with these bananas, and he falls to the ground.",
    					"The police, who shot down zombies before reaching you, handcuffed you and put you in their patrol car.",
    					"BAD END", "1. to continue / 2. for bananas", },
    			{
    					"You ran after her.",
    					"You see her run around a bench, and into a corner.",
    					"As you followed her, you find yourself in a magical land of unicorns and rainbows.",
    					"You find a unicorn approach you in a friendly manner.",
    					"You find a chocolate bar in your pocket.",
    					"1. Give / 2. Eat" },
    			{
    					"So you're not going to follow her?",
    					"Really?",
    					"You don't deserve to tell her your feelings.",
    					"She could be just waiting for you to tell her how you feel.",
    					"If you're a man, act like one.",
    					"You don't deserve to continue this game.", "THE END" },
    			{
    					"Hey, you don't remember me?",
    					"You really don't remember me? You confessed to me, didn't you?",
    					"I'm not really sure how I feel for you, but I'll give it time.",
    					"It's all right, we'll start over. This world needs to change.",
    					"We are, after all, the remaining humans left in this world of zombies, aren't we?",
    					"THE END" },
    			{
    					"Well, too bad.",
    					"Good luck, anyway.",
    					"Here, a revolver. You'll need it in this world.",
    					"We are, after all, the remaining humans left in this world of zombies, aren't we?",
    					"THE END" },
    			{ "You are in a prison cell.", "THE END" },
    			{
    					"BANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANANA",
    					"boat.", "THE END" },
    			{
    					"You give the unicorn your chocolate bar.",
    					"It transforms into a giant cactus and gives you the ability to fly.",
    					"I have given you this power for now.",
    					"Do more good deeds, and you may gain more.",
    					"The fate of the world is in your hands.", "THE END" },
    			{
    					"You eat the chocolate bar in front of the unicorn.",
    					"You wake up at school.",
    					"YOu realized that you were dreaming the whole time.",
    					"There is this one girl whom you like.",
    					"One day, you decided to tell her your feelings after school.",
    					"She is coming your way. What do you do?", "THE END" },
     
    	};
     
    	static void decide(int accPos) {
     
    		for (String s : SENTENCES[accPos - 1])
    			System.out.println(s);
    		if (2 * accPos <= SENTENCES.length
    				&& SENTENCES[(2 * accPos) - 1] != null) {
    			int childPosition = 2 * accPos + (SavitchIn.readLineInt() - 1);
     
    			decide(childPosition);
     
    		}
    	}
     
    	public static void main(String[] args) {
    		decide(1);
    	}

  7. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: a==b, b not initialized

    somehow, the problem all began with the else (a==b) statements, since no matter what input i typed, it all came out as a==a
    tnx for your help, guys
    although the problem has yet to be solved, i think i can manage

  8. #7
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: a==b, b not initialized

    The problem has been solved by Zyrion. The problem is with the way you are trying to evaluate the if statements a==b or b==a. As Zyrion said use a==1 or b==2 to solve the problem.

    Quote Originally Posted by Zyrion View Post
    The expression inside the argument in the first if-statement will always result to True. Meaning the else statement will not run because it is never false (with the options given.) Also you don't need to use the else if statements. You are able to just use else { code }.
    Try this

    //1 being yes and 2 being no
    if(a==1){
    block of code
    }
    if(b==2){
    block of code
    }

Similar Threads

  1. variable might not have been initialized!!
    By bassie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2012, 11:39 AM
  2. Variable might not have been initialized
    By JavaGirl9001 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2011, 10:24 AM
  3. Conditions and 'might not have been initialized' error.
    By mwebb in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 2nd, 2011, 11:22 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, 02:03 PM