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

Thread: If Testing, testing for another if?

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

    Default If Testing, testing for another if?

    I was wondering if their is a way to test if an IF test has already been done? So the test would test to see if an if test has already been completed, and then based its actions on if that IF test has or has not been completed?


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: If Testing, testing for another if?

    If statements on their own don't change anything, so it's impossible to tell whether or not they've been executed. It's not uncommon for an if-statement to set a flag (say a boolean variable in Java), so that later code can inspect the flag.

    Do you have some code that illustrates your question?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If Testing, testing for another if?

    a way to test if an IF test has already been done
    That is what programmers do when they scan their code.
    Not sure what you mean by an "if test". Are you referring to the boolean value determined by the conditions in ()s?
    You could set a boolean based on some conditions and use it in multiple if statements later in the program.

    Or give an example of what you are asking.

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

    Default Re: If Testing, testing for another if?

    Sorry, i didn't phrase it very well. I am a beginner, just starting a few weeks ago in a Programming Tech Class in high school. I'm creating a program that compiles data from 4 different friends and using an array list, displaying all of their information. My code is a little lengthy... bare with me. My goal is to once the friend has entered his data for his name to disappear from the prompt when it asks, "Are you Taylor, James, Josh, or Ben?"


    import javax.swing.*;
    import java.util.*;
     
    public class FriendGUI
    {
        private Friend taylorFriend;
        private Friend jamesFriend;
        private Friend joshFriend;
        private Friend benFriend;
        public static String friendName;
        public static String quest;
        public static String airSpeed;
        public static String color;
        public static int age;
        public String benName;
        public String joshName;
        public String jamesName;
        public String taylorName;
        private  ArrayList<Friend> friendList;
     
         public FriendGUI()
         {
            friendList = new ArrayList<Friend>();
            friendName = "";
            quest = "";
            airSpeed = "";
            age = -1;
     
     
     
         }
     
        public void start()
        {
            askQuestion();
     
        }
     
        private void askQuestion()
        {
            friendName = JOptionPane.showInputDialog("Are you Taylor, James, Josh, or Ben?");
     
            if (friendName.equalsIgnoreCase("taylor"))
            {
                displayTaylor();
     
            }
     
            if (friendName.equalsIgnoreCase("ben"))
            {
                displayBen();
     
            }
     
            if (friendName.equalsIgnoreCase("james"))
            {
                displayJames();
            }
     
            if (friendName.equalsIgnoreCase("josh"))
            {
                displayJosh();
            }
     
        }
     
        private void displayTaylor()
        {
            JOptionPane.showMessageDialog(null, "Welcome " + friendName + ", I want to get to know you!");
     
            String quest = JOptionPane.showInputDialog("What is your quest " + friendName + "?");
            JOptionPane.showMessageDialog(null, quest + " is really your quest?  That's so interesting!");
     
            String color = JOptionPane.showInputDialog("What is your favorite color?");
            JOptionPane.showMessageDialog(null, "I've heard " + color + " is a pretty manly color.");
     
            String airSpeed = (JOptionPane.showInputDialog("What is the average airspeed velocity of an unlaiden swallow?"));
     
            if (airSpeed.equalsIgnoreCase("African or European?"))
            {
                JOptionPane.showMessageDialog(null, "Well I don't know!!");
            }
            else
            {
                JOptionPane.showMessageDialog(null, airSpeed + " is WRONG!");
            }
            int age = Integer.parseInt(JOptionPane.showInputDialog("How old are you " + friendName + "?"));
            if (age <= 18)
            {
                JOptionPane.showMessageDialog(null, age    + "?  Really?  Thats pretty young!");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Holy cow!!! " + age + " is old!!");
            }
            JOptionPane.showMessageDialog(null,    "Thank you for giving me all of your private information "    + friendName + "... I will now steal your identity with it");
            taylorFriend = new Friend(friendName, quest, color, airSpeed, age);
        }
     
        private void displayBen()
        {
            JOptionPane.showMessageDialog(null, "Welcome " + friendName + ", I want to get to know you!");
            String quest = JOptionPane.showInputDialog("What is your quest " + friendName + "?");
     
            JOptionPane.showMessageDialog(null, quest + " is really your quest?  That's so interesting!");
     
            String color = JOptionPane.showInputDialog("What is your favorite color?");
            JOptionPane.showMessageDialog(null, "I've heard that only little girls like " + color + ".");
     
            String airSpeed = (JOptionPane.showInputDialog("What is the average airspeed velocity of an unlaiden swallow?"));
     
            if (airSpeed.equalsIgnoreCase("African or European?"))
                    {
                JOptionPane.showMessageDialog(null, "Well I don't know!!");
                    }
            else
            {
            JOptionPane.showMessageDialog(null, airSpeed + " is WRONG!");
            }
            int age = Integer.parseInt(JOptionPane.showInputDialog("How old are you " + friendName +"?"));
             if (age <= 18)
             {
                 JOptionPane.showMessageDialog(null, age + "?  Really?  Thats pretty young!");
             }
             else
             {
                JOptionPane.showMessageDialog(null, "Holy cow! " + age + " is old!");
             }
             JOptionPane.showMessageDialog(null, "Thank you for giving me all of your private information " + friendName + "... I will now steal your identity with it");
        }
     
        private void displayJames()
        {
            JOptionPane.showMessageDialog(null, "Welcome " + friendName + ", I want to get to know you!");
            String quest = JOptionPane.showInputDialog("What is your quest " + friendName + "?");
     
            JOptionPane.showMessageDialog(null, quest + " is really your quest?  That's so interesting!");
     
            String color = JOptionPane.showInputDialog("What is your favorite color?");
            JOptionPane.showMessageDialog(null, "Are you sure that " + color + " is a color?  Don't lie to me James...");
     
            String airSpeed = (JOptionPane.showInputDialog("What is the average airspeed velocity of an unlaiden swallow?"));
     
            if (airSpeed.equalsIgnoreCase("African or European?"))
                    {
                JOptionPane.showMessageDialog(null, "Well I don't know!!");
                    }
            else
            {
            JOptionPane.showMessageDialog(null, airSpeed + " is WRONG!");
            }
            int age = Integer.parseInt(JOptionPane.showInputDialog("How old are you " + friendName +"?"));
             if (age <= 18)
             {
                 JOptionPane.showMessageDialog(null, age + "?  Really?  Thats pretty young!");
             }
             else
             {
                JOptionPane.showMessageDialog(null, "Holy cow!!! " + age + " is old!!");
             }
             JOptionPane.showMessageDialog(null, "Thank you for giving me all of your private information " + friendName + "... I will now steal your identity with it");
        }
     
        private void displayJosh()
        {
            JOptionPane.showMessageDialog(null, "Welcome " + friendName + ", I want to get to know you!");
            String quest = JOptionPane.showInputDialog("What is your quest " + friendName + "?");
     
            JOptionPane.showMessageDialog(null, quest + " is really your quest?  That's so interesting!");
     
            String color = JOptionPane.showInputDialog("What is your favorite color?");
            JOptionPane.showMessageDialog(null, color + " somehow relates to Super Smash Brothers doesn't it?");
     
            String airSpeed = (JOptionPane.showInputDialog("What is the average airspeed velocity of an unlaiden swallow?"));
     
            if (airSpeed.equalsIgnoreCase("African or European?"))
                    {
                JOptionPane.showMessageDialog(null, "Well I don't know!!");
                    }
            else
            {
            JOptionPane.showMessageDialog(null, airSpeed + " is WRONG!");
            }
     
            int age = Integer.parseInt(JOptionPane.showInputDialog("How old are you " + friendName +"?"));
     
            if (age <= 18)
             {
                 JOptionPane.showMessageDialog(null, age + "?  Really?  Thats pretty young!");
             }
             else
             {
                JOptionPane.showMessageDialog(null, "Holy cow!!! " + age + " is old!!");
             }
     
             JOptionPane.showMessageDialog(null, "Thank you for giving me all of your private information " + friendName + "... I will now steal your identity with it.");
        }
     
        public void friendLoop()
        {
             friendList.add(jamesFriend);
             friendList.add(joshFriend);
             friendList.add(benFriend);
             friendList.add(taylorFriend);
     
             for(Friend loopFriend : friendList)
             {
                 JOptionPane.showMessageDialog(null, "So in summary...");
                 JOptionPane.showMessageDialog(null, Friend.getName() + " is his name");
                 JOptionPane.showConfirmDialog(null, Friend.getQuest() + " is his quest");
                 JOptionPane.showConfirmDialog(null, Friend.getColor() + " is his favorite color");
                 JOptionPane.showConfirmDialog(null, Friend.getAirSpeed() + " is what he guessed for the air speed.");
                 JOptionPane.showConfirmDialog(null, Friend.getAge() + " is how old he is.");
     
     
             }
        }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If Testing, testing for another if?

    My goal is to once the friend has entered his data for his name to disappear from the prompt when it asks, "Are you Taylor, James, Josh, or Ben?"
    How are you removing the name from the prompt? If looks the display of the names is in a String literal in the JOptionPane call and not in a String variable where you can remove one of the names.

    What are the separate display.... methods for? That looks like a place where you should have one method that does the job and not one method for each name.

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

    Default Re: If Testing, testing for another if?

    Yeah, my teacher told us that any good programmer would find a 1000 flaws with the way we are building this program. It's not the most efficient program by a long shot. Referring to the removing a name, thats a good idea! Creating each name as a variable and when the method for each friend is complete set their name to equal nothing. Am i understand that right?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If Testing, testing for another if?

    Creating each name as a variable and when the method for each friend is complete set their name to equal nothing
    That sounds like it would work.

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

    Default Re: If Testing, testing for another if?

    So new question, what would be the best way to test this out? This is right at the end of the display methods for all my friends, where just before, I set their name value = "";

    if(benName + joshName + taylorName + jamesName.equals(""))
            {
                friendLoop();
            }
            else
            {
                askQuestion();
            }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If Testing, testing for another if?

    Does that code you posted compile? The syntax of the if condition looks wrong.
    Build a String in one step and test its value in another.

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

    Default Re: If Testing, testing for another if?

    No it doesn't, i was just trying to show my general goal. My goal is for that if statement to check and see if every method has been run through, if it has, it should display everyone's data, if it hasn't, go back to asking questions for the other users.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If Testing, testing for another if?

    That if statement could work if you built the String of results and tested that String.

Similar Threads

  1. [SOLVED] Testing Java EE
    By serdar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 16th, 2011, 04:39 AM
  2. Need Help on testing Code
    By indidude89 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 9th, 2011, 12:53 PM
  3. testing Java jsp/servlets
    By cgodfrey in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: March 30th, 2011, 03:44 PM
  4. SMS Encryption Testing
    By ants_dj07 in forum Java ME (Mobile Edition)
    Replies: 5
    Last Post: March 27th, 2011, 11:14 AM
  5. Testing the DataSource with MySQL
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 0
    Last Post: January 9th, 2010, 02:23 AM