PDA

Click to See Complete Forum and Search --> : ASCII Table


NOTSOSURE
Dec 7th, 2002, 03:54 PM
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.



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

Phenix
Dec 7th, 2002, 08:57 PM
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:

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

NOTSOSURE
Dec 8th, 2002, 07:18 AM
Thanks for the help

I modified your code and it works thanks mate much appreciated

this is the code i used



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

Phenix
Dec 8th, 2002, 07:57 PM
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.

NOTSOSURE
Dec 10th, 2002, 11:36 AM
LOL i didnt even notice that hahaha thnx for pointing it out


ohh yeah thanx again