• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

help me please??

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i'm a student(BSIT) and i'm having a hard time figuring out how to make my code work...
about my code: it is called MyLinkedList.java. Unfinished because i got stuck with the loops! As obvious as its name implies, this code implements linked list. The catch is, is that the input will be coming from a textfile. I used bufferedReader to read the whole file and StringTokenizer to read the individual lines in the files...
the code:
import java.io.*;
import java.util.*;
class Item
{
public Item( Object d, Item n )
{
data = d;
next = n;
}

public Object data;
public Item next;
}
class MyLinkedList
{
private Item head;
private Item tail;

//head is the first element in the list.
//tail is the last element in the list.
public MyLinkedList()
{
head = null;
tail = null;
}

public void addTail( Object data )
{
Item newTail = new Item( data, null );
if( head == null )
{
head = newTail;
tail = head;
}
else
{
tail.next = newTail;
tail = newTail;
}
}

public void addHead( Object data )
{
Item newHead = new Item( data, head );
if( head != null )
head = newHead;

else
{
head = newHead;
tail = head;
}
}

public void add( Object data, int index )
{
if( ( index == 0 ) || ( head == null ) )
addHead( data );
else
{
Item current = head;
while( ( current != null ) && ( index > 1 ) )
{
current = current.next;
index--;
}
if( current != null )
{
current.next = new Item( data, current.next );
if( current == tail )
tail = current.next;
}
}
}

public Object removeHead()
{
if( head == null )
return null;
Object data = head.data;
head = head.next;
return data;
}

public int find( Object data )
{
if( head == null )
return -1;
int index = 0;
Item current = head;
while( ( current != null ) && ( !current.data.equals( data ) ) )
{
index++;
current = current.next;
}
if( current == null )
return -1;
else
return index;
}

public Object remove( int index )
{
if( index == 0 )
return removeHead();
if( head == null )
return null;
Item current = head, previous = null;
while( ( current != null ) && ( index > 0 ) )
{
previous = current;
current = current.next;
index--;
}
if( current == null )
return null;
Object data = current.data;
previous.next = current.next;
return data;
}

public String toString()
{
if( head == null )
return "<empty>";
Item current = head;
String res = "{";
while( current != null ){
res = res + current.data;
if( current.next != null )
res += ",";
current = current.next;
}
res += "}";
return res;
}


public static void main( String args[] )
{

System.out.println("Enter file name:");
String fileName = SavitchIn.readLine();

try
{
BufferedReader inputStream =
new BufferedReader(new FileReader(fileName));


String line;
boolean done = false;
while(!done)// to get to the next line
{
line = inputStream.readLine();

StringTokenizer inputReader = new StringTokenizer(line);
//System.out.println(inputReader);
while(inputReader.hasMoreElements())//figures out every element in the line.
{

if (inputReader.nextElement().equals("d"))
{

Object x = inputReader.nextElement();

System.out.println("x = " + x);
System.out.println("element x is removed");

}
else if(inputReader.nextElement().equals("i"))

System.out.println("the element after the next is inserted");

else if(inputReader.nextElement().equals("ie"))
System.out.println("the next element is inserted at the end of the list");
else
{
boolean finish = true;
finish = inputReader.hasMoreElements();

}
line = inputStream.readLine();
}
done = true;

}
inputStream.close();
}

catch(FileNotFoundException e)
{
System.out.println(fileName + " not found!");
}

catch(IOException e)
{
System.out.println("Error reading" + fileName);
}


}
}
example input from a text file:
d 10 //remove 10
ie 2 //insert 2
the output:
Enter file name:
sample.txt
x = 10
element x is removed.
my problem:
my first while loop seemed to be incorrect, suppose to be, it would switch to every line but it wont switch, my second while loop worked though...as i said earlier, i got stuck with the loops that i can't complete my main...so can some genius help me please! my life is at stake...
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc,
For your outer loop which reads each line from the input file, use the fact that readLine() returns null when eof is reached in your while statement:
[code]
String line;
while( (line = inputStream.readLine()) != null)
{
// add your code to act on each line here
}
This is a much better method for reading a file. Your method will fail if the file is empty - done will be true so the loop will be entered, but the first call to readLine will set line to null, and the next call to the tokenizer will generate an null pointer exception because of the null line.
The method above will not execute the loop if the file is empty.
Try that, and post if you are still having problems,
 
marc perry batuigas
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ic! i tried that last time, but did this :
code:
String line = null;
while(line!=null)
{
line = inputStream.readLine();
}
----
did'nt like the way it reads...
haven't thought of that...thanks man
oh and one more thing...i just realized how hard it is to call back the functions/methods of Linklist when the line is already contained into the tokenizer, the "ifs" part...so how do i call the methods, e.g. remove,add, etc...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on an extremely similar project for my class as well, and also running into a looping problem.
I'm trying to determine how BufferedReader's readLine method works...is it supposed to move to the next line in the buffer after each line is read? How can you determine the number of lines in a buffer to cycle through them?
Here's my code...it seems to be only reading the first line of my file and getting stuck there infinitely:
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch LeeAnn
How many items are there in each line?
Because createOrderArray attempts to read 20 tokens from StringTokenizer, but tok is only given a line as input.
 
LeeAnn Sumner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your welcome! I'm glad to find such a friendly knowledgable community!
There are five elements in each line and four lines to the data file that I'm reading in. I'm trying to read each element from each line, assign it to a variable then pass it to the OrderWithCharge constructor to create an array of OrderWithCharge objects.
I think part of my problem was that I needed to process the whole buffer into an array of tokenized Strings to pass to the method that creates the order array instead of passing one String (one line) at a time, since it was only reading the first line and never moving on. At least I think that was my problem....right now I'm dealing with another infinite loop and a NullPointerException error in this code:

The error refers to three places: the line in my main method in another class that calls to the processOrderInFile method, the line where createOrderArray is called and passes the tokenArray, and the line where the item variable is defined in createOrderArray (item = token.nextToken() ;) .
I'm not sure what it means by NullPointerException...I'm thinking that somewhere my array is empty or something isn't lined up right, but I'm having a hard time knowing what to look for. Your help is most appreciated!
[ May 09, 2004: Message edited by: LeeAnn Sumner ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic