Regards,
Vishnu
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:I think that Vishnu is getting 48 but wants 0. The solution is easy:
1) subtract '0' Yes that's right, you can use chars in mathematical expressions.
2) even better, use Character.digit(x, 10). This has better error checking; it returns -1 if the read character is not numeric, instead of, well, anything.
Regards,
Vishnu
Sridhar Santhanakrishnan wrote:In addition to Rob's ideas, Change your loop to check that k is between 48 and 57.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:
You mean between '0' and '9', don't you? Yes they are the same values, but using chars makes it easier to read. For me, 48 doesn't mean a thing, whereas '0' does.
Campbell Ritchie wrote:Why are you using System.in.read() in the first place?
Regards,
Vishnu
Sridhar Santhanakrishnan wrote:
Rob Prime wrote:
You mean between '0' and '9', don't you? Yes they are the same values, but using chars makes it easier to read. For me, 48 doesn't mean a thing, whereas '0' does.
Yes, I was. But I thought the OP was not clear with using chars. He asked for the "solution" again.
Regards,
Vishnu
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:You apparently choose to ignore my posts, since I have given you an answer already in the second reply to this thread. But if you don't want to read it, go ahead. It's your problem, not mine.
Regards,
Vishnu
Vishnu Sharma wrote:
Sridhar Santhanakrishnan wrote:
Rob Prime wrote:
You mean between '0' and '9', don't you? Yes they are the same values, but using chars makes it easier to read. For me, 48 doesn't mean a thing, whereas '0' does.
Yes, I was. But I thought the OP was not clear with using chars. He asked for the "solution" again.
Thats fine. But it is an alternate only, that use ASCII values in condition itself. So, there is no method exists by which we can convert output of System.in.read() method to integer from ASCII.
Campbell Ritchie wrote:Rather than System.in.read() try the readLine() methods of BufferedReaders or use a Scanner, which you can read about in the Java™ tutorials.
Regards,
Vishnu