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?
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.
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?
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?
That is what programmers do when they scan their code.a way to test if an IF test has already been done
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.
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."); } } }
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.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?"
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.
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?
That sounds like it would work.Creating each name as a variable and when the method for each friend is complete set their name to equal nothing
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(); }
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.
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.
That if statement could work if you built the String of results and tested that String.