Re: Still in while loop hell, Now with refined question!
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Tue, 18 Sep 2007 02:00:01 -0400
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
.
- Follow-Ups:
- References:
- Still in while loop hell, Now with refined question!
- From: mmoski
- Re: Still in while loop hell, Now with refined question!
- From: mmoski
- Re: Still in while loop hell, Now with refined question!
- From: blmblm
- Re: Still in while loop hell, Now with refined question!
- From: mike . mainguy
- Still in while loop hell, Now with refined question!
- Prev by Date: Re: Design choices and patterns for passing contextual runtime information.
- Next by Date: Re: Still in while loop hell, Now with refined question!
- Previous by thread: Re: Still in while loop hell, Now with refined question!
- Next by thread: Re: Still in while loop hell, Now with refined question!
- Index(es):
Relevant Pages
|