• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

how to change a char to an int

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a problem for school that has a string of the top ten reasons for something. Then you prompt the user to enter
either 0 - 9 as their choice of the reasons. I am supposed to convert the number to an integer and then use the integer to print the reason chosen. I am stuck on trying to convert the Char from the prompt to an int....what class should I be using or is there a better way to approach this........
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the part that's messing you up is that when you read user input, it is always read as a String. That String must me converted to an int for you to use in in this particular application.
likely should look like this
myInt = Integer.parseInt(userInputString);

[This message has been edited by Peter Lyons (edited November 03, 2000).]
 
deb uebersetzig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ReasonJava
{
public static void main(String[] args) throws Exception
{
String[] inputNumber = {"It's fun", "It's new", "It's easy",
"It's exciting", "It's different", "It's wild", "It's cool",
"It's gonna help me get a job", "It's something my hubby doesn't know",
"It's out there"};
String inputString = new String();
char inputChoice;
int inputNum = 0;
System.out.println("Enter why you like Java..pick number 0 - 9: ");
inputChoice = (char)System.in.read();
System.in.read();
System.out.println("You have chosen " + inputChoice);

inputNum = Integer.parseInt(inputNumber);
System.out.println("this is inputNums value " + inputNum);
-----this is my code...and the problem is at the Integer.parseInt----it points to the dot between the two words.......Am I doing it wrong???

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi deb uebersetzig!
The solution to ur previous problem is given below.
import java.lang.*;
public class ReasonJava
{
public static void main(String[] args) throws Exception
{
String[].....
System.out.println("Enter why you like Java..pick number 0 - 9: ");
char inputChoice=(char)System.in.read();
int inputNum = 0;
inputNum=Character.getNumericValue(inputChoice);
System.out.println("You have chosen " + inputNum);
System.out.println(inputNumber[inputNum]);
}
}
Try this out.Hope this will satisfy u.
Good Luck
Preethi M
 
grapes are vegan food pellets. Eat this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic