Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re-implement

Reply
Thread Tools

Re-implement

 
 
PRadyut
Guest
Posts: n/a
 
      02-10-2005
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

 
Reply With Quote
 
 
 
 
Roland
Guest
Posts: n/a
 
      02-10-2005
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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57