How to put integer value in JLabel? - java

I need to display an integer onto JLabel, the following code does not work out well, even with Integer.parse().
How do I rectify it?
JLabel lblTemp = new JLabel("");
lblTemp.setBounds(338, 26, 46, 14);
contentPane.add(lblTemp);
//store int value of item clicked # JList
int temp = list.getSelectedIndex() + 1;
lblTemp.setText(temp); // <- problem

Use String.valueOf method :
Returns the string representation of the int argument.
lblTemp.setText(String.valueOf(temp));

lblTemp.setText(String.valueOf(temp));
Your temp is an integer but the type that the setText(...) method accepts is String. You need to convert first your integer to String.

The quick and dirty solution for putting integers in places that expect strings is to do the following:
lblTemp.setText(temp + "");
I hope this helps.

setText() takes string as an argument. Use this line to code to convert int to string.
Integer.toString(number)
Hope it helps.

If you use Wrapper class Integer instead of primitive type int then you can get temp.toString() method that automatically convert to string value

You can Use String.valueOf() or Integer.toString() Methods
lblTemp.setText(String.valueOf(temp));
or
lblTemp.setText(Integer.toString(temp));

Related

How to add a char into an array using add?

I am trying to coding Java and adding a char to an array. My idea is compare two char, if they are different, I will add them to an array and here is my code.
ArrayList<String> different = new ArrayList<>();
if (character.charAt(i) == (character.charAt(i+1)))
{
}
else
{
different.add(character.charAt(i+1));
}
But when I run my code, they said to me that "no suitable method found for add (char)" at line 6, and I can not run the code. Could you please give me some ideas? Thank you very much for your help.
The problem is that you are adding char datatype to an arraylist which is of type String. You need to change the code to:
ArrayList<Character> different = new ArrayList<>();
if (character.charAt(i) != (character.charAt(i + 1))) {
different.add(character.charAt(i + 1));
}
String::charAt returns a char, not a String. So your char cannot be put into a list holding String objects. Square peg, round hole.
Also char is a legacy type, limited to a subset of less than half of the over 110,000 characters defined in Unicode, restricted to code points in the “basic plane”. You should learn to work with code points as numbers to be able to handle any Unicode characters.
When you declared List<String> or ArrayList<String>, it means the List should contain type String or literals(something between double quote "". FYI, this is called Generic Type.
Use String#valueOf(char c) to create new String from a char. Example:-
//returns a type char
char c = character.charAt(i+1);
//use String#valueOf(char c) to create new `String` from variable `c`
different.add(String.valueOf(c));
** Oracle has good tutorial on Generic Type, you could refer to it.
https://docs.oracle.com/javase/tutorial/java/generics/index.html
ArrayList<Character> different = new ArrayList<>();
if (character.charAt(i) == (character.charAt(i+1)))
{
}
else
{
different.add(character.charAt(i+1));
}
///////////////////
Just add Wrapper class Character instead of String.

Printing integer to textfield area

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.

How do I convert the content of System.out.print to a String variable in java

I have this line of code:
System.out.print(postalCodeIndex.findClosestBruteForce(latitude, longitude));
It returns output from a text file that was ran through an algorithm. For example the output is can be : "A0E 2Z0 Monkstown Newfoundland NL D [47.150300:-55.299500]". I would like to convert that output to a string so I can use it in a javafx GUI text. Is that posible?
System.out.print accepts a String as a parameter, in fact, anything that you send it will be converted to a String in order for it to be displayed.
Using the following code, you could then put the result of the postalCodeIndex method call into a variable called myString.
String myString = postalCodeIndex.findClosestBruteForce(latitude, longitude).toString();
It might be worth your while remembering that the process in the System.out.print() code sample works as follows:
postalCodeIndex is called FIRST, creating a temporary String in-place because the .toString() method is called on your behalf.
The System.out.print method is only called AFTER the postalCodeIndex method has returned, because System.out.print requires this returned String to enable it to print something to the console.
postalCodeIndex.findClosestBruteForce(latitude, longitude)
this method it self would returning a String or if not you can do like
String str = postalCodeIndex.findClosestBruteForce(latitude, longitude).toString();
Based on the code you've given, the code inside the System.out.print() call will return a PostalCode object. So to get a string you could do something like:
String x = postalCodeIndex.findClosestBruteForce(latitude, longitude).toString();
//use x as String

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());

Java - Set a "Int" in a textfield

I'd like to set a int in a textfield to represent a ID. This int will be incremented when the user clicks the button Next. I'm using awt. I tried to do this but it gives a error because it expects a string. :( Is there a solution?
Thanks
Sounds like you will need to keep an internal int variable which you can increment, then update the textfield with the String representation when it's changed. Similarly make sure to update the int if the user manually edits the textfield.
How about public static String String#valueOf(int)
One can convert an int to a String with either
int i = 100;
String s2 = String.valueOf(i);
String s1 = "" + i;
I believe that valueof() is the preferred approach because it can re-use static data for the value.

Categories

Resources