0

How do you "delete" a character from a file. Also, how do you print the stuff in the file out?

Write a program that reads in a file of text, perhaps the text of a novel. The program copies the same text to an output file, except that all the useless words such as "the", "a", and "an" are removed. (Decide on what other words you with to remove. The list of words removed is called a stop list.) Do this by reading the text file token by token using hasNext() and next(), but only writing out tokens not on the stop list.

Prompt the user for the names of the input and output files. Preserve the line structure of the input file. Do this by reading each line using nextLine() and then creating a new Scanner for that line. (Look at the on-line documentation for Scanner.) With each line's Scanner, use hasNext() and next() to scan through its tokens.

public static void main(String[] args) throws IOException {

    String fileName;
    Scanner user = new Scanner(System.in);
    System.out.print("File name: ");
    fileName = user.nextLine().trim();

    File file = new File(fileName);



    PrintStream printfile = new PrintStream(file);
    System.out.println("Input data into file: ");
    String datainfile = user.nextLine();
    Scanner scan = new Scanner(file);
    printfile.println(datainfile);

    while (scan.hasNextLine()) {
        while (scan.hasNext()) {
            String character = scan.next();
            if (character.equals("a")) {

            }
        }
    }
}

EDIT thanks to peeskillet I tried attempting again. However, there seems to be an error somewhere in my program and I get:

  AAApotatopotatopotatojava.util.Scanner[delimiters=\p{javaWhitespace}+][position=0]                            [match valid=false][need input=false][source closed=false][skipped=false][group  separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

Can you inspect my program?

public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Input File name: ");
    String filename1 = keyboard.nextLine().trim();
    System.out.print("Output File name: ");
    String filename2 = keyboard.nextLine().trim();

    File inputFile = new File(filename1);
    File outputFile = new File(filename2);

    PrintStream printfile = new PrintStream(inputFile);
    System.out.println("Input data into file: ");
    String datainfile = keyboard.nextLine();

    printfile.println(datainfile);

    Scanner inFile = new Scanner(inputFile);
    PrintWriter writeFile = new PrintWriter(outputFile);

    Scanner lineScanner;

    while (inFile.hasNextLine()) {

        String line = inFile.nextLine();

        lineScanner = new Scanner(line);

        while (lineScanner.hasNext()) {
            String word = lineScanner.next();
            if(!(word.equals("a"))) {
                writeFile.print(word + " ");
                System.out.print(word);

            }
            if(!(word.equals("an"))) {
                writeFile.print(word + " ");
                System.out.print(word);
            }
            if(!(word.equals("the"))) {
                writeFile.print(word + " ");
                System.out.print(word);
            }
            else {
                writeFile.print(" ");
            }

        }

        writeFile.println();

    }
    writeFile.close();

    Scanner readOutput = new Scanner(outputFile);

    System.out.println(readOutput);

}
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Freedom
  • 347
  • 2
  • 7
  • 24

1 Answers1

1

First of all you need two File objects, one for input and one for output. You only have one.

You want to do something like this

Scanner keyboard = new Scanner(System.in);
System.out.print("Input File name: ");
String filename1 = keyboard.nextLine();
System.out.print("Output File name: ");
String filename2 = keyboard.nextLine();

File inputFile = new File(filename1);
File outputFile = new File(filename2);

Scanner infile = new Scanner(inputFile);
PrintWriter outputFile = new PrintWriter(outputFile);

Scanner lineScanner;

while(infile.hasNextLine()){             // here you read each line of a file
    String line = inFile.nextLine();     // here is a line

    lineScanner = new Scanner(line);     // for the above line, create a scanner 
                                         // just to scan that line
    while(lineScanner.hasNext()){        // loop through that line
        // do something
    }
}
outputFile.close();

Edit: I would just put all the conditions into one statement

    while (lineScanner.hasNext()) {
        String word = lineScanner.next();
        if(!(word.equals("a")) && !(word.equals("an")) && !(word.equals("the"))) {
            writeFile.print(word + " ");
            System.out.print(word);
        }

    }
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks, but I was wondering what I should put in the while(lineScannner.hasNext()) { } Should I do lineScanner.replace("a", " ") etc? Also, how do you print out the words in a file? Thanks. – Freedom Dec 03 '13 at 18:42
  • 1
    Since you're scanning word by word, you don't want to use replace. Instead use an if statement. `If word does not equal "a" or if word does not eqaul "the", ouputFile.print(word + " ");`. After each line print to next line `outputFile.println()` – Paul Samsotha Dec 04 '13 at 00:46
  • That helped a lot peeskillet, but could you check my improved program to see what I did wrong? Thanks! – Freedom Dec 04 '13 at 03:23
  • I would just put all the ifs into one line. See my edit. What you were doing is checking the word for each if, so the program would print three times if the word is not met. – Paul Samsotha Dec 04 '13 at 03:32
  • Thanks! Sorry for bothering you again, but what does the java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E] mean? – Freedom Dec 04 '13 at 16:42
  • When I run the program after i input the data it shows me that – Freedom Dec 04 '13 at 19:18