-1

I am trying to code a program that:

  • Scan a file through a file path given by the user
  • counts its words
  • print its content to the terminal
  • Add to it, in a new file, through another file path given by user

And it can also count the words of that new file, and print its content to the terminal.

but I have two problems with that new file: 1- it only saves my adittion without the old file's content (even though I used .concat() method) 2- it cannot count the words of that new file.

other than that it works, I tried alot to solve those problems, but I couldn't...

package com;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;

public class FileReader {
  public static void main(String[] args) throws IOException {
    Scanner scanTwo = new Scanner(System.in);
    System.out.println("Please Enter Your File Path");
    String filePath = scanTwo.nextLine();  
    File fileInput = new File(filePath);
    Scanner fileScanner = new Scanner(fileInput);
    System.out.println(fileScanner.nextLine());
    System.out.println("Commands: PRINT.FILE --> Prints all file    COUNT.WORDS --> Counts all words   ADD.TO.FILE --> add to selected file");
    System.out.println("Type Command:");
    
    Scanner scan = new Scanner(System.in);
    String printFileCommand = scan.nextLine();
     
     
     if (printFileCommand.contains("PRINT.FILE")) {
       while (fileScanner.hasNextLine()) {
         System.out.println(fileScanner.nextLine());
        }
      } else if (printFileCommand.contains("COUNT.WORDS")) {
        int wordCount = 0;
        while (fileScanner.hasNext()) {
          String fileWords = fileScanner.next();
          wordCount++;
        }
        System.out.println(wordCount);
      } else if (printFileCommand.contains("ADD.TO.FILE")) {
        System.out.println("Please Enter Your new file path");
        String newFilePath = scan.nextLine();
        FileWriter addToFile = new FileWriter(newFilePath);
        System.out.println("Please Enter Your Additions: ");
        String newFileContent = scan.nextLine();
        File newFileInput = new File(newFilePath);
        Scanner newFileScanner = new Scanner(newFileInput); 
        while (newFileScanner.hasNextLine()) {
          newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
        }
        addToFile.write(newFileContent);
        addToFile.close();
        System.out.println("Commands: PRINT.FILE --> Prints all file    COUNT.WORDS --> Counts all words");
        System.out.println("Please Enter Your Command:");
        String newCommand = scan.nextLine();
        if (newCommand.contains("PRINT.FILE")) {
          while (newFileScanner.hasNextLine()) {
            System.out.println(newFileScanner.nextLine());
           }
        } else if (newCommand.contains("COUNT.WORD")) {
          int newWordCount = 0;
           while (newFileScanner.hasNext()) {
           String newFileWords = newFileScanner.next();
           newWordCount++;
         }
         System.out.println(newWordCount);
        } else {
          System.out.println("COMMAND INVALID!");
        }
        addToFile.close();
        // newFileScanner.close();
      }
      else {
        System.out.println("COMMAND INVALID!");
      }
      scanTwo.close();
      fileScanner.close();
      scan.close();
    } 
  }
Omar Faris
  • 11
  • 2

1 Answers1

0

Are you sure you open old file for read and concat? I see you open new file by newFilePath path and this file is empty, this is your code:

File newFileInput = new File(newFilePath);
Scanner newFileScanner = new Scanner(newFileInput); 
while (newFileScanner.hasNextLine()) {
    newFileContent = newFileContent.concat(newFileScanner.nextLine() + "\n");
}

Also if you already read file with Scanner you need create new Scanner instance (Resetting a .nextLine() Scanner)