M
mmoski
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.
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;
}
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.
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;
}