The below code is working perfecly
String input = JOptionPane.showInputDialog(null, "Enter Input",
"Dialog title",JOptionPane.QUESTION_MESSAGE);
Now I have Ok and Cancel buttons. I want to do something like
if(OK is selected){
String input1 = input
do something with input1
}
else if (cancel is selected){
System.dispose();
}
I'm clueless about what to write inside if condition. I know that for ShowOptionDialog I can get an int of selected option and use it but for inputdialog Im not sure how I can get both the selected option and input text.
Could you please help me
So the JavaDocs say
Returns:
user's input, or null meaning the user canceled the input
That mean that something like
String input = JOptionPane.showInputDialog(null, "Enter Input",
"Dialog title",JOptionPane.QUESTION_MESSAGE);
if (input != null) {
// User accepted
} else {
// User cancelled
}
Should work...
Related
I was trying to build a simple UI where a user enters a username and if the username is xyz then the user will be shown "enter your password". If the password is xyz1234 then the user will be shown "please wait loading..." and the scanner exits. If the username was incorrect, I tried to code it in a way so that it keeps asking for the username until the correct username is entered.
Apparently, the first part of the code works, if the username is xyz, then the code works correctly and displays what i want it to. However if the username is wrong in the first attempt and right in the second attempt, it still continues to show "incorrect username" instead of showing "enter the password".
The code is shown below:
import java.util.Scanner;
class User_Authentication
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter your Username");
String username=in.nextLine();
if(username.equals("xyz"))
{
System.out.println("Enter the Password");
String password=in.nextLine();
if(password.equals("xyz1234"))
{
System.exit(0);
System.out.println("Welcome");
System.out.println("Please wait while the system is loading....");
}
else
{
System.out.println("Your system has been locked for security reasons.");
System.out.println("Please contact an administrator to reset the password");
System.out.println("Any attempt to break in the computer will result in self destruction.");
System.exit(0);
}
}
else
{
do
{
if(username.equals("xyz"))
{
break;
}
System.out.println("Incorrect username");
System.out.println("Please try again");
in.nextLine();
}
while((username.equals("xyz"))==false);
}
}
} ```
Your saying in.nextLine() in your do-while loop, rather than username = in.nextLine()
Replace that and it should work, but overall you're do-while loop needs some work, it's relatively messy. Here's how I would approach it.
do {
System.out.println("Incorrect username");
System.out.println("Please try again");
username = in.nextLine();
} while(username.equals("xyz") == false);
I am currently trying to receive an input from a JTextField and store it as an integer in my data. When I use ParseInt(), an error message pops up
Invalid SSN
but when I hardcode my variable SSN with the same text I enter to the JTextField, it works.
Here's the specific section:
try {
ssn = Integer.parseInt(ssnText.getText());
//ssn = 123456789;
if((Integer.toString(ssn).length()) != 9)
{
JOptionPane.showMessageDialog(null,"Invalid SSN","Invalid SSN", JOptionPane.ERROR_MESSAGE);
ssnText.setText("");
return;
}
}
I'm working on a Java project, building a simple system, and it has some methods, one of them is "Change PassWord", I put the user's information (username & password) in a text file called ("Users.txt").
Now this is the description of the method:
boolean ChangePassWord(): Asks the user to enter old password for
verification, the user has at maximum three tries to enter correct old
password; if not the password will not be changed and a message Box
will be shown for the user. If user entered correct old password then
he is authenticated to changer his password and asked to enter new
password and confirming the new. Once if confirmed correctly the old
password will be changed to the new one and a message box will be
shown if wrong confirmation the old password will not be changed and a
message box will be shown.
I wrote this code:
boolean changePassword(){
String pass=JOptionPane.showInputDialog(null, "Enter old password: ", "Input", JOptionPane.QUESTION_MESSAGE);
if(pass.equals(Password)) {
String newpass=JOptionPane.showInputDialog(null,
"Enter new password: ", "Input", JOptionPane.QUESTION_MESSAGE);
String connewpass=JOptionPane.showInputDialog(null,
"Enter confirming new password: ", "Input",
JOptionPane.QUESTION_MESSAGE);
if(newpass.equals(connewpass)){
Password= newpass;
JOptionPane.showMessageDialog(null, "password changed ", "message",
JOptionPane.INFORMATION_MESSAGE);
return true;
}
else
JOptionPane.showMessageDialog(null, "Wrong Conferm ", "message",
JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(null, "Wrong password ", "message",
JOptionPane.INFORMATION_MESSAGE);
return false;
}
but I think that it's wrong, and I need to use a loop I think.
I hope you help me!
A while loop is appropriate for your case. I will briefly explain how this while loop runs 3 times.
So, first n=3. The condition n-- > 0 means 2 things. Check if n is greater than zero and subtract the value of n by 1. These 2 things happen in that exact order.
So the condition checks that n is indeed greater than zero and enters the loop. At the same time n is also decreased by 1 and becomes 3-1=2.
This goes on 3 times. After the 3rd time, n becomes 0. And the condition 0 > 0 is false and therefore the while loop breaks.
boolean changePassword(){
String pass = ""; //get old password from user
int n = 3;
while (n-- > 0) {
if(pass.equals(Password)) {
String newPass = ""; // get new password from user
String conNewPass = ""; // confirm new password from user
if (newPass.equals(conNewPass)) {
Password = newPass;
// password changed
return true;
} else {
// wrong confirmation.. password not changed
return false;
}
}
else {
// tell user to enter the correct old password
pass = ""; // ask user for old password again
}
}
// show error message that user entered the old password 3 times incorrectly
// and return false
return false;
}
// My Scanner
Scanner input = new Scanner(System.in);
//using Do While Loop
do {
//Asking user to enter email
System.out.println("enter your email:");
//Read and safe input in to Var userEmail
String userEmail = input.next();
//Check for contains '#' and '.com' simbols
Pattern pattern = Pattern.compile("\\S+?#\\S+?\\.com");
//And it checking in users entered email
Matcher matcher = pattern.matcher(userEmail);
//if userEmail contain '#'and '.com' print next line
if (matcher.matches()) {
System.out.println("Matches"); // Prints this for this email
}
//if user put email with out '#'and'.com' print next line
else {
System.out.println("your email should
looks like this sample bob.Dillon#gmail.com");
}
// And here I have a problem don't know what to type in
// so that it starts looping until user input will be 100% correct.
} while(!matcher.matches());
Can someone help what needs to be done here while(here); to make it looping?
You want to see if the user entered anything in those fields. So, check like this:
if (INPUTVALUE.length > 0) { //THEY ENTERED SOMETHING
// do something
}
Then, put this in your while statement. Like so:
// My Scanner
Scanner input = new Scanner(System.in);
//using Do While Loop
do{
//Asking user to enter email
System.out.println("enter your email:");
//Read and safe input in to Var userEmail
String userEmail = input.next();
//Check for contains '#' and '.com' simbols
Pattern pattern = Pattern.compile("\\S+?#\\S+?\\.com");
//And it checking in users entered email
Matcher matcher = pattern.matcher(userEmail);
//if userEmail contain '#'and '.com' print next line
if (matcher.matches()) {
System.out.println("Matches"); // Prints this for this email
}
//if user put email with out '#'and'.com' print next line
else{
System.out.println("your email should
looks like this sample bob.Dillon#gmail.com");
}
//And here I have a problem don't know what to type in so that it starts looping until user input will be 100% correct
}while(INPUTVALUE.length > 0);
You need:
}while(INPUTVALUE.length > 0);
To break the loop:
Just erase all of the values that the user has entered at the end of the do. That way, INPUTVALUE.length < 0. That will break the loop ! Good luck !
I have a few buttons on my panel and everytime I click on it an input dialog box appears. It has an inbuilt cancel button. Now, when i click on the cancel button in the beginning of the code without entering the quantity in the dialog box, it says, "This is an invalid" number. This line has to only appear if the user enters alphabets or symbols, and not on pressing cancel. Can we solve this?
First you need a way to decide if a String represents a number; the method below uses Double.valueOf() to decide.
private Double valueOf(String s) {
try {
return Double.valueOf(s);
} catch (NumberFormatException e) {
return null;
}
}
Here's an example of how you might use the method:
private void display() {
String input = JOptionPane.showInputDialog(
null, "Enter a number?", "Number", JOptionPane.QUESTION_MESSAGE);
Double value = valueOf(input);
JOptionPane.showMessageDialog(null, "The value " + input
+ " is " + (value != null ? "valid" : "invalid") + ".");
}
See also How to Make Dialogs.
Try doing,
String Input = JOptionPane.showInputDialog(null,"Enter the number?",
"Number", JOptionPane.QUESTION_MESSAGE);
if (Input.equals(""))
{
JOptionPane.showMessageDialog(null,"This is an invalid number");
}
The following link explains it even better: Simple Data Validation.
String Input = JOptionPane.showInputDialog(null,"Enter the number?",
"Number", JOptionPane.QUESTION_MESSAGE);
if
(Input.matches(("((-|\+)?[0-9]+(\.[0-9]+)?)+"))) {
JOptionPane.showMessageDialog(null,"valid number");
}
else{
JOptionPane.showMessageDialog(null,"This is an invalid number");
}