Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Char To Int a->1 b->2 c->3

 
Raj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

i hav a bit of a problem well i am taking rows and colmn of a matrix as input in which rows are char and coloms are int.
Display shows this
1 2 3 4 5 6 7 8 910
a . . . . . . . . .
b . . . . . . . . .
c . . . . . . . . .
d . . . . . . . . .
e . . . . . . . . .
f . . . . . . . . .
g . . . . . . . . .

hence the user needs to input "a 1" then the prog searches for tht position I am using i,j of type int to traverse through the array
hence need to convert the a into 1 b->2 c->3 d->4 e->5;

i am taking the input using Scanner class.



well i am sorry if this Q has allready been answered, i tried finding it in the threads sooo.... i need to submit this by tommorow .
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
get the ascii value of input char. i dont remeber what is ascii value , for eg. a-> 96 then b-> 97 and so on
then get do use this expression

i hope this should solve the problem
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cast a char to an int you get its ascii value.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
be aware though that 'a' is not the same a 'A', so you may want to normalize your input.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50208
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find the values from the Unicode charts and using the % operator with 0x20 can reduce your numbers, so 'A' %0x20 or 'a' %0x20 will both return 1; 'Z' % 0x20 and 'z' % 0x20 both return 26.
 
Raj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
input2= myScanner.next();
temp_R = myScanner.next();
input_C = myScanner.nextInt();
input_R=temp_R.charAt(0);
Go_obj.Check(input2,input_R,input_C);

so is this rite ???
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You can find the values from the Unicode charts and using the % operator with 0x20 can reduce your numbers, so 'A' %0x20 or 'a' %0x20 will both return 1; 'Z' % 0x20 and 'z' % 0x20 both return 26.

I think I'd prefer the following:
 
Campbell Ritchie
Sheriff
Pie
Posts: 50208
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

I think I'd prefer the following:
Both methods would work; I think yours is generally regarded as the usual way to perform that trick, and would probably have a performance advantage because the - operator is faster than the % operator. It might be worthwhile having a check that the char is an ASCII letter; the Russian letters that somebody else had problems with yesterday would give totally misleading results with %0x20.
 
Vikas Hogale
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anchit kalra:
Hi guys,

i hav a bit of a problem well i am taking rows and colmn of a matrix as input in which rows are char and coloms are int.
Display shows this
1 2 3 4 5 6 7 8 910
a . . . . . . . . .
b . . . . . . . . .
c . . . . . . . . .
d . . . . . . . . .
e . . . . . . . . .
f . . . . . . . . .
g . . . . . . . . .

hence the user needs to input "a 1" then the prog searches for tht position I am using i,j of type int to traverse through the array
hence need to convert the a into 1 b->2 c->3 d->4 e->5;

i am taking the input using Scanner class.



well i am sorry if this Q has allready been answered, i tried finding it in the threads sooo.... i need to submit this by tommorow .



First need to check the case(Upper/Lower) of entered charater then ....
(ascii(input_char) - ascii(a))+1

Hope so it will work out!
[ September 26, 2008: Message edited by: Vikas Hogale ]
 
Raj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okey i got wht i needed but there is a small catch.
the input needs to be only

clear <row> <col>

but as i am using Scanner it accepts

clear
<row>
<col>

also which is not required

code:

there are only 3 inputs whish are taken

clear <row> <col>
or
black <row> <col>
or
white <row> <col>
or
quit

should i use tokenizer please advise......
 
Campbell Ritchie
Sheriff
Pie
Posts: 50208
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New problem. You need a lot of practice with Scanner; it divides the input into tokens, using a regular expression as the "delimiter;" if you don't specify a delimiter it defaults to whitespace. So calling next() gets a token; in the example you show you get three tokens per line so it takes three calls to next() to read the whole line.
On the other hand you can call nextInt() three times and give it 1 2 3 as the input and it will read 1 2 3 whether or not you use the enter key in between.
You may need the nextLine() method to clear the remainder of the current line.

Go through the API for Scanner and you find all sorts of nextXXX() methods, which allow you to read the next token as an int, or even take the whole line. You will probably find some combination of those will suit your requirements.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic