There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
wrapper class in Java


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Caliraj's Avatar
Senior Member with 145 posts.
 
Join Date: Mar 2006
Experience: Intermediate
08-Jan-2007, 12:47 AM #1
wrapper class in Java
Hi,
I was going through the wrapper classes aspect in Java, where I had come across the utility methods llike valueOf(),toString etc.I have the follwing program :
class Wrappers
{
public static void main(String args[])
{
int j=10;
String str=Integer.toString(j);
System.out.println("The string value of the Integer is "+str);
String st="Hello";
Integer x=String.valueOf(st);
System.out.println("The Integer value of the string is "+x);
}
}

Firstly, Im trying to convert integer 10 to a string.But the output shows 10 only.Secondly, Im tryong to cnvert "Hello" to integer for which Im getting an error that says as below:
Wrappers.java:12: incompatible types
found : java.lang.String
required: java.lang.Integer
Integer x=String.valueOf(st);
^

Im totally lost Could anyone explain how these functions work?
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
08-Jan-2007, 02:33 AM #2
How to convert a string into an integer in Java

Here's the formula :

Code:

String test = "12345";
int value = Integer.parseInt(test);
In your example, you try to convert a string that contains no numeric data ("Hello").
If you apply my above formula, you'll get a NumberFormatException.
When you're not sure of a string content to convert into an integer, it is advised to apply the following code :

Code:

String test = "Hello";
int value;
try {
    value = Integer.parseInt(test);
    System.out.println("The value returned is " + value); 
} catch(NumberFormatException nfe) {
    System.out.println("<" + test + "> is not numeric");
}
Caliraj's Avatar
Senior Member with 145 posts.
 
Join Date: Mar 2006
Experience: Intermediate
08-Jan-2007, 04:35 AM #3
wrapper class
Hi Chicon,
Thanks for the answers...could you suggest a website that explains everything clearly regarding wrapper classes & the utility methods?
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
08-Jan-2007, 05:07 AM #4
Here is a good explanation.

To have a more complete description of the attributes and methods of classes, it is better to browse the site of the Java creators : example, here is the description of the wrapper class Integer.

Last edited by Chicon : 08-Jan-2007 05:13 AM.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:08 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.