GypsyKing
GypsyKing

Reputation: 3

How to read a file and pass the information to separately saved class objects?

I'm setting a java application that has 3 separately saved classes WordApp, WordProcessor, and WordType. WordApp reads the file and is suppose to pass the data to the other classes. I can read the file but I can't the data to pass to the other classes. How to pass the information via a string?

I've searched this and every example I find has all the classes saved to the same file. I'm using textpad and I tried passing the string to the WordProcessor class which has constructs an ArrayList of WordType objects. This method is not working for me.

This is the first part of the WordApp class for reading the file.

import java.io.*;                                                                                                // Import IO package
import java.util.Scanner;                                                                                        // Import Scanner utility
import java.util.ArrayList;                                                                                      // Import ArrayList utility

public class WordApp                                                                                             // Declared WordApp Class
{
    public static void main (String[] args)                                                                      // Main
    {
        // Declared Variables
        String again;
        String initial = "";
        String inputFileName;
        String outputFileName;


        // Declared Objects
        Scanner input = new Scanner(System.in);


        do
        {
            try
            {
                System.out.println("                  Welcome to the Word Processor App!                  ");
                System.out.println("**********************************************************************\n");
                System.out.println("Enter file names with .txt extension.");
                System.out.print("Please Enter File Name to Read: ");
                inputFileName = input.nextLine().trim();
                File mcFile = new File(inputFileName);
                Scanner scan = new Scanner(mcFile);

                System.out.println("Enter file names with .txt extension.");
                System.out.print("Please Enter File Name to Save: ");
                outputFileName = input.nextLine().trim();
                File deFile = new File(outputFileName);
                PrintWriter out = new PrintWriter(deFile);

                System.out.println("Reading file...\n");

                while(scan.hasNext())
                {
                    initial = scan.next();
                }

                scan.close();

                System.out.println("Scanning Paragraph.....\n");
                WordProcessor x = new WordProcessor();
                x.addWord(initial);

I'm trying to pass the words in the file here:

import java.util.ArrayList;
import java.util.Collections;

public class WordProcessor
{
    private ArrayList<WordType> words;
    private int totSentences;
    private int totUnique;

    public WordProcessor()
    {
        words = new ArrayList<WordType>();
        totSentences = 0;
        totUnique = 0;
    }

and here:


public class WordType implements Comparable
{

    // instance data
    private String word;
    private int count;
    private int syllables;

    // Constructors
    public WordType(String newWord)
    {
        word = newWord;
        count = 1;
        syllables = countSyllables();
    }

I'm expecting that that words pass to the other classes but when I save the file it is blank. Sorry if I didn't post the code correctly

Upvotes: 0

Views: 37

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're reading the file and getting the items it holds but discarding all but the last.

while(scan.hasNext()) {
    initial = scan.next();
    // You do **nothing** with the String read in
}

scan.close();

System.out.println("Scanning Paragraph.....\n");
WordProcessor x = new WordProcessor();
x.addWord(initial);  // until here where you add the last one

Instead create the WordProcessor object before the while loop, and add words within the while loop

WordProcessor x = new WordProcessor();

while(scan.hasNext()) {
    initial = scan.next();
    x.addWord(initial);
}
scan.close();

There may be other errors, especially problems in your code not shown (addWord(...) comes to mind), but this error was immediately obvious.

Also what type of looping are you doing and why? You appear to have a do-while loop -- why? Again if this does not solve your problem, then you still will need to create and post a valid mcve so we don't have to guess what the problem truly is. Please understand that I am not asking for all your code but rather for something completely different -- please read the link and understand it fully if your problem has not been solved and you still need help.

Upvotes: 1

Related Questions