|
-
Dec 7th, 2002, 04:54 PM
#1
Thread Starter
Addicted Member
ASCII Table
i am experamenting with JAVA.
I wanna write a program which will print an ASCII values table using a for loop.
So if i type in a Start Value of "A" and a Last Value of "}" the output should be
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
Error conditions
================
If the last character is before the start character print an error message saying:
"Error - last is before start"
Here is the code i have writen so far.. but i cant get the output to show can any one tell me where i am going wrong.
Code:
public class Ascii {
public static void main(String[] argv) {
char startChar, lastChar;
String ascii = "";
System.out.println ("Please type the start character");
startChar = UserInput.readChar();
System.out.println ("Please type the last character");
lastChar = UserInput.readChar();
if (lastChar < startChar){
System.out.println ("Error, last character is before start character");
}
for (startChar = startChar; startChar == lastChar; ++startChar){
}
ascii = ascii + startChar;
System.out.println (ascii);
} // end of main
} // end class
thanx ppl
Last edited by NOTSOSURE; Dec 7th, 2002 at 05:00 PM.
-
Dec 7th, 2002, 09:57 PM
#2
Addicted Member
1)Let ascii also be datatype char instead of datatype String.
2)Make sure your UserInput.readChar(); really returns a char datatype. I used argv[0].charAt(0) and argv[1].charAt(0) for testing.
3)Include an exit path when you want to exit in your test condition for lastChar < startChar
System.out.println ("Error, last character is before start character");
System.exit(0);
Technically, you might want to use System.err.println..., but it may not be a big deal for your project.
4a)Think about what you are doing in your for loop.
Now consider what my code means:
for (ascii = startChar; ascii <= lastChar; ++ascii){
System.out.print (ascii);
}
4b)Make sure you have something in the body of your for loop. Unless you are just running a delay, you should have something in the body.
4c)Even if you had this in your for loop, you would not be incrementing properly
ascii = ascii + startChar;
4d)Notice the System.out.print (ascii);
instead of println for the output you requested in the top of your post. println would print one column instead of one row of output.
Now you can clean up my code to use your UserInput class:
Code:
public class Ascii {
public static void main(String[] argv) {
char startChar, lastChar, ascii;
System.out.println ("Please type the start character");
startChar = argv[0].charAt(0);//UserInput.readChar();
System.out.println (startChar);
System.out.println ("Please type the last character");
lastChar = argv[1].charAt(0);//UserInput.readChar();
System.out.println (lastChar);
if (lastChar < startChar){
System.out.println ("Error, last character is before start character");
System.exit(0);
}
for (ascii = startChar; ascii <= lastChar; ++ascii){
System.out.print (ascii);
}
} // end of main
} // end class
You might want to see how I ran the code. So I edited to add this after I posted.
I ran the code using these two test cases:
java Ascii A }
and
java Ascii } A
Last edited by Phenix; Dec 7th, 2002 at 10:04 PM.
-
Dec 8th, 2002, 08:18 AM
#3
Thread Starter
Addicted Member
Thanks for the help
I modified your code and it works thanks mate much appreciated
this is the code i used
Code:
public class Ascii {
public static void main(String[] argv) {
char startChar, lastChar, ascii;
System.out.println ("Please type the start character");
startChar = UserInput.readChar();
System.out.println ("Please type the last character");
lastChar = UserInput.readChar();
if (lastChar < startChar){
System.out.println ("Error, last character is before start character");
}
for (ascii = startChar; ascii <= lastChar; ++ascii){
System.out.print (ascii);
}
} // end of main
} // end class
Last edited by NOTSOSURE; Dec 8th, 2002 at 08:26 AM.
-
Dec 8th, 2002, 08:57 PM
#4
Addicted Member
You are welcome.
Pretty smooth. You don't even need the exit statement for the error condition because the for loop isn't executed under the error condition.
-
Dec 10th, 2002, 12:36 PM
#5
Thread Starter
Addicted Member
LOL i didnt even notice that hahaha thnx for pointing it out
ohh yeah thanx again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|