Please explain Ken_S
Hello All,
This is a continuation from my post back in January 2006:
http://forum.java.sun.com/thread.jspa?threadID=697219&messageID=4049116#4049116
Hi clubc21,
Maybe you can try to understand how this program works:
//Program is saved in a file called RemoveSpacesFromText.java
import java.io.*;
import java.util.*;
publicclass RemoveSpacesFromText
{
publicstaticvoid main(String ags[])throws Exception
{
//create the file RemoveSpacesFile.txt
FileWriter filew =new FileWriter("C:\\RemoveSpacesFile.txt");
int f;//this is what stores what the user types
boolean eof =false;//eof - end of file
//# will exit the program from the while loop
Character c =new Character('#');
while(!eof)
{
//read input from the console input and store it in f
f = System.in.read();
//test if f is the integer value of 35 [# in ascii is 35]
//if 35 then set value of eof to true
if(f== (int) c.charValue())
{
eof =true;
}
//if f is not equal to (32 and 35) then write input to file
//filew [32 is the ascii value for [space]
if((f!=32) && (f!=35))
{
filew.write(f);
}
}
//flush the file filew
filew.flush();
}
}
What it does is that it removes spaces from anything you type in the console window to a file.
When you type # it will stop the program and will have created a file called 揜emoveSpacesFile.txt?br>in your root C:\ directory.
35 is the ascii code for [#]
32 is the ascii code for [space]
If you were to type the following in the console window :
This will remove spaces from anything I write here.
The program writes the following to the 揜emoveSpacesFile.txt?
ThiswillremovespacesfromanythingIwritehere.
Can you make the program do something else?
Hope this is helpful to you.
Thanks,
ROuNIN
Then Ken_S wrote back with this reply:
Ken_S
Posts:309
Registered: 26/09/05
Re: Looking for a KIND person
10-Jan-2006 10:04 (reply 35 of 43)
"I will give you one tip: don't ever write anything that looks even remotely like this:"
>
>//Program is saved in a file called
> RemoveSpacesFromText.java
>import java.io.*;
>import java.util.*;
>
>publicclass RemoveSpacesFromText
>{
>publicstaticvoid main(String ags[])throws Exception
>{
>//create the file RemoveSpacesFile.txt
> FileWriter filew =new FileWriter("C:\\RemoveSpacesFile.txt");
>
>int f;//this is what stores what the user types
>boolean eof =false;//eof - end of file
>//# will exit the program from the while loop
>Character c =new Character('#');
>
>while(!eof)
>{
>//read input from the console input and store in f
>f = System.in.read();
>
>//test if f is the integer value of 35 [# in ascii is 35]
>//if 35 then set value of eof to true
>if(f== (int) c.charValue())
>{
>eof =true;
>}
>
>//if f is not equal to (32 and 35) then write input to file
>//filew [32 is the ascii value for [space]
>if((f!=32) && (f!=35))
>{
>filew.write(f);
>}
>}
>//flush the file filew
>filew.flush();
>}
>}
>
>
"If I was dead i would turn in my grave because of this code, but I'm not, so I'll just complain on this forum."
So, my question to Ken_S how would you write a program that does the same?
I would like you to explain in further detail why someone should not write code that looks like that?
Please note, I am asking for an exact example that I can copy and paste and then study the correct code that Ken_S says should be written. Hopefully, improving my Java programming skills.
Thanks,
ROuNIN

