im trying to make a program in java (NOT java script) that takes a text file, takes out any "bad chars" (!@#$%^&*,;:'" etc) and makes it so it only has X amound of chars a line, and line breaks after each
.
but for some reason it doesnt work:
infile:
Hello, my name is joel, how are you today, this is the coolest !@#$ thing
in the world. i think. you think. we all think. eddie is the master and he is cool, and he does really good crap and such
outfile:
Hellomy name is joelhow are you
todaythis is the coolest thing in
the world.
i think.
you think.
we
all think.
eddie is the master and
he is cooland he does really good
crap and such
notice it doesnt add spaces in the right place, anyone have an idea why?
source below
import chn.util.*;
import apcslib.*;
import java.io.*;
import java.lang.*;
public class main
{
public static void main(String[] args)
{
start exe = new start();
exe.Start();
}
}
class start
{
check Check = new check();
FileInput infile = new FileInput("C:\\Documents and Settings\\Joel\\My Documents\\parse\\file.txt");
FileOutput outfile = new FileOutput("C:\\Documents and Settings\\Joel\\My Documents\\parse\\temp.txt");
ConsoleIO console = new ConsoleIO();
String line;
int max;
int linelength;
char letter;
int time;
int space;
int addspace = 0;
public void Start()
{
System.out.println("How many characters do you want a line?");
max = console.readInt();
removeBad();
}
public void removeBad()
{
time = 0; //how many times it's gone thru the loop
space = max; //i can only have a certain amount of chars a line,
//so this makes sure i dont put a word in that doesnt fit
while(infile.hasMoreTokens())
{
line = infile.readToken(); //takes the first WORD from the infile
linelength = line.length(); //checks the length of the word and stores it as a int
time = 0; //resets the varable time
//if there's enough room to add the word, it will, otherwise it will go to the else
if(linelength < space)
printf(); //takes me to the method (or function) printf()
else
{
enter(); //if there isnt enough room, make it go to the next line
space = max; //reset the space varable
printf(); //prints the word to the NEW line
}
}
System.out.println("DONE"); //when it's finished, it will print "DONE" to the screen
outfile.close(); //closes writing to the textfile
}
public void printf()
{
for(int x = 0; x < linelength; x++)
{
letter = line.charAt(x); //this is the 'x' character from the current word
if(Check.isChar(letter) == true) //isChar() is just a small method to make sure a char is a unwanted char (ex: !@#$%^)
{
time = time + 1; //increments time
space = space - 1; //decrements space
addspace = addspace + 1; //heh, i dont even know why i have this :P ignore it for now
outfile.print(letter); //this prints the char to the outfile (in this case, "temp.txt"
System.out.print(letter); //This prints the same thing, but to the console,
//so the user can see what's going on
if(time == linelength) //if the word is all finished printing to the file
{
System.out.print(" "); //add a space to the screen
outfile.print(" "); //add a space to the file
addspace = 0; //resets the unused varable, lol, still cant remember why i have that
}
if(letter == '.') //if the character is a '.' then go to the method enter()
enter();
}
}
}
//this is a small method i made, all it does is just returns in both the file and on the screen
public void enter()
{
System.out.print("\n");
outfile.print("\n");
}
}
//just a simple char check i made
class check
{
public boolean isChar(char x)
{
boolean ischar = false;
if((x >= 65 && x <= 122) || x == 46 || Character.isDigit(x) == true)
ischar = true;
else
ischar = false;
return ischar;
}
}