This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Parse a string to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parse a string to int" Watch "Parse a string to int" New topic

Parse a string to int

S. Stacey

Joined: Jul 03, 2003
Posts: 20
Hello All,
I am trying to parse a string to an int with the following code:
Integer pa = Integer.parseInt(txtAudio.getText());
When I complie the code I get the following error: incompatible types
found : int
required: java.lang.Integer
Integer pa = Integer.parseInt(txtAudio.getText());
I have import the java.lang.Integer class and that did not fix the error.
Thanks for your help
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

You don't need to import java.lang.Integer, as every class in java.lang is implicitly imported.
The message is telling you that this method returns "int", but you're assigning the result to an Integer. "Integer" is a kind of object, while "int" is a primitive type. You most likely watt to use "int" here instead -- i.e., pa should be an int, not an Integer.
If you really do want pa to be an Integer, then parseInt is the wrong method to call -- use "new Integer(txtAudio.getText())" instead.

[Jess in Action][AskingGoodQuestions]
S. Stacey

Joined: Jul 03, 2003
Posts: 20
Thanks Ernest that worked....:-)
I agree. Here's the link:
subject: Parse a string to int
It's not a secret anymore!