Ok im writing a program in java thats suppose to Use a while loop, to print the lower case alphabeth and its corresponding ascii values
of the upper case alphabeth from Z to A
Secondly, I'm not getting what exactly you are asking. Did you run the program? What was the output? If it's not what you were expecting, what were the differences (between expected output and actual output)?
This is probably not printing what you've expected, and that's because of this line:
What happens here is that you convert counter to a char and then you add counter2, which is an int, to it. The result will be an int. (Note that this is NOT string concatenation - you are adding numbers!).
For example, if counter is 97, then counter2 will be 97 - 32 = 65. Then you add them: 97 + 65 = 162. That's the number that's printed. In the next iteration it prints 164, then 166, etc. up to 212.
If you want to have the character and then the number printed, you'll have to fix that line of code.
make my brain hurt. Why are you using 123? This is what's called a "magic number". It appears out of nowhere, has no inherent meaning, and in six weeks when you come back to this code, you'll be asking yourself "Why the heck did I use 123 here? Why not 124? Why not 87? Why not 1,237,310?"
Magic numbers are a BAD practice. At the VERY least, you should declare a variable with a meaningful name, and use it instead. Something like
Never ascribe to malice that which can be adequately explained by stupidity.
Jim Pouwels
Ranch Hand
Joined: Feb 22, 2012
Posts: 61
posted
0
fred rosenberger wrote:the lines
while ( counter < 123)
and
if (counter > 123)
make my brain hurt. Why are you using 123? This is what's called a "magic number". It appears out of nowhere, has no inherent meaning, and in six weeks when you come back to this code, you'll be asking yourself "Why the heck did I use 123 here? Why not 124? Why not 87? Why not 1,237,310?"
Magic numbers are a BAD practice. At the VERY least, you should declare a variable with a meaningful name, and use it instead. Something like
Well, if I'm looking at his code, coding conventions isn't the first thing he should be worried about ;)
make my brain hurt. Why are you using 123? This is what's called a "magic number". It appears out of nowhere, has no inherent meaning, and in six weeks when you come back to this code, you'll be asking yourself "Why the heck did I use 123 here? Why not 124? Why not 87? Why not 1,237,310?"
Magic numbers are a BAD practice. At the VERY least, you should declare a variable with a meaningful name, and use it instead. Something like
Well, if I'm looking at his code, coding conventions isn't the first thing he should be worried about ;)
on my computer courses, where were taught about magic numbers on day 1, you can never start in good habits too soon
Jim Pouwels
Ranch Hand
Joined: Feb 22, 2012
Posts: 61
posted
0
Can't argue with that! Cheers!
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
4
posted
0
Wendy’s right. Another coding convention is indenting; I would have added code tags if the post had been indented, but it’s pointless without indentation.