While loop hell.

  • Follow


This program, in theory, should read words from system.in and storeeach in a simple list.  Using, System.out I know for sure that thelist works fine.  It's a problem with my while loop.  It never ends.I've heard things about using == to compare strings instead of != butnothing works!  Any help would be greatly appreciated.Thank you for your time.import java.util.Scanner;public class Parse {	public static void main (String[] args) { 		Scanner sc = new Scanner(System.in);		Node head = null;		Node prev = null;		String read = sc.next();		while(sc.next() != null){		        Node a = new Node(); 		   	a.data = read;	    	if(head == null){	    		head = a;	    	}else{	    		prev.next = a;	    	}	    	prev = a;	    	read = sc.next();	    }		System.out.println("Works"); // Doesn't get to this point         for(Node pointer = head; pointer != null; pointer =pointer.next){    		System.out.println(pointer.data);    	}	}}class Node{	public Node next;	public String data;}
0
Reply mmoski (8) 9/16/2007 8:27:59 AM

mmoski <mmoski@gmail.com> writes:> It's a problem with my while loop.  It never ends.....> import java.util.Scanner;>> public class Parse {>> 	public static void main (String[] args) {>>  		Scanner sc = new Scanner(System.in);>> 		Node head = null;> 		Node prev = null;> 		String read = sc.next();>> 		while(sc.next() != null){You might mean                 while(read != null)since you are doing two calls to "sc.next" for each loop iteration.It would be prettier, and probably better working, to do:                while(sc.hasNext()) {                  String read = sc.next();and only have the one call to "sc.next".Your real problem is that the call "sc.next()" will *never* returnnull. It will block if no input is available, and that is where yourprogram gets stuck.<URL:http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#next()>/L-- Lasse Reichstein Nielsen  -  lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>  'Faith without judgement merely degrades the spirit divine.'
0
Reply Lasse 9/16/2007 10:07:38 AM


On Sun, 16 Sep 2007 01:27:59 -0700, mmoski <mmoski@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>import java.util.Scanner;

see http://mindprod.com/jgloss/scanner.html
for an example of use.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Reply Roedy 9/16/2007 1:36:46 PM

mmoski <mmoski@gmail.com> wrote:> This program, in theory, should read words from system.in and store> each in a simple list.  Using, System.out I know for sure that the> list works fine.  It's a problem with my while loop.  It never ends.> I've heard things about using == to compare strings instead of != but> nothing works!  Any help would be greatly appreciated.Aside from what the other responses, I have one comment.If you're doing this for a homework assignment, good.  You should dothings by hand at least once.  But if this for production code, considerusing java.util.LinkedList<String>:final Scanner sc = new Scanner(System.in);final List<String> words = new LinkedList<String>();while (sc.hasNext()) {  words.add(sc.next());}The LinkedListclass does all the work of setting up nodes, using its owninner class.-- Respectfully,Eric Jablow
0
Reply ejablow 9/16/2007 3:06:05 PM

3 Replies
102 Views

(page loaded in 0.077 seconds)

Similiar Articles:













7/17/2012 2:39:01 AM


Reply: