0

So for homework I have to write a program that creates a linked list of names, makes it circular and then deletes every nth node until there is only one thing left in it. My code prints out that the things were taken out but never actually takes them out, could someone tell me why? Note: as it is homework I am not looking for exact answers, more of helpful advice on how to fix my code

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Survivor {
    public static void main(String[] args){
        ListNode list=makeNode();
        ListNode t=list;
        Scanner sc=new Scanner(System.in);
        while(t.getNext()!=null){           
            System.out.print(t.getValue() + "; ");
            t=t.getNext();
        }
        System.out.println();
        System.out.print("Enter a number to kill: ");
        int kill=sc.nextInt();
        kill(kill,list);    
        t=list;
        while(t.getNext()!=null){           
            System.out.print(t.getValue() + "; ");
            t=t.getNext();
        }
    }

    public static ListNode makeNode(){
        ListNode names=null;
        File data=new File("names.txt");
        Scanner fl=null;
        ListNode t=names;
        try {
            fl=new Scanner(data);
        } catch (FileNotFoundException e) {
            //handle errors
            System.out.println("The File isnt there"+data.getAbsolutePath()+"\"");
            System.exit(-1);
        }
        while(fl.hasNext()){
            String theName=revName(fl.nextLine());
            if(t!=null){
                if(t.getNext()!=null){
                    ListNode t2=t.getNext();
                    if(theName.compareToIgnoreCase((String) names.getValue())<0){
                        names=(new ListNode(theName, names));                   
                    }
                    if(theName.compareToIgnoreCase((String) names.getValue())>0){               
                        while(t.getNext() !=null &&theName.compareToIgnoreCase((String) t2.getValue())>0 ){                     
                            t=t.getNext();
                            t2=t2.getNext();
                        }
                        t.setNext(new ListNode((theName),t2));
                    }
                }
                if(t.getNext()==null){
                    if(theName.compareToIgnoreCase(((String)t.getValue()))<0){
                        names=(new ListNode((theName),null));
                    }
                    else{
                        t.setNext(new ListNode(theName,null));
                    }
                }
                t=names;
            }
            if(names==null){
                names=new ListNode(theName,null);
                t=names;
            }
            t=names;
        }
        return(names);
    }

    public static String getCauseOfDeath(String name){
        int first=(int)(Math.random()*10);
        String cause = "fell off of a cliff";
        if(first==1)
            cause="tripped";
        if(first==2)
            cause="got a boo-boo and died";
        if(first==3)
            cause="insulted the gods";
        if(first==4)
            cause="had a coconut fall on their head";
        if(first==5)
            cause="drowned";
        if(first==6)
            cause="showed up to cuadrados class late";
        if(first==7)
            cause="tried to steal honey from giant bees and failed";
        if(first==8)
            cause="doesn't even lift";
        if(first==9)
            cause= "got bitten by a unicorn and poisoned";
        if(first==10)
            cause="got lost and starved";
        return("Alas, the mighty " + name+ " has fallen; " + name + " "+cause);
    }

    public static String revName(String name){
        String name1;
        String name2;
        int space;
        int dot;
        dot=name.lastIndexOf(".");
        if (dot == -1){
            name= name.trim(); //Trims spaces at front and end
            space=name.lastIndexOf(" ");

            if (space == -1){  
                //If there are no spaces in the code this line just returns the name
                return name;
            }
            else{
                name1=name.substring(0, space);
                name2=name.substring(space+1);
                if(name2.length()>1){
                    return name2+ " " + name1;
                }
                else{
                    space=name.indexOf(" ");
                    name1=name.substring(0, space);
                    name2=name.substring(space+1);
                    return name2+" "  + name1;
                }
            }
        }
        return (name);
    }

    public static void kill(int i, ListNode list){
        ListNode t=list;
        ListNode t2=t.getNext();
        int z=2;
        while(t2.getNext()!=null){          
            if(i==z){       
                System.out.println(getCauseOfDeath(revName((String) t2.getValue())));
                t.setNext(t2.getNext());
                z=0;
            }
            z++;
            t=t.getNext();
            t2=t2.getNext();
        }
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
user2319595
  • 23
  • 1
  • 3

2 Answers2

0

Perhaps it has something to do with passing parameters to kill method?

Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
athabaska
  • 455
  • 3
  • 22
0

You might look into using the remove functionality of linked lists to remove an element. Also dont forget that Java does manipulate objects by reference, and all object variables are references. BUT, Java doesn't pass method arguments by reference; it passes them by value. Take a look here for more information on how things are passed in Java.

Kyle
  • 553
  • 2
  • 7