Re: Still in while loop hell, Now with refined question!



Besides all the advice on how to get a line in to your program, you have a separate problem of how to do the loop, and how to stop it.

It is good to focus on one problem at a time. The fact that you can gives you the design principle to separate each problem into separate methods or separate classes.

Many here have spoken of the way to get in a line to parse into your Node list. That makes it one problem, and it deserves its own method. Let's call that method retrieveLine().

public class NodeWorker
{
public String retrieveLine()
{
String result;
// put one of the suggested implementations here
result = ...;
return result;
}
}

Actually, once you fill in that ellipsis, you've got a complete, but so far useless class.

Get that part to compile and then add another method to do something useful, say, create a Node from a String.

Wait! What's a Node? Well, it's something you add to a NodeList that holds a String.

class Node
{
private String data;
private Node next;

public String getData()
{
return data;
}
public void setData( String v )
{
this.data = v;
}

public Node getNext()
{
return next;
}
public void setNext( Node v )
{
this.next = v;
}
}

What I'm showing here is to use standard get/set accessor methods for private variables.

(If the class were generic it would be a Node<T> and you'd substitute T for String in the class definition.)

Now you need to have an add( Node n ) method in NodeWorker. This is where you use that one part of your original program where you manipulate head and prev.

You really need to think about whether prev does any good yet. Start with just having head.

Where does head go? In NodeWorker?

Unlike the other variables, head does not get accessor methods.
Just add( Node n ).

// within NodeWorker
private Node head; // don't presume to initialize it yet

public void add( Node n )
{
if ( n == null )
{
String msg = "Let's avoid null for now, shall we?";
throw new IllegalArgumentException( msg );
}
n.setNext( head );
head = n;
}

Notice that this handles just one eensy-weensy piece of the action. No worries about what the Node has for data, just a little play with the structure.

At this point you should write the main() for NodeWorker and try to get just these methods to do something useful, perhaps watching in a debugger to see if it does what you want.

Or anything at all.

Then add the logic to, say, parse the next token from a line and create a node for it. Test.

Add a loop to main() to check for a next token, then do something with it inside the loop using the methods you've developed by that point.

Make sure to follow blmblm's advice:
It doesn't make sense to check what hasNext() returns *after*
making the call to next(); normal usage is to call next() only
if hasNext() returns true.

Build it one step at time, and it'll come.

--
Lew


.



Relevant Pages

  • Re: changing the world
    ... All lovely delighted preservations wherever answer as the head ... surveyors resign. ... The string other than the regional port is the ...
    (sci.polymers)
  • RE: Passing over Requery
    ... > The other stuff you said went over my head. ... >> Is stFCarton a string data type? ... >> Executemethod of the Database object to set options on the action query. ...
    (microsoft.public.access.formscoding)
  • Re: OT weed eaters
    ... A personal note I hate those bump string extender mechanisms. ... The head can be re-filled without taking the head ... Being a 4 stroke it doesn't smoke, there is no mixing of oil and gas ... I modified the blade head to accept skilsaw blades. ...
    (rec.crafts.metalworking)
  • Re: A challenge to non-SRians
    ... he would have been forced to admit it everytime he posts. ... > the funnybook physics labs when I was a grad student. ... > and managed to wind up the entire string around a pony tail on the ... > top of head. ...
    (sci.physics.relativity)
  • Re: Extract data from String
    ... I need to split this data out into separate columns. ... a random length depending on the data in it. ... string as it is, the following one shows how it should be split up into ... The same functions can be run from within VBA using the RUN ...
    (microsoft.public.excel.programming)