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

Thread: while instead of if-else

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default while instead of if-else

    Hi, I'm taking JAVA JDK2 course in college and I need some help please.

    I need to know how to use (while) statement instead of (if-else) to execute the program again in case if the input is not correct.

    --------------------------------------------------------------------------------------------------------------------------------
    import java.util.Scanner;
    import javax.swing.*;
     
    public class Main {
     
     
        public static void main(String[] args) {
     
            String name;
            String passw;     
     
     
     
     
            name= JOptionPane.showInputDialog("Enter Your Name Please: ");
            if (name.equals("fahad"))
            {
     
    passw=  JOptionPane.showInputDialog("Your Password: ");
     
             if (passw.equals("123"))
             {
                      JOptionPane.showMessageDialog(null, "Welcome " + name + " Successful Login");
             }
     
                else
                {
                 JOptionPane.showMessageDialog(null,"Incorrect input, please exit the program!! ");
                }
     
     
     
            }
             else
            {
               JOptionPane.showMessageDialog(null,"Incorrect input, please exit the program!! ");
            }
     
     
     
     
     
        }
    }
    --------------------------------------------------------------------------------------------------------------------------------

  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: while instead of if-else

    Hello Fahad,

    Welcome to the Java Programming Forums

    How about this?

    import javax.swing.*;
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		[B]boolean locked = true;[/B]
    		String name;
    		String passw;
     
    		name = JOptionPane.showInputDialog("Enter Your Name Please: ");
     
    		[B]while (locked) {[/B]
    			if (name.equals("fahad")) {
     
    				passw = JOptionPane.showInputDialog("Your Password: ");
     
    				if (passw.equals("123")) {
    					JOptionPane.showMessageDialog(null, "Welcome " + name
    							+ " Successful Login");
    					[B]locked = false;[/B]
    				}
     
    				else {
    					JOptionPane.showMessageDialog(null,
    							"Incorrect input, please exit the program!! ");
    				}
     
    			} else {
    				JOptionPane.showMessageDialog(null,
    						"Incorrect input, please exit the program!! ");
    			}
    		[B]}[/B]
    	}
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while instead of if-else

    thaanks a lot , it worked for the password but it doesn’t worked for the name what should I do?

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: while instead of if-else

    You could do something like this:

    import javax.swing.*;
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		boolean nameLocked = true;
    		String name;
     
    		name = JOptionPane.showInputDialog("Enter Your Name Please: ");
     
    		while (nameLocked) {
     
    			if (name.equals("fahad")) {
     
    				nameLocked = false;
    				passBox();
     
    			} else {
     
    				name = JOptionPane
    						.showInputDialog("Incorrect Username! Enter Your Name Please: ");
    			}
     
    		}
     
    	}
     
    	public static void passBox() {
     
    		boolean passLocked = true;
    		String passw;
     
    		while (passLocked) {
    			passw = JOptionPane.showInputDialog("Your Password: ");
     
    			if (passw.equals("123")) {
    				JOptionPane.showMessageDialog(null, "Successful Login");
    				passLocked = false;
     
    			}
     
    			else {
    				JOptionPane.showMessageDialog(null,
    						"Incorrect input, please exit or re enter password ");
    			}
     
    		}
     
    	}
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.