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");
Related
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
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
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.
Im having a trouble in java. Im creating a HRRN scheduling. I want to print the integer that I input into a textfield area. Please help me to solve this problem. Thankyou!
private void AWTActionPerformed(java.awt.event.ActionEvent evt) {
int firstprocess=1;
if (bt1.getText().equals("")){
double tempbt1 = Double.parseDouble(bt1.getText());
awttotalprocess = (firstprocess + (tempbt1));
AWTCLICK = 0;
jtf_awt.setText(String.valueOf(awttotalprocess+"ms"));
}
I want to print the awttotalprocess into jtf_awt.
Bracketing issue:
jtf_awt.setText(String.valueOf(awttotalprocess)+"ms");
Many classes come with what's called a .toString() method that prints a pre-specified output when joined with a string. You can concatenate or join a string and a variable -in this case an integer- like this:
int i = 50;
String join() {
return "I'm a string, next is a number: " + 50;
}
Keep in mind that int and Integer are different in that the first is a primitive data type, and the second is the object. This isn't an issue for you in this code but in the future if you try to concatenate a string with an object it may end up printing out the memory address as written in the .toString() default method and would require you to #override the method to specify your own string output. The primitive data types are "easier" to combine and don't require such .toString() overriding or .valueOf() shenanigans.
I'm making a GUI program. In my first program I have the following code:
double num1;
num1 = Double.parseDouble(guess.getText());
I believe that this code gets the value from the text field and converts it to double.
How can I get the value and convert it to String or Char?
Since the getText() already returns a String, storing its value as a String is trivial.
In order to parse a double, you've already done it, just watch for the NumberFormatException, in case of invalid input.
To store its value as a char, that depends on your requirements. Do you want the first character? Do you require the string to have only a single character? Is any character valid? And so on.
// Storing the value as a String.
String value = guess.getText();
// Storing the value as a double.
double doubleValue;
try {
doubleValue = Double.parseDouble(value);
} catch (NumberFormatException e) {
// Invalid double String.
}
// Storing the value as a char.
char firstChar = value.length() > 0 ? value.charAt(0) : (char) 0;
// Require the String to have exactly one character.
if (value.length() != 1) {
// Error state.
}
char charValue = value.charAt(0);
use String.valueOf() instead of Double.parseDouble() this will help you convert double into string value
The getText() already returns the text as String.
Btw, be careful of Exceptions due to parse error. But your on the right track. :)
The getText()method returns a String. when you use .parseDouble what you are really doing is turning the string the user entered and into a double therefore in the case of a string you do not have to use a .parse method because the value called is already a string. In the case of a character you would have to use something like this:
String text = jTextField1.getText();
if (text.length() > 1 && !text.contains(" ") && !text.contains(",")) {
//make sure that its length is not over 1, and that it has no spaces and no commas
char ch = text;
} else {
//if a space or comma was found no matter how big the text it will execute the else..
System.out.println("this is not allowed");
jTextField1.setText("");
}
The getText() function already fetches a String value from the Textfield.
For example:
String var = guess.getText();
Here, var is storing the String value received from the Textfield.