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: Nested-If Problem/ Initializing

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nested-If Problem/ Initializing

    So basically my code works (or at least I'm pretty sure it does). I removed the last if/else statements and it all runs. However when I put it in I get a "max" may not have been initialized.

    Could someone please explain to me how to initialize it. I know the basics behind it. Apparently it's not giving the values to "max" which is preventing the program from compiling.

    Here is the code:

    import java.util.Scanner;
    /**
     * MaxOfThree is a program that asks the user to input 3 different
     * integers, separated by a space and all on one line.
     * 
     * It then checks if all 3 are different. If not, it informs user
     * about the error and terminates the program.
     
     * Otherwise, it proceeds with computing the maximum of these 3
     * integers and prints it.
     *
     * 
     */
     
    import java.util.Scanner;
    public class MaxOfThree
    {
    	public static void main(String[] args)
    	{
    		// INITIALIZE YOUR SCANNER FOR GETTING INPUT FROM THE KEYBOARD
    		Scanner keyboard = new Scanner(System.in);
     
    		// ASK THE USER TO TYPE IN THREE DIFFERENT INTEGERS.
     
    		System.out.print("Enter three different integers, all on one line.");
     
                    int max; // Maximum of i, j, and k.
                    // Declare and read i, j, k.
     
    		int i;
                    int j;
                    int k;
     
    		i = keyboard.nextInt();
    		// ADD CODE HERE for reading j and k.
                    // -->
    		j = keyboard.nextInt();
                    k = keyboard.nextInt();
     
    		// Validate input by checking if any two are equal.
                    // If yes, inform user of the error and
                    // terminate by executing System.exit(0).
                    // ADD CODE after if. HERE YOU MAY USE LOGICAL AND (&&) or OR (||).
                    // --> 
     
    		if ((i==j) || (i==k) || (k==j))
    		{	
    		System.out.println("Equal integers are not allowed! Exiting...");
    		System.exit(0);
    		}
    		else
    		{ 
    		System.out.println("You gave me legal numbers");
                    }
                    // Since input is valid, proceed with determination of maximum.
                    // Important: DO NOT USE LOGICAL AND (&&) or OR (||) IN THIS PART.
                    // Just write nested  else .. if ... else if .. statement.
                    // This is just one long  if - else - if - else.... statement.
     
                    // For example, if (i > j) 
                    //                  if ( i > k ) max = i;
     
                    // Complete the  if-else statement 
                    // ADD CODE AFTER else.
                    // -->        
     
     
                    if (i > j)
                    {
                     	if (i > k)
    			{
    				max = i;
                    	}
     
    			else
    			{
    				max = k;
    			}
     
    		}
    		if (j > i)
                    {
                            if (j > k)
                            {
                                    max = j;
                            }
     
                            else
                            {
                                    max = k;
                            }
     
                    }
    		if (k  > j)
                    {
                            if (k > i)
                            {
                                    max = k;
                            }
     
                            else
                            {
                                    max = i;
                            }
     
                    }
     
                   // Print max as shown in the sample session.
                   // ADD CODE HERE.
                   // -->
     System.out.println("Maximum of Three integers " + i + ", " + j + ", " + k + " is " + max);
     
    }
    }

    Oh and I cannot use && and ||. I have to compare them with < > =, and if else statements.
    Last edited by ak120691; March 2nd, 2011 at 11:30 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Nested-If Problem/ Initializing

    Basically, the problem is that Java needs a guarantee that a variable has been initialized before it can use it. So this won't compile:

    int x;
     
    if(something){
       x = 1;
    }
     
    System.out.println(x);

    Because if something does not evaluate to true, then there is no guarantee that x has been initialized, so there will be no way to do the last line. However, this is okay:

    int x;
     
    if(something){
       x = 1;
    }
    else{
       x = 2;
    }
     
    System.out.println(x);

    Because either way, x will have been initialized. Another way to do that is this:

    int x = 2;
     
    if(something){
       x = 1;
    }
     
    System.out.println(x);

    But that only works if you have a logical default value for the variable.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nested-If Problem/ Initializing

    I understand that. What confuses me is how to compare three variables without using the && and || operators.


    I have never coded for 3 variables. Comparing 2 is simple. When it hits 3 you have to compare the first two and if it still doesn't work out then it'll assign it to whatever is left.


    Could you please look at my code and help me fix the if / else / else if portion?

    I removed that portion and just threw in a simple if/else with two variables and it compiled without a problem.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Nested-If Problem/ Initializing

    Then I guess I don't understand your question. What exactly is the line you're having trouble with? What's the problem? Are you getting an exception? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nested-If Problem/ Initializing

    bash-2.05$ javac MaxOfThree.java
    MaxOfThree.java:98: variable max might not have been initialized
    System.out.println("Maximum of Three integers " + i + ", " + j + ", " + k + " is " + max);

    That is my error. Apparently it won't give the values to max.

    Can someone please fix it? I honestly have tried hours re-editing and re-coding it in all sorts of ways. It will not assign the greatest values to max.

  6. #6
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Nested-If Problem/ Initializing

    You never initialized the "max" variable. I'll try to explain it as easy as I can. To initialize means to set equal to a value, even if that value has the possibility of being changed 100 times over again, the variable still must have an initial value. Without initializing it, Java thinks that it is possible to compile and execute the code without the variable ever getting a value. Simply set it equal to zero when it is declared, and it should run fine.
    Last edited by vanDarg; March 2nd, 2011 at 08:30 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nested-If Problem/ Initializing

    ... I love you lol. Thank you so much!!!

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  2. initializing a java JApplet
    By j_a_lyons in forum Java Theory & Questions
    Replies: 0
    Last Post: January 8th, 2011, 04:49 PM
  3. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 04:31 PM
  4. Nested If Else Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 6th, 2010, 03:46 PM
  5. Nested try blocks
    By Ahmed. in forum Member Introductions
    Replies: 2
    Last Post: April 30th, 2010, 09:12 AM