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

[6721 byte] By [ROuNINa] at [2007-10-3 2:43:32]
# 1

Ken_S probably isn't looking right now--he hasn't posted since June. Hard to tell exactly what he was complaining about.

For one thing, you could replace this:

Character c = new Character('#');

if(f== (int) c.charValue())

with:

final int POUND_SIGN = (int)'#';

if (f==POUND_SIGN)

And then you later use 32 and 35 explicitly for space and pound sign, so you could have just used 35 above (or define POUND_SIGN as 35):

if (f==35)

You could use regular expressions and String.replace to remove spaces from a String. Then you could use a BufferedReader and readLine to read a line at a time from System.in, get each line as a String, call 'replace', and then write it to the output file.

MLRona at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 2
> with:> final int POUND_SIGN = (int)'#';> > if (f==POUND_SIGN)Only in America? We call it a HASH_SIGN.:-)
TimRyanNZa at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 3
> > with:> > final int POUND_SIGN = (int)'#';> > > > if (f==POUND_SIGN)> > Only in America? We call it a HASH_SIGN.:-)We call it pound sign or number sign in America.
MLRona at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 4
Why not call it Vertical_bar_Vertical_bar_Horizontal_bar_Horizontal_bar?Nice and clearn and it's i18n'ed ? Just kidding - If I ever post code with a variable like that shoot me.
Aknibbsa at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 5

> We call it pound sign or number sign in America.

Not all of us. For me, what it's called is dependent on context, and I've used the term "hash sign" for that symbol on more than one occasion. :o)

But then, I'm a loner, Dottie. A rebel. There are things about me you wouldn't understand.

~

yawmarka at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 6

> > We call it pound sign or number sign in America.

>

> Not all of us. For me, what it's called is dependent on context, and I've

> used the term "hash sign" for that symbol on more than one occasion. :o)

I prefer to call it TicTacToeMyTurnNow but just for communication

reasons I call it 'hash sign' too ;-)

kind regards,

Jos

JosAHa at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 7

> Ken_S probably isn't looking right now--he hasn't

> posted since June. Hard to tell exactly what he was

> complaining about.

My guess is that he was complaining about the confusing usage of numbers in place of chars. If you want to compare something to a number sign (the character #, or the pound sign as the Americans call it) then compare it to '#' and not to some number where you have to get out a table to find out what it means.

DrClapa at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 8

> > We call it pound sign or number sign in

> America.

>

> Not all of us. For me, what it's called is dependent

> on context, and I've used the term "hash sign" for

> that symbol on more than one occasion. :o)

The people at AT&T tried to get people to call the symbol "octothorp", but that never stuck. The people at Unicode call it NUMBER SIGN, but that doesn't work well with telephones ("Press Number 690" doesn't make people press the keys #, 6, 9, 0). Personally I think of it as the sharp symbol.

DrClapa at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...
# 9

The only thing that looks strange here is the usage of ints for the characters and the variable c.

The variable c is confusing because your creating a object of type Character from a char to get the char back.

It's the same as

int one = new Integer(1).intValue();

int two = 2;

The assignment of two is easier to understand.

Your if statements would have be easier to read if you had just used the character constants.

char f = (char)System.in.read();

// Use '#' to indicate end of input

if (f == '#')

{

eof = true;

}

else if (f != ' ')

{

file2.write(f);

}

gte42ea at 2007-7-14 20:31:53 > top of Java-index,Java Essentials,New To Java...