473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Int vs Text on setText for jbuttons

10
I have tried to search, maybe I did not locate the right information since I am new to these forums and new to java, but, I have a quick question...

I am working on an inventory program for a school homework assignment.. #5 to be exact, I am sure you guys are aware of them...

I have tried to find out exactly why this works this way but searching the web and other places I still can't put my finger on this simple question..

qty.setText(food[recsel].getQuantity());
VS
qty.setText(food[recsel].getQuantity() + "");

The first one gives me the invalid type (String vs Int) but the second one works perfect. It does calculations on the field and performs just like an int field.

Is there someplace that someone can point to me that really puts this in easy terms to explain? I understand the theory, being in programming cobol, etc, for a while, but for some reason I just can't get the first one to work right...

I tried using Double, Int, and other forms to set the information returned from getQuantity, but to no avail unless I use + ""

Thanks!
(Hope this posts ok, no clue how to post code yet, so bear with me!)
Dec 18 '08 #1
6 5553
Ganon11
3,652 Expert 2GB
setText takes a String argument, and requires a String. An int, double, char, etc. won't work. By adding the + "", you are performing string concatenation. Java automatically treats your number (int, double, whatever) as a String (by using its .toString() method or some equivalent), then appends it with the empty string "".

It's one of those weird things that you just have to get used to.
Dec 18 '08 #2
Noonga
10
Ok, as long as no one is going to give me grief about it. Is that a common shortcut or should I get used to doing it some other way?
Dec 18 '08 #3
JosAH
11,448 Expert 8TB
@Noonga
That's a common way: the compiler takes care of the overloading of the + operator and creates a StringBuilder that catenates the (empty) String to your int.

kind regards,

Jos
Dec 18 '08 #4
samido
52
my friend, there is no magics in Java, but only OOP language, the method setText is provided by the compiler for you to use and takes only string parameter type, so you want this method to take 'int' parameter which it wasn't declared like that, there is nothing to confuse you here just create your own method(Or Overload) with one that take 'int' type and call it and you will see that it will work e.g.

public void setText(int arg){
myInt = arg;
}

this shoul help you ...
Dec 18 '08 #5
JosAH
11,448 Expert 8TB
@samido
No it doesn't help; the compiler doesn't 'provide' methods for you. Overloading that method with an int parameter doesn't help you much: you could've supplied a simple String parameter ""+args instead. Please read your literature before you post this incorrect advice. There is already confusion enough in these forums; no need to increase it.

kind regards,

Jos (moderator)
Dec 18 '08 #6
Noonga
10
Jos and everyone,

Thanks for the update. I didn't think it could be that simple to force it to do something like that. I appreciate the input a ton!
Dec 19 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: tonyg | last post by:
Hi everyone, I was wondering if there is an efficient or fast or preferred way to open text files in java. I want to load the text file into a JTextPane. The way I do it now is... ........
15
by: Henning Vestergaard Poulsen | last post by:
Hi, I have a problem that I hope someone can help me with. I'm building a web page with pictures I've taken with my digital camera. I have succeded making a javacript that, when clicking on a...
5
by: CQMMAN | last post by:
Hello, I am trying to change a piece of text using onmouseover. I have tried a few things so far and nothing has really worked. I get get a new piece of text to appear but bastardizing someone...
1
by: Maurice Mertens | last post by:
Hi, I'm currently working on an app in VB.NET 2005 which uses a splashscreen. In the 'my project' settings I used the 'Spash screen' setting to set the form that will be the splash screen. ...
4
by: sherifffruitfly | last post by:
Hi, I'm using StatusBar text to inform the user what's going on in the application. I pass a reference to from the main form's class to other (instances of) classes that are doing the actual...
6
by: fifersheep | last post by:
Hi, there, very new to Java. I'm looking to get a number from a text box, then use the number in a calculation, and then put the answer into another text box. JButton convertButton = new...
4
by: Badboy112 | last post by:
Hi guys i have used a data file like you guys have asked me to but im not entirely sure how to use the DataInputStream What i would like to do for this assigment is import the text from the data...
1
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up...
0
by: yatinbvn | last post by:
hi , my name is yatin i have creating swt text with vertical scroll bar in shell, my question is: vertical scroll bar thumb is still visible in swt text when its size is not increase , i want...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.