permaculture playing cards*
The moose likes Beginning Java and the fly likes how to change a char to an int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to change a char to an int" Watch "how to change a char to an int" New topic
Author

how to change a char to an int

deb uebersetzig
Greenhorn

Joined: Sep 29, 2000
Posts: 6
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

Joined: Sep 12, 2000
Posts: 202
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

Joined: Sep 29, 2000
Posts: 6
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

Joined: Oct 24, 2000
Posts: 22
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to change a char to an int