• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Numeric Value of a String

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to get the int value for string "C";
Gunjan
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know oyu can get the numeric value of a character (ex: 'c') by casting it to an int, I don't know how you get it for a String "C".

Bosun
 
GK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the value for 'C' and 'c' the same?

Gunjan
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gunjan kuwadia:
is the value for 'C' and 'c' the same?


NO!!
C => 67
c => 99
Try this code inside main method, it should help you.
<pre>
System.out.println((int)("C".charAt(0)));
</pre>
HTH,
- Manish
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u should use the concept of wrapper classes to solve ur problem.
OPTION 1
Integer intobj1 = Integer.valueof("C");
//will convert the string "C" into Integer class
int i = intobj1.intvalue();
// will give the int value
OPTION 2
int i = Integer.parseInt("C");
// but this can generate NumberFormatException so put it in a try and catch block.
then u can print the value of i
java being a case sensitive language, 'c' cannot be equal to 'C'

shweta agarwal
Sun Certified Programmer for the Java� 2 Platform
 
GK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shweta agarwal - ur solution didnt work cause it gives me number format exception (Option 1)

Gunjan
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gunjan kuwadia:
How to get the int value for string "C";


What int value? There is no "the" value; there are a number of possible conversions; as you don't tell us what you want it for it's impossible to guess which one you need.

  • The String interpreted as a decimal number? <code>Integer.parseInt("C")</code>, but as "C" is not a number this will throw a NumberFormatException.
  • The String interpreted as a hexadecimal number?<code>Integer.parseInt("C", 16)</code> will return 12.
  • The Unicode word value for the first character ('C') in the String? <code>(int)"C".charAt(0)</code> will return 67.

  • - Peter
 
GK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx peter but dont be harsh on wordings please

Gunjan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My sincere apologies if the wording appeared harsh. It was not meant to be.
- Peter
 
reply
    Bookmark Topic Watch Topic
  • New Topic