| Author |
How can I do this?
|
Manuel Diaz
Ranch Hand
Joined: Apr 22, 2005
Posts: 79
|
|
I want to input this: m2 I have to variables. One is String "word" and the other one is int "col". Im spliting the input text. I want to assign the second letter to col. Here is my code: The program outputs "50" . How can this happen?. What's wrong in here?. How can "col" be 50 if when I split the text I have this: a 2 IN THIS CASE THE INPUT WAS "a2". [ May 14, 2005: Message edited by: Manuel Diaz ]
|
Note: I love programming.
|
 |
M Beck
Ranch Hand
Joined: Jan 14, 2005
Posts: 323
|
|
i suspect your code is actually correct. what may be confusing you is the fact that "col" is an integer variable instead of a char variable. have you studied the difference, and do you know what it means to assign a char to an int? for a "quick fix", you might get away with just modifying the println statement where "col" is printed out; cast "col" back to a char before printing it, and see what happens.
|
 |
Manuel Diaz
Ranch Hand
Joined: Apr 22, 2005
Posts: 79
|
|
|
Is there any way where I can transform the char "c" int integer?. I need teh integer value.
|
 |
Vijay Jagannathan
Greenhorn
Joined: Feb 17, 2003
Posts: 22
|
|
charecters can be implicitly converted to an int. Example: Is this what you wanted? Cheers Vijay
|
 |
Joel McNary
Bartender
Joined: Aug 20, 2001
Posts: 1815
|
|
Yes, '2' == 50. As has been pointed out, you can say: and i will == 50. However, I suspect that you want '2' to result in 2. There are two ways of doing this. The first is to use the Integer.parseInt() method; but that takes a String. The second way is to say: Now, value will equal 2. However, if you put aChar = '?', value will equal 15. So you probably want to ensure that your char is a digit (aChar >='0' && aChar <='9').
|
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
|
 |
Manuel Diaz
Ranch Hand
Joined: Apr 22, 2005
Posts: 79
|
|
OK, I think I have to be more specific. Here is my code: In this code, Im telling the following, I have two variables "col" and "row". I ask the user to enter sometext of this format (a letterfirst, a number after). Now Im separating the text, Im saying in my code, that the "number" (the second part of the text after splitted) takes the value of that "number". Example: the user inputs: g2 the program outputs: g 2 The program assigns "2" to "col". But col is a char type. When I printed "col" the program outputs "50", it has to be "2" that's the real value. I need the integer value because I want to use it on an array. array[row][col] = something!. OK, now I think I expressed a little more, if anyone can help me out, please!! feel free!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16813
|
|
The program assigns "2" to "col". But col is a char type. When I printed "col" the program outputs "50", it has to be "2" that's the real value. I need the integer value because I want to use it on an array. array[row][col] = something!. OK, now I think I expressed a little more, if anyone can help me out, please!! feel free!
I would suggest re-reading some of the responses, particulary the one by Joel McNary. He did answer your question. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: How can I do this?
|
|
|