Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ASCII code & loop

 
Claire Pearce
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have been asked to program a code for some college work using the ascii code as a display on command prompt screen however, my tutor suggested using a 'for' loop?? hmmm puzzled nope glad your not but i can only figure it out as an 'if else' anyone got any suggestions? xx
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I quite understand the requirement, but if you post the code you've written using if/else, I'm sure I (or someone else) will be able to point you in the right direction.
Remember to enclose the code in code tags (see the buttons below the window where you type your reply) as this makes it easier to read.
 
Claire Pearce
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a program which will print an ASCII values table (characters).

Method
A �for� loop should do the trick...

and you must include
Error conditions
If the last character is before the start character print an error message saying:
Error - last is before start
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, requirement is now clear.

Reminder to all readers that we don't just want to post a full solution. That's not the JavaRanch way. And, to be fair, the original poster didn't request a solution - just guidance.

To specify the start and end, you will presumably want your program to accept two command-line arguments. These are the String array that gets passed to main(). You'll need to decide if you want to pass characters (e.g. A and Z) or codes (e.g. 65 and 91). Look at the java.lang.Integer class for how to convert a String to an integer. You may want to cast to char afterwards.

You will want an "if" (more than one, actually), to perform the error check that you are required to do.

You will indeed want a "for" loop, as the tutor hints. You'll need to loop from the first to the last chosen character. Within the loop, you'll want to print the numeric value and the character value of the loop variable.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you don't worry about the command line arguments to start with (unless you have already covered these in your course and understand them). Just try to print out the values 'A' to 'Z'. Give it a go and then post your code if you have any problems.
Once we have that working, it shouldn't be too difficult to work up to the actual requirements.

Here is the part of the Java tutorial that describes the for loop. There are a couple of examples of how to use it.
 
Claire Pearce
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help i finally got my brain in gear and got a complete programme will post it later for your perusal many thanks for your help it was much appreciated guys xxx
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic