Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Variable convertion

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i was wondering if it is possilbe to convert an Integer to a charachter?
ie - so '1' = 'a'
and '2' = 'b' and so on
i know this much : number=integer.parseInt(number); < this , i think,
converts a srting to an int, i think its something like this
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marky
Because an int to a char is considered a narrowing conversion you need to cast the int to a char.
int i = 65;
char itoc = (char)i;
System.out.println(itoc);
// prints A
Going the other way is easier because you dont need to cast an a char to an int, as it is conseidered a widening conversion.
char a = 'A';
int atoi = a;
System.out.println(atoi);
// prints 65
hope that helps
 
I child proofed my house but they still get in. Distract them with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic