• 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

How can I do this?

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way where I can transform the char "c" int integer?. I need teh integer value.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
charecters can be implicitly converted to an int.

Example:



Is this what you wanted?

Cheers
Vijay
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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').
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic