Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Incompatiable variable types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incompatiable variable types" Watch "Incompatiable variable types" New topic
Author

Incompatiable variable types

LaTeef Lusk
Greenhorn

Joined: Mar 28, 2009
Posts: 15
I am trying to compile my applet and keep getting the following error. It keeps telling me that it found a Variable String type but I am only defining type int. My code is below. Can anyonejust point me in the right direction please. Thanks!






[Edit: put code between [code] and [/code] UBB tags.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41088
    
  43
Take a look at the javadocs of the java.awt.TextField class. You'll find that it has neither a "setText(int)" nor an "int = getText()" method. That's what the error message is telling you.

You'll need to convert the int to a String for use with the setText method, and you need to convert the String result of getText to an int to use it on your program.

(I'll move the topic to a different forum, as there's nothing applet-specific about it.)


Ping & DNS - my free Android networking tools app
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

In lines 7 & 8 you have defined farenheitInput and celsiusInput as TextField. According to the API, "A TextField object is a text component that allows for the editing of a single line of text."

For your first error at line 43, you are using the getText() method to get the data from the farenheitInput field. According to the API (and as the name suggests), this method "Returns the text that is presented by this text component. By default, this is an empty string."

So the error message is correct: it found that you are getting text from the TextField and attempting to assign this text to an integer.

What you need to do in this case is find a method that will convert a String to an int. I would recommend looking at the Integer class, and seeing if there is a method that can parse the String for you.

Does this help you determine what the likely cause of the second error is?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Durn, beaten by a few minutes by Ulf.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
. . . and welcome to Beginners, where Rob and I can race to post the next answer
 
Don't get me started about those stupid light bulbs.
 
subject: Incompatiable variable types
 
Similar Threads
How do I start the Java Virtual Machine
Button not working in applet
beginner's question
Reading the values of csv file and displaying it in AWT table
non-static method init() cannot be referenced from a static context