The Artima Developer Community
Sponsored Link

Java Answers Forum
Using filter to get #s from file, sort, show...help?

0 replies on 1 page.

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 0 replies on 1 page
Pyriel Baranos

Posts: 3
Nickname: pyriel
Registered: Jul, 2002

Using filter to get #s from file, sort, show...help? Posted: Aug 21, 2002 12:48 PM
Reply to this message Reply
Advertisement
Ok, here is the deal. I am getting ready for a test and trying to practice stuff, so the prof. gave me a lab. It is supposed to basically use a filename as a command line argument, read the #s in, and print them out in a neat 5 column style.

The #s are supposed to be sorted first, lowest to highest [without using arrays.sort()]. Also, the first # in the file needs to be set to the array length. He said to use Integer.toString() for spacing, and to make a reusable file-sorting object.

He also is letting us use a LineFilter class that we have made before in class (see below). If anyone can throw some code to give me an idea how to do it / start, I would appreciate it.

//******************
// LineFilter.java *
//******************
import java.io.*;
 
public class LineFilter
{
  private BufferedReader m_file;
  private String m_line;
 
  public LineFilter(String fileName)
  {
    setFile(fileName);
  }
 
  public LineFilter()
  {
    m_line = "";
  }
 
  public void setFile(String fileName)
  {
    try
    {
      m_file = new BufferedReader(new FileReader(fileName));
    }
    catch(IOException e)
    {
      throw new IllegalArgumentException (e.getMessage());
    }
  }
 
  public String readLine()
  {
    if (m_file == null)
      throw new IllegalStateException ("No such file!");
 
    try
    {
      do
      {
        int comments;
        m_line = m_file.readLine();
        comments = m_line.indexOf("#");
 
        if (comments >= 0)
        {
          m_line = m_line.substring(0, comments);
        }
      }while (m_line.compareTo("") == 0);
    }
 
    catch(IOException e)
    {
      return null;
    }
 
    return m_line;
  }
 
  public static void main(String[] args)
  {
    LineFilter filter = new LineFilter();
 
    for (int i=0; i<args.length; ++i)
    {
      try
      {
        filter.setFile(args[i]);
      }
      catch (RuntimeException e)
      {
        System.out.println("Error: " + e.getMessage());
        continue;
      }
      String line = filter.readLine();
      while (line != null)
      {
        System.out.println(line);
        line = filter.readLine();
      }
    }
  }
}
 

Topic: how to do this program Previous Topic   Next Topic Topic: Java applet status bar issue !!!

Sponsored Links



Google
  Web Artima.com   

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