0

So my problem is that I want the program to show the "You are not allowed to pass" screen ONLY when I'm fulfilling the condition which is to NOT write "xma", else I want it to just show me the "Hello Mr.xma" screen.

Can you guys tell me what am I missing in my program?

Also what should normally be written where I wrote null? I only wrote it (null) cause the instructor did so.

package com.xma91;

import javax.swing.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
    // write your code here
        
        Scanner scanner = new Scanner(System.in);
        String name = "";
        while (!name.equals("xma")) {
            name = JOptionPane.showInputDialog(null, "Whats your name?");
            JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
        }
        JOptionPane.showMessageDialog(null, "Hello Mr."+name+"!");


    }
}
2
  • 1
    Scanner is (usually) for reading text input from the console (System.in). It is entirely separate from JOptionPane (Swing). Aside from wasting System.in, this looks like it should work; if it doesn't you need to explain in more detail what the behavior is. Commented Sep 5, 2021 at 5:23
  • "Also what should normally be written where I wrote null? I only wrote it (null) cause the instructor did so." What is the difference between frame and null in showMessageDialog in Java?, But in the future please ask one question per post Commented Sep 5, 2021 at 7:06

2 Answers 2

1

I would do this:

while (true) {
        name = JOptionPane.showInputDialog(null, "Whats your name?");
        if (name.equals("xma")) {
          break;
        }
        JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
}

So that you check the name immediately after you read it, and leave the loop if it is correct, before you show the error message.

To answer your question about the first parameter of showMessageDialog: please read the Javadoc. You can probably navigate directly to it in your IDE, but in any case learning how to get to the Javadoc for functions you are using is very important.

Sign up to request clarification or add additional context in comments.

Comments

1

Take a moment to look over your code...

String name = "";
while (!name.equals("xma")) {
    name = JOptionPane.showInputDialog(null, "Whats your name?");
    JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
}

So your code is basically saying, while name is not equal to "xma", show error message, then prompt for name

So, in order to get this to work (in it's current state), after you've got the input from the user, you'd need to test the name value and display the error message if it's not correct and check it again when testing if your should exit the loop.

while (!name.equals("xma")) {
    name = JOptionPane.showInputDialog(null, "Whats your name?");
    if (!"xma".equals(name)) {
        JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
    }
}

This is some what inefficient, as you need two places to check the condition. Very easy to screw that up.

Arguably, you could use a state flag instead, for example...

boolean isCorrect = false;
while (!isCorrect) {
    name = JOptionPane.showInputDialog(null, "Whats your name?");
    isCorrect = "xma".equals(name);
    if (!isCorrect) {
        JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
    }
}

Now, we're only checking the condition once and then responding to the flag.

But, we could do something a little bit different. The fact is, you have to enter the loop at least once anyway, so a while-do loop is necessarily the right approach (it works, don't get me wrong). Instead, I'd approach the problem from a do-while point of view.

This basically puts the conditional check at the end of the loop, instead of the start...

String name = null;
do {
    if (name != null) {
        JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
    }
    name = JOptionPane.showInputDialog(null, "Whats your name?");
} while (!"xma".equals(name));
JOptionPane.showMessageDialog(null, "Hello Mr." + name + "!");

Now, this is simular to the "state flag" idea presented above, except, we're using the name as the flag instead. It's assumed that if name is not null and we've gone back to the start of look, then name is incorrect (because otherwise we'd have exited the loop).

A a side effect, see what happens if you press the "cancel" button ;)

Last but not least, we could merge the two ideas. That is, we use a state flag and a do-while loop. This workflow might make it a bit easier to understand, as it flows in a natural way, that, you ask for the name, validate it and then display the error message...

boolean isCorrect = false;
String name = null;
do {
    name = JOptionPane.showInputDialog(null, "Whats your name?");
    isCorrect = "xma".equals(name);
    if (isCorrect) {
        JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
    }
} while (!isCorrect);
JOptionPane.showMessageDialog(null, "Hello Mr." + name + "!");

This highlights the fact, that there is a number of ways to skin this solution ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.