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

Thread: Help with nested if statements.

  1. #1
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help with nested if statements.

    Hi there,
    I have tried for ages to figure out why this won't work and I think it must be something very
    simple, but I'm unable to get it working. After the menu loop the user enters in a valid option, namely A, B, C or X. The only option that works is option A. I'm just trying to get the program functioning at the moment and I'm a beginner programmer so any assistance would be very much appreciated.


    //This program allows the user to select from three menu options
    //The menu loops back if a valid option isn't chosen and the
    //program is exited if the user selects x 
    //=====================================
    import java.util.Scanner;
     
     
     
    public class Assignment1{
       public static void main (String [] args){
          Scanner input = new Scanner(System.in);
     
          char selectedOption = 0;
          char primeNumberChecker = 'A';
          char stampDutyCalculator = 'B';
          char vowelCounter = 'C';
          char exit = 'X';
     
    		//Start the menu loop
          while (selectedOption != primeNumberChecker && selectedOption != stampDutyCalculator &&
                selectedOption != vowelCounter && selectedOption != exit)
          {
             //Display application menu to user
             System.out.println("*************** Assignment 1 Menu ****************\n");
             System.out.println(primeNumberChecker + " Prime number checker");
             System.out.println(stampDutyCalculator + " Stamp duty calculator.");
             System.out.println(vowelCounter + " Vowel counter.");
             System.out.println(exit + " exit option.");
             System.out.println("\n**************************************************");
             System.out.println("Please select one of the above options: ");
     
             //Capture selected option
     
             selectedOption = input.next(".").charAt(0);
     
     
     
             if (selectedOption == primeNumberChecker)            
             {
     
                int num;
                System.out.print("Enter an integer value: ");
                num = input.nextInt();
                int i;
                for (i=2; i < num ;i++ )
                {
     
                   if (num % i == 0)
                      {
                         System.out.println(num + " is not a prime number!");
                         break;
                      }
                }
                if(i == num)
                {
                   System.out.println(num + " is a prime number!");
     
              }else if (selectedOption == stampDutyCalculator) 
              {
                final double THRESHOLD_ONE = 20000;
                final double THRESHOLD_TWO = 115000;
                final double THRESHOLD_THREE = 870000;
                final double THRESHOLD_FOUR = 870001;
     
                double purchasePrice;
                double stampDutyPayable = 0;
                double difference;
     
                System.out.print("Enter a purchase value: ");
     
                purchasePrice = input.nextInt();
     
                if (purchasePrice <= 20000)
                {
                    stampDutyPayable = (purchasePrice * 1.4) / 100;
                }else if (purchasePrice > THRESHOLD_ONE && purchasePrice <= THRESHOLD_TWO)
                {
                    difference = purchasePrice - THRESHOLD_ONE;
                    stampDutyPayable = (THRESHOLD_ONE*1.4) / 100 + (difference*2.4) / 100;
                }else if (purchasePrice > THRESHOLD_TWO && purchasePrice <= THRESHOLD_THREE)
                {
                    difference = purchasePrice - THRESHOLD_TWO;
                    stampDutyPayable = (THRESHOLD_TWO * 2.4) / 100 + (difference * 6) / 100;
                }else if (purchasePrice >= THRESHOLD_FOUR)
                {
                    stampDutyPayable = (purchasePrice * 5.5) / 100;
                }
                System.out.println("Your stamp duty payable: $" + stampDutyPayable);
             }else if (selectedOption == vowelCounter)
             {
                char vCounter[] = new char[5];
     
                vCounter[0] = 'a';
                vCounter[1] = 'e';
                vCounter[2] = 'i';
                vCounter[3] = 'o';
                vCounter[4] = 'u';
     
                //Prompt user to input a string
                int counter = 0;
     
                System.out.println("Enter a string of text: ");
                //Hold the input in the String object aString
                String aString;
                aString = input.nextLine();
                if (aString.isEmpty())
                {
                    aString = input.nextLine();
                }
     
                for (int k = 0; k < aString.length(); k++)
                {
                    char currentChar = aString.charAt(k);
     
                    for (int j = 0; j < vCounter.length; j++ )
                    {
                        if (currentChar == vCounter[j])
                        {
                            counter++;
                        }
     
                    }
                }
                System.out.println("Number of vowels found in line is " + counter);
             }else if (selectedOption == exit)
             {
                System.out.println("Exiting the program - goodbye...");
             }
     
          }  
        }	
      }	 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with nested if statements.

    The only option that works is option A.
    Can you explain what the problem is with the other options?
    What happens when you try to use them?

  3. #3
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with nested if statements.

    Hi Norm,
    When I select B,C or X the program terminates.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with nested if statements.

    Your problem is due to incorrect nesting of your if/else if statements.
    if (selectedOption == primeNumberChecker)
    {
        // snip
        if(i == num)
        {
            System.out.println(num + " is a prime number!");
    }else if (selectedOption == stampDutyCalculator)
    Due to your indentation it appears that the else if statment is part of the outer if statement. Whereas it is actually the else if to the i==num if statement and therefore inside the outer if statement.

  5. The Following 2 Users Say Thank You to Junky For This Useful Post:

    JavaPF (June 15th, 2011), joon (June 15th, 2011)

  6. #5
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with nested if statements.

    Lol. Thanks heaps for your help Junky. I thought it had something to do with the curly brackets being
    positioned incorrectly. There was an extra one down the bottom which should not have been there. Thanks
    again for solving my problem.

  7. #6
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with nested if statements.

    I have another question regarding this thread. Could someone tell me what I need to do to be able to loop back to the menu options after an option has been selected and executed. At the moment the program just ends after an option has been chosen but I only want it to end when the user selects X. Thank you in advance for your help.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with nested if statements.

    need to do to be able to loop back to the menu options after an option has been selected and executed.
    Use a loop that only exits when the user selects an option to exit the loop.

    What does your code do now? It has a while loop?

  9. #8
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Help with nested if statements.

    Quote Originally Posted by joon View Post
    Could someone tell me what I need to do to be able to loop back to the menu options after an option has been selected and executed. At the moment the program just ends after an option has been chosen but I only want it to end when the user selects X.


    while (selectedOption != primeNumberChecker && selectedOption != stampDutyCalculator &&
                selectedOption != vowelCounter && selectedOption != exit)
          {


    You have your answer in your while statement.

    while(selectedOption != exit)

  10. The Following User Says Thank You to william For This Useful Post:

    joon (June 17th, 2011)

  11. #9
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with nested if statements.

    Oh man, I think I should give up trying to learn programming. I was starring at your sentence for a good 3 minutes before I actually understood. It's so logical but I couldn't figure it out. Thanks for that William.

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  2. regarding nested queries in jsp
    By ravi_91 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2011, 10:15 AM
  3. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM
  4. Nested If Else Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 6th, 2010, 02:46 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM