Re-implement

P

PRadyut

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: (e-mail address removed)
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
 
R

Roland

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_/ /__/
/ \ /_/ / \
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top