wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Char To Int a->1 b->2 c->3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Char To Int a->1 b->2 c->3" Watch "Char To Int a->1 b->2 c->3" New topic
Author

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

Raj
Greenhorn

Joined: Apr 08, 2007
Posts: 20
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

Joined: Apr 15, 2008
Posts: 131
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


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

If you cast a char to an int you get its ascii value.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

be aware though that 'a' is not the same a 'A', so you may want to normalize your input.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38461
    
  23
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

Joined: Apr 08, 2007
Posts: 20
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

Joined: Oct 27, 2005
Posts: 19674
    
  18

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

Joined: Oct 13, 2005
Posts: 38461
    
  23
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

Joined: Sep 26, 2008
Posts: 1
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 ]

VikasH
Raj
Greenhorn

Joined: Apr 08, 2007
Posts: 20
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

Joined: Oct 13, 2005
Posts: 38461
    
  23
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.
 
 
subject: Char To Int a->1 b->2 c->3