The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Help

6 replies on 1 page. Most recent reply: Aug 10, 2005 5:11 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Chris

Posts: 5
Nickname: jarvio678
Registered: Aug, 2005

Java Help Posted: Aug 9, 2005 5:16 AM
Reply to this message Reply
Advertisement
Sorry i don't know if this is the right place to post this, but i'm fairly new to java and was i've been working on some tasks to try and improve my skills. I was wondering if anybody would be able to help me by explaining or showing me how i would go about writing an application to find the number of occurancces of words in a text file and how many lines each word is contained in?

Thanks for any help,

Chris.


Chris

Posts: 5
Nickname: jarvio678
Registered: Aug, 2005

Re: Java Help Posted: Aug 9, 2005 9:49 AM
Reply to this message Reply
would i first write an application that outputs the number of chars, words and lines contained in a text file using the StringTokenizer method, then from there using the String.indexOf method?

I'm not sure exactly how to do that but i'll keep having a look around for some tutorials.

Thanks,

Chris.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Java Help Posted: Aug 9, 2005 10:55 PM
Reply to this message Reply
I would go as follows:

Create a BufferedReader:


//  Google "Java Reading from a File
//  There are various ways to set up the buffer
BufferedReader buf = new BufferedReader_To_Read_From_A_File;
 
//  Declare a HashMap (checkout HashMap in the Java Doc
//  online on the Sun website
HashMap storage = new HashMap();
while(buf.readLine() Still has Lines)
     String str = buf.readLine();
     //  Read word by word separated by " "
     StringTokenizer tokenizer(str, " ");
     //  Make up for all punctuations
     while(tokenizer.hasMoreTokens())
         String str2 = tokenizer.nextToken();
          if(str2 has ","){
               getRid of "," in str2
          }else if(str2 has "."){
               getRid of "." in str2
          }
          //...  etc
          // place str2 in storage and increase its
          // value
          int i = 0;
          Integer my_int = new Integer(1);
          if(storage_contains_str2){
             my_int = new Integer(storage.get(str2))
             i = my_int.intValue();
             i = i +1;
             my_int = new Integer(i);
           }
           storage.put(str2, my_int);
      }//  end while tokenizer
}//  finished readingLines
 



Now obviously this is very Algorithmic so you can
fill in the blanks and turn this into proper Java
Syntax your self. As mentioned, go through the
Java API (Java Doc), look at what methods you can
use for HashMap and Google reading from a File.

Good luck.

Spike

Chris

Posts: 5
Nickname: jarvio678
Registered: Aug, 2005

Re: Java Help Posted: Aug 10, 2005 3:11 AM
Reply to this message Reply
Thx for that information, I had a little mess around with it and go a little confused.

I tried a different approach and was able to do this:

import java.io.*;

class WordCounter {

// Main

public static void main (String[] args) {
WordCounter t = new WordCounter();
t.fileRead();
}


// Read the file and output

void fileRead() {

String record = null;
int numLines = 0;

try {

FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);

record = new String();
while ((record = br.readLine()) != null) {
numLines++;
System.out.println(numLines + ": " + record);
}
System.out.println("Number of lines:" + numLines);
} catch (IOException e) {

// catch possible io errors from readLine()
System.out.println("IOException error!");
e.printStackTrace();
}

} // End of fileRead

} // End of class


Now this outputs whats on every line of the text file and using the numLines variable in the loop i can output the number of lines in the text file. Now what i'm trying to do is to figure out how i can get it to output the number of characters and words in the text file. And once i got that i want to be able to ouput the number of occurences of every word in the text file and on how many lines that word is contained. And that's where i've got stuck again :/

Thanks again for your previous post and anymore help is much appreciated.

Chris

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Java Help Posted: Aug 10, 2005 3:40 AM
Reply to this message Reply
PLEASE FORMAT YOUR CODE APPROPRIATELY!!

As mentioned you will have to tokenize each line you read.


  StringTokenizer tokenizer = new StringTokenizer(fr.readLine(), " ");
  //  tokenizer is some sort of *iterator* - term loosely used for lack clarity
  //  Each token is a subset of the String fr.readLine() with " " as a delimeter
  //  e.g.  
  String str = "Today was a Good day";
  StringTokenizer tokenizer = new StringTokenizer(str, " ");
  String str1 = tokenizer.nextToken();
  String str2 = tokenizer.nextToken();
  String str3 = tokenizer.nextToken();
  ....  etc...
  
  //  str1 is "Today"
  //  str2 is "was"
  //  str3 is "a"
  ...  etc ...
  
  


Now look at my suggestion again...

Go through a Tutorial for "Java HashMap", and it might make sense.

Once again, "Please follow the instructions on the side of the TextArea
when posting your code, look at the difference between my response and the code you
wrote.

Chris

Posts: 5
Nickname: jarvio678
Registered: Aug, 2005

Re: Java Help Posted: Aug 10, 2005 4:51 AM
Reply to this message Reply
ok thanks for that i'll take a look.

and sorry about the incorrect formatting i wasn't concentrating.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Java Help Posted: Aug 10, 2005 5:11 AM
Reply to this message Reply
I must admit. My my code was not exactly *user-friendly*
Ideally this task should be split up into a bunch
of methods. E.g.

HashMap storage = new HashMap();
public void incrementOccurences(String line){
     readThroughLineViaTheStringTokenizerMethodIexplained
     updateValueOfHashMap via storage.put...
     As explained in previous suggestion.
}



I'm just pressed for deadlines of my own so my appologies,
I could have made it a little more clear.

Though Google will lead you to a wealth of info. with
regards to HashMaps and StringTokenizers, as will
the Java API.

Flat View: This topic has 6 replies on 1 page
Topic: Tick Color for JCheckBox Previous Topic   Next Topic Topic: Compiling Bruce Eckel's

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use