Android Studio edit text numeric type - java

I need to store an integer value into database, so I need to convert the value of the textfields into integers and for some reason I'm getting this exception:
java.lang.NumberFormatException: Invalid int: "android.widget.EditText{431fbec VFED..CL. ........ 0,250-1080,386 #7f0b0076
I set the editText to be number type, but when I want to add the value of that field to the integer variable I get the exception above.
Integer year = Integer.parseInt(editTextYear.toString());
This line of code is the problem; I tried to first create the string and then parse the value to int but it doesn't work.

Use getText() property to get input from EditText. toString() property on object will return you object representation in String form.
final String input = editTextYear.getText().toString(); // To get input and
// then parse it
if(input!= null)
Integer year = Integer.parseInt(input);

Rasi is essentially right, but I'd use isEmpty instead of null.
So instead of
editTextYear.toString()
it should be
editTextYear.getText().toString()
but to avoid a nullpointerexception, just do:
if(!input.isEmpty)
to avoid having either an empty or a null value there

Related

What happens to a int i = Integer.parseInt(string) when there are no integers in string?

Right now, I am making a simple tic-tac-toe project. I would like to know what happens to the integer i when:
string s = "hello"; //or something else, non integer
int i = Integer.parseInt(s);
What will i be equal to?
Integer.parseInt("hello") statement will throw an exception: java.lang.NumberFormatException
If the given string does not contain a parseable integer, a
NumberFormatException will be thrown.
For more information, see: https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29

How do I get user input on a jForm?

I can use numbers and stuff like
num1 = Float.parseFloat(txt1.getText());
but I am looking to get words from a text field, then do calculations based on which word. Like:
String input1;
if (input1.equalsIgnorecase("Word")) {
2 + 2; }
I just don't know how to do that on a jform.
As pointed out by #MadProgrammer, .getText() will help you in retrieving String values from a test field.
For example, If your Java Form contains a text field whose variable name is jTextField1, you can retreive value from it by:
String input1 = jTextFielf1.getText();
Just for information,
float num1 = Float.parseFloat(txt1.getText());
also does the same thing, it first gets the string value from the text field and converts it into a float value.

Java have a int value using setText

I'm trying to set an int value using jTextField and the setText method. But of course setText wants a String. How do I get round this? I'll give you a snippet of the code:
private void setAllTextFields(FilmSystem e){
getFilmNameTF().setText(e.getFilmName());
lectureTF.setText(e.getLecture());
ageTF.setText(e.getAge());
priceTF.setText(e.getTicketCost());
seatsTF.setText(e.getNoOfSeats());
seatsTF is a jTextField and getNoOfSeats is a method in another class that returns a int value.
Thanks again for answering this question. Now how would I go about getting the value of the int to do something to do?
public void buyTicket() {
String newFilmName = filmNameTF.getText();
String newLecture = lectureTF.getText();
String newAge = ageTF.getText();
String newPrice = priceTF.getText();
int newSeats = seatsTF.
As you can see the code, the String values I can get easy with getText. I can then print them out or whatever with them. How can I do this with the seats int? Thanks again.
String#valueOf convert your int to String.
String.valueOf(e.getAge()); will return the string representation of the int argument.
seatsTF.setText(String.valueOf(e.Age()));
...
USe
seatsTF.setText(""+e.getNoOfSeats());
OR
seatsTF.setText(String.valueOf(e.getNoOfSeats()));
Normal ways would be
seatsTF.setText(Integer.toString(e.getNoOfSeats()));
or
seatsTF.setText(String.valueOf(e.getNoOfSeats()));
but, this can be achieved with a concatenation like this:
seatsTF.setText("" + e.getNoOfSeats());
Assuming age field is of type int, you could try something like:
ageTF.setText( Integer.toString(e.getAge()) );
Setting an int converting it to a String not a big deal. Displaying a value is a problem. To take care of how the value is displayed properly in the textfield you may use a DecimalFormat to format the numeric value. But may be the number is locale specific then you need NumberFormat instance
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setMaximumIntegerDigits(12);
nf.setMaximumFractionDigits(0);
nf.setMinimumFractionDigits(0);
String s = nf.format(e.getNoOfSeats());
seatsTF.setText(s);
You may also need to read the tutorial on how to use the DecimalFormat.
To convert Integer Value to String you should
MedicineTM medicine=tblmedicine.getSelectionModel().getSelectedItem();
txtmedicine.setText(medicine.getMID());
txtDescription.setText(medicine.getDescription());
txtQty.setText(String.valueOf(medicine.getQty())); // this is what i did
cmbApproval.setValue(medicine.getApproval());
I think you should write the code as
seatsTF.setText(e.getNoOfSeats().toString());

String to int gives nullpointerexception

When I run this code the conversion of a string to int is printing out NULL ? When I print out the string it gives me a string number, but when I try to convert that string into a int it says null, why is that?
for(int j = 0; j < removetrack.size(); j++){
String removetrackArray[] = removetrack.get(j).split(" ");
String candidateBefore = "";
int removetracklocation = Arrays.asList(removetrackArray).indexOf(past)-1;
if(removetracklocation != 1) {
String candidateBefore = "";
System.out.println(removetrack.get(j)+" location = "+ removetracklocation +" "+
(past)+" candidate name "+dictionary.get(votedfor) );
candidateBefore= Arrays.asList(removetrackArray).get(removetracklocation+1);
System.out.println(" this is a string "+candidateBefore);
System.out.println( Integer.getInteger(candidateBefore));
}
}
Integer.getInteger does not cast your string into an int, int returns the value of a system property (see http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#getInteger(java.lang.String)). You should be using Integer.parseInt instead.
Javadoc to the rescue:
Determines the integer value of the
system property with the specified
name.
Use Integer.parseInt to transform the string into an int, and Integer.valueOf to transform the string into an Integer.
From the docs for Integer.getInteger:
Determines the integer value of the system property with the specified name.
The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty.
If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.
In other words, it doesn't parse an integer. To parse an integer, either use Integer.parseInt (to get an int) or Integer.valueOf (to get an Integer).
However, even your description is slightly odd - you claim in the title that it's giving you a NullPointerException, but you then say it's printing null. Which is it? They're very different. I can't see how the code you've given us would throw a NullPointerException at Integer.getInteger.
Alternatively, if this is a value entered by a user, you may want to use java.text.NumberFormat instead.
Integer.getInteger() is used for system properties:
Integer.getInteger("sun.arch.data.model");

How do I return an int from EditText? (Android)

Basically, I want an EditText in Android where I can have an integer value entered into. Perhaps there is a more appropriate object than EditText for this?
For now, use an EditText. Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer.parseInt(myEditText.getText().toString())).
In the future, you might consider a NumberPicker widget, once that becomes available (slated to be in Honeycomb).
Set the digits attribute to true, which will cause it to only allow number inputs.
Then do Integer.valueOf(editText.getText()) to get an int value out.
First of all get a string from an EDITTEXT and then convert this string into integer like
String no=myTxt.getText().toString(); //this will get a string
int no2=Integer.parseInt(no); //this will get a no from the string
You can do this in 2 steps:
1: Change the input type(In your EditText field) in the layout file to android:inputType="number"
2: Use int a = Integer.parseInt(yourEditTextObject.getText().toString());

Categories

Resources