The Artima Developer Community
Sponsored Link

Java Answers Forum
homework help

2 replies on 1 page. Most recent reply: Jan 25, 2003 6:44 PM by Charles Bell

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 2 replies on 1 page
josh lambert

Posts: 1
Nickname: codex
Registered: Jan, 2003

homework help Posted: Jan 24, 2003 4:37 PM
Reply to this message Reply
Advertisement
I'm lost on my homework and need a bit of help...i can't figure out a loop that will only print the distinct words while keeping their count...the lab and my code so far is below...

In this lab you will write a program that reads in a text file and then display all words appearing in this file one by one. If a word has appeared more than once you should display it just one time. You should also display the number of times each word has appeared in the file. You are required to use JFileChooser to let the user pick the input file. Moreover, words are considered case-insensitive, meaning "the" and "The" ought to be considered the same.

For example, if your input file has the following content

Already a force in software for personal digital assistants, Microsoft is ramping up efforts to provide the operating systems for those devices' close relative: the next generation of "smart" cell phones that can handle e-mail, digital photos and other data.

Microsoft splashed into the smart-phone scene in Europe last year with a device sold by the British wireless carrier Orange.

Your output should look like (or very similar to):

already 1
a 2
force 1
in 2
software 1
for 2
personal 1
digital 2
assistants 1
microsoft 2
is 1
ramping 1
up 1
efforts 1
to 1
provide 1
the 4
operating 1
systems 1

here's what i've got...


package arrayreader;
import java.io.*;
import javax.swing.*;
import java.util.*;
 
public class ArrayThing
{
  public static void main(String[] args)
  {
  JFileChooser chooser = new JFileChooser ("C:\\Documents and Settings");
  if(chooser.showOpenDialog (null)  == JFileChooser.APPROVE_OPTION)
  {
    File selectedFile = chooser.getSelectedFile();
    try
    {
      FileReader in = new FileReader (selectedFile);
      BufferedReader br = new BufferedReader (in);
 
      String line = br.readLine();
 
      ArrayList thing = new ArrayList();
 
      while (line != null)
      {
        StringTokenizer st = new StringTokenizer (line, "\t, ");
        while (st.hasMoreTokens())
        {
          String sToken = st.nextToken ();
          thing.add(sToken);
        }
 
        line = br.readLine();
      }
 
      br.close();
 
      System.out.println(selectedFile.getName());
 
      Collections.sort(thing);
 
      for (int i =0; i < thing.size(); i++)
      {
        System.out.println(thing.get(i));
      }
 
    }
    catch (FileNotFoundException e)
    {
      System.out.println("File not found!");
    }
    catch (IOException e)
    {
      System.out.println("Error reading file!");
    }
  }
  else
  {
    System.out.println("Please chose a file!");
  }
 
  System.exit(0);
  }
}


Swaraj

Posts: 24
Nickname: swaraj
Registered: Mar, 2002

Re: homework help Posted: Jan 25, 2003 2:25 AM
Reply to this message Reply
Hi Josh

Dont expect someone else to do your homework, Homework is meant to test your knowledge of understanding of concepts. so if you really need help, this is not the place for it, kindly look elsewhere.

Swaraj

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: homework help Posted: Jan 25, 2003 6:44 PM
Reply to this message Reply
/*
*/
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;
 
public class DisplayWords{
 
    public static void main(String[] args){
        DisplayWords displayWords = new DisplayWords();
        File textFile = displayWords.getFile();
        String fileText = displayWords.readFile(textFile);
        Vector wordsInFile = displayWords.getWords(fileText);
        ListIterator iterator = wordsInFile.listIterator();
        while (iterator.hasNext()){
            String nextWord = (String)iterator.next();
            System.out.println(nextWord + " " + String.valueOf(displayWords.countOccurrences(fileText, nextWord)));
        }
        System.exit(0);
    }
    
    public File getFile(){
        File file = null;
        JFileChooser fileChooser = new JFileChooser(new File("."));
        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) file = fileChooser.getSelectedFile();
        return file;
    }
    
    public String readFile(File file){
        String text = "";
        try{
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String nextLine = "";
            while ((nextLine = br.readLine()) != null){
                text = text + nextLine;
            }            
        }catch(FileNotFoundException fnfe){
            System.err.println("FileNotFoundException: " + fnfe.getMessage());
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());            
        }
        return text;
    }
    
    public Vector getWords(String s){
        Vector words = new Vector();
        StringTokenizer st = new StringTokenizer(s, " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
        while (st.hasMoreTokens()){
            String nextWord = st.nextToken();
              if ((hasNoPunctuation(nextWord)) && (!isInList(words, nextWord)) && (nextWord.trim().length() > 0)) words.add(nextWord);
        }
        return words;
    }   
 
    public boolean hasNoPunctuation(String s){
        return !Pattern.matches("\\p{Punct}",s);
    }
    
    public boolean isInList(Vector v, String s){
        return (v.contains(s.toLowerCase()) || v.contains(s.toUpperCase()));
    }
    
    public int countOccurrences(String text, String word){
        int count = 0;
        StringTokenizer st = new StringTokenizer(text, " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
        while (st.hasMoreTokens()){
            String nextWord = st.nextToken();
              if ((hasNoPunctuation(nextWord)) && (word.toLowerCase().compareTo(nextWord.toLowerCase()) == 0) && (nextWord.trim().length() > 0)) count++;
        }
        return count;
    }
}

Flat View: This topic has 2 replies on 1 page
Topic: urgenthelp Previous Topic   Next Topic Topic: parallelport

Sponsored Links



Google
  Web Artima.com   

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