This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to change a char to an int

 
deb uebersetzig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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........
 
Peter Lyons
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • 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
  • 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???

 
Preethi M
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic