Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

please help java linked lists

JavaIsFunJavaIsFun Posts: 3Member
[b]I am trying to: [/b]
[color=Blue]1)read a word from a txt file
2)search for the word in my linked list (at beginning it will be empty)
3)if the word is found then increment count for that word
4)if the word is not found insert it into the list [/color]

The goal is to output all words from the txt file and sort them depending on how many times a word appears. So far I can read the txt file into a linkedlist called ll and then print it out again. I would really appreciate some advice on how to set up my loops to get where I need to go with this.

Thanks in advance!


[code]public static void getTxt(){
//read from txt
try{
BufferedReader in=new BufferedReader(new FileReader("biblewords.txt"));
String words;
LinkedList ll=new LinkedList();
long start, stop;
start=System.currentTimeMillis();
//read whole file line by line and stick it in ll
while((words=in.readLine()) !=null){
ll.add(words);
System.out.println(words);
}//end while
in.close();//closes buffer reader
String first=ll.getFirst();
String last=ll.getLast();
System.out.println(ll.indexOf(first) +": "+ first);
System.out.println(ll.indexOf(last) +": "+ last);
stop=System.currentTimeMillis();
System.out.println("Time elapsed: "+((stop-start)/1000.0)+" seconds");//times how long it takes to complete this section of code
}catch (Exception e){
System.err.println("This program has encountered an error: " + e.getMessage());
}//end try catch
}//end getTxt[/code]

Comments

  • JavaIsFunJavaIsFun Posts: 3Member
    It forgot to mention that I am doing all of this in that while loop. I will need an if/else statment to check if it is in the linked list and then somehow increment a counter for that word if it allready lives there. That is what I need help with : )

    Thanks again
Sign In or Register to comment.