aspose file tools*
The moose likes Beginning Java and the fly likes Need help with program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with program" Watch "Need help with program" New topic
Author

Need help with program

Gregg Smith-Jones
Greenhorn

Joined: Mar 06, 2012
Posts: 1
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

Joined: Dec 08, 2010
Posts: 1507
    
    5

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)?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

Gregg, please KeepItDown, also in subject lines. Also, UseAMeaningfulSubjectLine.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

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

Joined: Oct 02, 2003
Posts: 11314
    
  16

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



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Feb 22, 2012
Posts: 61
Can't argue with that! Cheers!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with program