Reputation: 27
I've created this class along with the linkedlist class and they are in the same folder. The input appears to work fine besides the fact that the runner class won't output anything, even when I enter p into the command prompt. Even when I encapsulate the methods in System.out.print, nothing happens.
Runner class
public class runner{
static String s;
public static void main(String[] args) throws java.io.IOException{
linkedlist link= new linkedlist();
System.out.println("Type a command\n");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try{
s=in.readLine();
char first=s.charAt(0);
while(in.readLine()!=null){
s=in.readLine();
int space= s.indexOf(" ");
if(first=='i'){
String w=s.substring(space);
link.insert(w);
}
if(first=='d'){
String w=s.substring(space);
link.delete(w);
}
if(first=='f'){
String w=s.substring(space);
link.find(w);
link.find(w);
}
if(first== 'p'){
link.printlist();
}
}
}catch(Exception e) {
System.out.println("Ack!: " + e);
}finally{
in.close();
}
}
}
Linked List class
import java.io.*;
public class linkedlist{
node head;
public linkedlist(){
head=null;
}
public void insert(String s){
head= new node(s,head);
}
public void printlist(){
node i= head;
while(i.getData(i)!=null){
System.out.print(i.getData(i));
i=i.getNext();
}
}
public String find(String s){
String comp=head.getData(head);
node ref=head.getNext();
String check=ref.getData(ref);
String temp=check;
while(check != "null"){
if(s.equals(check)){
head=ref;
ref=head.getNext();
check=ref.getData(ref);
}
else{
temp=check;
}
}
return temp;
}
public String delete(String s){
String comp=head.getData(head);
node ref=head.getNext();
String check= ref.getData(ref);
node ref2= ref.getNext();
String post=ref2.getData(ref2);
String temp=check;
while(check != "null"){
if(s.equals(check)){
ref=ref2;
}
else{
comp=check;
ref=head.getNext();
check=ref.getData(ref);
ref2= ref.getNext();
post=ref2.getData(ref2);
}
}
return temp;
}
}
Upvotes: 1
Views: 45
Reputation: 19194
I guess it happens because you are swallowing several input lines here:
try{
s=in.readLine(); //you consume one line here
char first=s.charAt(0);
while(in.readLine()!=null){ //you consume another here
s=in.readLine(); //and another one..
replace it with:
while ((s = in.readLine()) != null) { // while loop begins here
char first=s.charAt(0);
[...]
} // end while
Upvotes: 1