• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with program

 
Gregg Smith-Jones
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

So far i came up with

int counter = 97;
int counter2;

while ( counter < 123)
{
counter2 = counter - 32;
System.out.print((char) counter + counter2);
counter++;

if (counter > 123)
break;

}


but im at a stand still dont know what to do next any suggestions???
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg Smith-Jones,

Welcome to CodeRanch!

First things first : Please UseCodeTags.

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)?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

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.
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the previous reply, this line of code:



is completely useless, since you'r while-loop will always stop when counter is not smaller than 123. So this looks somewhat redundant to me.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg, please KeepItDown, also in subject lines. Also, UseAMeaningfulSubjectLine.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have forgotten is that a char is not a letter or similar; it is a number. You can do arithmetic using + - < > ++ etc directly on the char/.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;)
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Pouwels wrote:
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 ;)


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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't argue with that! Cheers!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

OP: Please always indent your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic