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: Switch + Case - comp.lang.asm.x86Sometimes, some software may need while loop that has to scan __1, __2, or __3, but ... want to use is currently registered. this sounds like i have to use a hell of a ... improve strlen - comp.lang.asm.x86Example, the first while loop might not do what expected if x has certain value ... Maybe on 386 or older processor it might pay off, hell, most likely it would. why in hell the uigetdir works so slowly? - comp.soft-sys.matlab ...I just woder why in hell the uigetdir function works so ... long to run, unless you're calling it inside a loop or ... using mdadm and LVM - comp.os.linux ... >> But while you ... Neatest way to get the end pointer? - comp.lang.c... my_array/sizeof*my_array; do *p++ = 42; while ... It's a hell of a lot shorter to write, and also I think ... At no stage is pend dereferenced; and the loop exits on ... How to generate random number without replacement? - comp.lang ...my $i = @$deck; while (--$i ... isn't needed as it is easily made from a hash and a loop ... WHO THE HELL ARE YOU AND WHAT ARE YOU DOING HERE? Chage passwords in script without expect - comp.unix.solaris ...BOFH == Bastard Operator From Hell, as immortalised by ... Why not just quit now while you're still living in the ... Cisco 2610 stuck in boot loop! - comp.dcom.sys.cisco ... ... Solidworks Dongles - comp.cad.solidworksJerry, I've just gone through this loop and I bought a ... just by making a lot of noise and complaining like hell! ... Once in a while, strange problems can happen when a ... input & output in assembly - comp.lang.asm.x86While you're there, you might want to look at HLA (the ... @=$100 loop: bsr.l getc ; get char from ... And if you need more RAM than that, why the hell are ... Sampling: What Nyquist Didn't Say, and What to Do About It - comp ...... com Do you need to implement control loops in software? ... I had a hell of a time arguing with "Nyquist" theorists ... mid-line", finish the line with your right hand -- while ... Parsing file names with spaces - comp.lang.perl.miscOh hell, have a fish. ----- #!/usr/bin/perl use warnings ... > @files = <DATA>; > foreach (@files) { > print; > } A for-loop? Make it a while. while loop - C++ Forum - cplusplus.com - The C++ Resources NetworkI made up my own while loop to imitate cruse control on a car but theres just one error ... 100) i onow its supposed to be 2 lines like II but thats two I's - what the hell ... C programming, how to make a incremental loop and what the hell is ...C programming, how to make a incremental loop and what the hell is it asking for? ... statment can be used for a delay, inser it inside the while loop as the ... 7/17/2012 2:39:01 AM
|