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");
}
Related
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 just want a simple popup message box that asks for multiple inputs and such.
System.out.println("Please enter Name");
Name = name.nextLine();
System.out.println("Please enter Age");
age=ages.nextInt();
System.out.println("Please enter Gender");
Gender =gender.nextLine();
System.out.println("Please enter Contact Number");
ConNum =number.nextLine();-->
Is there a way to make a gui for this? (its a foundation level project)
Use JOptionPane to take your input:
String name = JOptionPane.showInputDialog(null, "Please enter Name");
Edit:
You could ask the user all the questions on the one box (for this example, ensure they separate the answers by a space).
String input = JOptionPane.showInputDialog(null, "Please enter name.\nPlease enter age.\nPlease enter gender.");
String[] allInput = input.split("\\s+");
System.out.println("Name: " + allInput[0] + " Age: " + allInput[1] + " Gender: " + allInput[2]);
Note: obviously there will be issues, such as not using a space, or using a full name (Jack Sparrow) etc; but it gives you a general idea.
As #notyou mentioned you can use JOptionPane it's very simple to implement it into you code check this example :
import java.io.*;
import javax.swing.*;
public class SystemOutDialog {
public static void main(String[] args) {
// set up a custom print stream
CustomPrintStream printStream = new CustomPrintStream();
System.setOut(printStream);
// from now on, the System.out.println() will shows a dialog message
System.out.println("hello!");
System.out.println("how are you?");
}
}
class CustomPrintStream extends PrintStream {
public CustomPrintStream() {
super(new ByteArrayOutputStream());
}
public void println(String msg) {
JOptionPane.showMessageDialog(null, msg);
}
}
Source link
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...
Here's my code
if(numberbyUser.getText().toString().equals(""))
{
//Toast.makeText(getApplicationContext(), "Please Enter a Number", Toast.LENGTH_SHORT).show();
message = "Please Enter a Number !!";
DialogFunction();
}
If I enter a value for eg: 1.5 code goes into the above loop. But for values which are not decimal code just works fine.
Can somebody please tell me whats going wrong?
I suggest that make the EditText field as a text android:inputType="text" then do the conversion. Try this:
String sNumberByUser = numberbyUser.getText().toString();
Double number = 0.0;
try{
if(!sNumberByUser.equals("")){
// This will accept decimal and non-decimal number
number = Double.parseDouble(sNumberByUser);
//do your process here
}else{
throw new NumberFormatException();
}
//Catch if not a number.
}catch(NumberFormatException nfe){
message = "Please Enter a Number !!";
DialogFunction();
}
can you post the XML for the your edittext...it may be possible that you set the
inputType="number" instead of "decimal..."
<EditText
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="decimal" />
change inputType attribute to "decimal"
if(numberbyUser.getText().length() > 0) {
String numberString = numberByUser.getText().toString();
Double number = Double.parseDouble(numberString);
//To operation with entered number.
} else {
message = "Please enter number.";
DialogFunction();
}
And in your layout add android:inputType = "numberDecimal"
Hope this will help you.
I have a login form which if is success to login. I want to get a name of the logged, here see my code:
try {
if(jTextField1.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Pleass Input Username");
}
else if(jPasswordField1.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please Input Password");
}
else
{
rs = con.executeQuery("SELECT * FROM MsEmployee WHERE EmployeeID='"+jTextField1.getText()+"' And Password='"+jPasswordField1.getText()+"'");
if (rs.next())
{
help.IDemployee = jTextField1.getText();
JOptionPane.showMessageDialog(null, "Login Success! welcome "+ help.IDemployee);
MainMenu mm = new MainMenu(true);
mm.setVisible(true);
this.dispose();
}else
{
JOptionPane.showMessageDialog(null, "Username not found!!");
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
}
So I want to display the EmployeeName from MSEmployee which it got from EmployeeID.
I think something like this:
String qry;
qry = "Select EmployeeName From MsEmplyee WHERE EmployeeID = '" + jtextField1.gettext() + "'" ;
But when I compile, its just print the text above, not get the value. Can anyone help please?
As suggested in a comment, please check which name, MsEmplyee/MsEmployee, is correct.
This is not related to your problem but I think there can be one (possible) improvement in your code.
Instead of using
if(jTextField1.getText().equals(""))
It is better to use trim() method before using equals() method. So if user enters a space in jTextField1, your check will be bypassed in this case. So better to use trim() method to remove all first and last space characters from the string.
if(jTextField1.getText().trim().equals(""))
Same can be done for password field.
You did not read the result set value.
// help.IDemployee = jTextField1.getText();
help.IDemployee = rs.getString(1);
The column index is from 1 for the first column. Not from 0
You Can Try This !
String EmployeeName = rs.getString("Column Label In Database (Like EmployeeName)");
System.out.println(EmployeeName);