Int vs Text on setText for jbuttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Noonga
    New Member
    • Dec 2008
    • 10

    Int vs Text on setText for jbuttons

    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(foo d[recsel].getQuantity()) ;
    VS
    qty.setText(foo d[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!)
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • Noonga
      New Member
      • Dec 2008
      • 10

      #3
      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?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Noonga
        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?
        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

        Comment

        • samido
          New Member
          • Oct 2007
          • 52

          #5
          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 ...

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by samido
            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 ...
            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)

            Comment

            • Noonga
              New Member
              • Dec 2008
              • 10

              #7
              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!

              Comment

              Working...