FIle I/O

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 5159 Views - Last Post: 03 March 2012 - 01:36 PM Rate Topic: -----

#1 jan07  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 22-January 12

FIle I/O

Posted 01 March 2012 - 12:41 PM

Hello,
I have a few questions about my current program and i will attach the word for word assignment (so there's no confusion) and my progress.

Assignment:
Write a program that asks the user to enter the name of a text file and then displays the line count, word(token) count and the character(non-delimeter) count in the file in a nice format. Use the StringTokenizer class in the java.util pacjkage to parse the tokens from the input lines. In addition to the whitespace characters, question mark(?), double quotes("), commas(,) and periods(.) should also be treated as delimeters and thus not to be counted as tokens or parts of tokens. Use the sample text file input5.txt as an input to your program.


input5.txt
Little miss Muffet,
sat on a tuffet,
eating her candy away.
Along came a spider,
who sat down beside her,
and said "Will you marry me?"

My code thus far:
import java.io.*;
import java.util.*;

public class Project5
{
    public static void main(String[] args) throws IOException
    {
        Scanner inputStream = null;
        PrintWriter outputStream = null;
        System.out.println("Please enter a file name");
        Scanner keyboard = new Scanner(System.in);
        String fileName = keyboard.nextLine();
        try
        {
            
            inputStream = new Scanner(new FileInputStream(fileName));
            outputStream = new PrintWriter(new FileOutputStream("newinput5.txt"));
            
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error");
            System.exit(0);
        }
        
        
        String wordCount = null;
        int count = 0;
        String lineCount = null;
        int count1 = 0;
       
        while(inputStream.hasNext())
        {
           wordCount = inputStream.next();
           count ++;
        }
           while(inputStream.hasNext())
         {	
           lineCount = inputStream.nextLine();
           count1 ++;
         }
        
        
        outputStream.println("Word count:" +count);
        outputStream.println("Line count:"+count1);
        //outputStream.println("Character count:"+charCount)
        System.out.println("writing to file");
        inputStream.close();
        outputStream.close();
    }
}
  



my ouput (with my keyboard input):

Please enter a file name
input5.txt
writing to file

my file ouput after the file is written:

Word count:26
Line count:0

my questions and comments:
I am a little confused about writing the input and output. Should i reenter the input5.txt (the 6 line poem) into the code as ouputSream.println() statements, or do i just pre-write it like i have so that i may access it in this program?
I believe i only need to use the StringTokenizer class on char because it seems as if the scanner class methods are suitable for the lines and words, is this correct? How would i go about starting the char tokenization? My line count is wrong and i tried a nested while loop with no avail. Would StringTokenizer come in handy for char and line or just char? Thank you very much.

Is This A Good Question/Topic? 0
  • +

Replies To: FIle I/O

#2 btanne6  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 46
  • Joined: 24-November 11

Re: FIle I/O

Posted 01 March 2012 - 01:29 PM

I'm not sure of the best approach here but you may could try this

String line;
int lineCount = 0;

BufferedReader br = new BufferedReader(new FileReader(“Input5.txt"));

while ((line = br.readLine()) != null) {
	lineCount++;
}



that should count the lines. When you separate each line then tokenize that line for individual words, and for each word call the String's length() method to get the length of the String, or characters.
while(line.hasMoreTokens())
    {	
      String temp = line.nextToken();
      count += temp.length();
    }


Hopefully this will at least get you moving in the right direction
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,110
  • Joined: 27-December 08

Re: FIle I/O

Posted 01 March 2012 - 01:30 PM

The StringTokenizer class is legacy code, and should be avoided as per the documentation. Use the String split() method instead. Read in a line, then split() it. The length of the array returned from split() is the number of words for that line.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2814
  • View blog
  • Posts: 11,913
  • Joined: 20-September 08

Re: FIle I/O

Posted 01 March 2012 - 01:35 PM

Quote

I believe i only need to use the StringTokenizer class on char because it seems as if the scanner class methods are suitable for the lines and words, is this correct?


Well you were asked to use the StringTokenizer class (which is all but deprecated) so i should do that for the sake of your assignment. I assume the above quotation is verbatim, and that it shouldn't really be StreamTokenizer (which would make it much easier)? What version of java are you using btw?

java -version

This post has been edited by g00se: 01 March 2012 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

#5 jan07  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 22-January 12

Re: FIle I/O

Posted 01 March 2012 - 09:55 PM

Yeah unfortunately i have not been taught buffer reader and yes i have to use StringTokenizer (not streamTokenizer)that is my assignment verbatim. IDK what java version i think 5 or 6 i use eclipse?

This post has been edited by jan07: 01 March 2012 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: FIle I/O

Posted 01 March 2012 - 10:01 PM

        while(inputStream.hasNext())
        {
           wordCount = inputStream.next();
           count ++;
        }

// the following statements are useless
// if you exit the previous while() it is because inputStream.hasNext() was false
// won't be tru if you try it again

           while(inputStream.hasNext())
         {	
           lineCount = inputStream.nextLine();
           count1 ++;
         }


Was This Post Helpful? 0
  • +
  • -

#7 jan07  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 22-January 12

Re: FIle I/O

Posted 01 March 2012 - 10:07 PM

Yes that makes sense. I am trying to implement the while loops consecutively.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: FIle I/O

Posted 01 March 2012 - 10:12 PM

View Postjan07, on 02 March 2012 - 12:07 AM, said:

Yes that makes sense. I am trying to implement the while loops consecutively.

consecutively ??? Based on what ?
Nothing else to read, what do you want to try to read ?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2814
  • View blog
  • Posts: 11,913
  • Joined: 20-September 08

Re: FIle I/O

Posted 02 March 2012 - 02:44 AM

a. Use a Scanner to read the actual file - you'll need to create the StringTokenizer on each s.nextLine
b. There is no IO output - forget that. Just print the results to System.out
Was This Post Helpful? 0
  • +
  • -

#10 jan07  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 22-January 12

Re: FIle I/O

Posted 02 March 2012 - 12:39 PM

Quote

consecutively ??? Based on what ?
Nothing else to read, what do you want to try to read ?


I was acknowledging my mistake not telling you what i was going to do. I am not trying to read i am trying to take the existing file (enterd by the user) and StringTokenize it do you get what i am saying?
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2814
  • View blog
  • Posts: 11,913
  • Joined: 20-September 08

Re: FIle I/O

Posted 02 March 2012 - 01:19 PM

Well - i've told you what you need to do above
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: FIle I/O

Posted 02 March 2012 - 01:20 PM

When you say "StringTokenize it" what do you mean by that? from what I can see from the original post you are just trying to get the word count and the line count. You can do this with 1 loop and 2 scanners.

// Create scanner for the entire file.
Scanner doc = new Scanner(someFileObject);
/* create another scanner for the words but do not initialize it*/
/* Create an int variable for the line count here, set to 0 */
/* Create an int variable for the word count here, set to 0 */
while(doc.hasNextLine())
{
    /* Increase your line count by 1 */
    /* Initialize the scanner for the words with the nextLine of the document */
    /* create a while loop while has next() increase word count */
}



Make sense?
Was This Post Helpful? 0
  • +
  • -

#13 jan07  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 22-January 12

Re: FIle I/O

Posted 02 March 2012 - 01:27 PM

Okay based on the way this assignement is written, do i display the word, line and char count in the file itself, or in the ouput in the program itself? If i am to diplay in the file, wouldn't i need I/O output?

What i mean by StringTokenize is to break this file apart by word, line and characters as three seperate operations. Perhaps parse is better i am just not understanding some of the wording and i seem to be getting nowhere thus far.

This post has been edited by jan07: 02 March 2012 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2814
  • View blog
  • Posts: 11,913
  • Joined: 20-September 08

Re: FIle I/O

Posted 02 March 2012 - 01:34 PM

Quote

or in the ouput in the program itself?


Yes, which is why you don't need IO output

http://docs.oracle.c...gTokenizer.html
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: FIle I/O

Posted 02 March 2012 - 01:35 PM

Ok, so minor change. Create a String temp and a String total.

String total = "";
String temp;
while(in.hasNextLine())
{
    // assign temp to the nextLine
    // append temp to total. ( total += temp ) or the .append() method
    // increase line count
    // initialize the Scanner using temp as a parameter
    // while loop to cycle through the word scanner and increase word count after every next()
}
//Make total = total.replace(<Token from directions> , "");
// call it for each token
// Print oput each statistic. Note - The character count is the length of the String total.



Make Sense?

This post has been edited by Fuzzyness: 02 March 2012 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3