Re-implement

Discussion in 'Java' started by PRadyut, Feb 10, 2005.

  1. PRadyut

    PRadyut Guest

    Request for reimplementation of the program

    ----------------------------------------------------------

    import java.lang.*;
    import java.util.*;
    import java.io.*;

    public class ListApp1
    {

    public static void main(String args[])
    {
    LinkedList list = new LinkedList();
    String he = "test.txt";
    while(true)
    {
    System.out.print("Enter the element: ");
    String test = accept();
    if (test.trim().equals(""))
    {
    displayList(list, he);
    System.exit(0);
    }
    if (test.trim().equals("1"))
    {
    System.out.print("Enter the element to search: ");
    test = accept();
    search(list, test);
    }
    if (test.trim().equals("2"))
    {
    System.out.print("Enter the filename: ");
    he = accept();
    loadList(list, he);
    }
    else
    {
    list.add(test);
    }
    }
    }
    //@time: 11:52 PM 11/17/2004
    //@author:
    public static void loadList(LinkedList list, String test)
    {
    try
    {
    RandomAccessFile file = new RandomAccessFile(test, "rw");
    long g=0;
    file.seek(0);
    while (g!=file.length())
    {
    list.add(file.readLine());
    g=file.getFilePointer();
    }
    file.close();
    }
    catch(Exception e)
    {
    System.err.println("Error in loading: " +e);
    }
    }
    public static String accept()
    {
    String name = null;
    try {
    InputStreamReader keyreader = new InputStreamReader(System.in);
    BufferedReader bfreader = new BufferedReader(keyreader);

    name = bfreader.readLine();
    }
    catch(Exception e)
    {
    System.err.println(e);
    }
    return name;
    }
    public static void displayList(LinkedList list, String ge)
    {
    System.out.println("The size of the list is: " +list.size());
    ListIterator i = list.listIterator(0);
    String km = null;
    try
    {
    while (i.hasNext())
    {
    Object o = i.next();
    if ( o == null)
    System.out.println("null");
    else
    {
    System.out.println(o.toString());
    km = km + o.toString() + "\n";

    }
    FileWriter file = new FileWriter(ge);
    //file.seek(0);
    file.write(km);
    file.close();
    }
    }
    catch(Exception e)
    {
    System.out.println("Caught a exception in writing out: " +e);
    }
    }
    public static void search(LinkedList list, String jam)
    {
    ListIterator i = list.listIterator(0);
    boolean f = false;
    while (i.hasNext())
    {
    Object o = i.next();
    if ( o == null)
    System.out.println("null");
    else if (o.toString().equals(jam))
    {
    f= true;
    System.out.println(o.toString());
    }
    }
    if (f == false)
    System.out.println("String not found");
    }
    }

    ----------------------------------------------------------
    The first line in the file gets the error as null Any help

    Thanks
    Pradyut
    http://pradyut.tk
    http://groups.yahoo.com/group/d_dom/
    http://groups-beta.google.com/group/oop_programming
    India
     
    PRadyut, Feb 10, 2005
    #1
    1. Advertising

  2. PRadyut

    Roland Guest

    On 10-2-2005 11:47, PRadyut wrote:

    > Request for reimplementation of the program
    >
    > ----------------------------------------------------------
    >
    > import java.lang.*;
    > import java.util.*;
    > import java.io.*;
    >
    > public class ListApp1
    > {

    [snip]
    > }
    >
    > ----------------------------------------------------------
    > The first line in the file gets the error as null Any help
    >
    > Thanks
    > Pradyut
    > http://pradyut.tk
    > http://groups.yahoo.com/group/d_dom/
    > http://groups-beta.google.com/group/oop_programming
    > India
    >

    Are you perhaps using NetBeans 4?

    FWIW, the program compiles and runs with JDK 1.5.0 from the commandline,
    and from within Eclipse 3.0.1.
    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Feb 10, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    8,627
    Roadie Roger
    Sep 23, 2003
  2. Oleg
    Replies:
    5
    Views:
    2,467
    Ray Andraka
    Feb 18, 2004
  3. pandora
    Replies:
    0
    Views:
    567
    pandora
    Apr 14, 2004
  4. Sidney Cadot
    Replies:
    0
    Views:
    2,366
    Sidney Cadot
    Apr 18, 2004
  5. fnord

    trying to implement a timer

    fnord, Aug 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    1,099
    Eric Frazier
    Sep 12, 2003
Loading...

Share This Page