Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
please help java linked lists Posted by JavaIsFun on 18 Jan 2012 at 1:08 PM
I am trying to:
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


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!


public static void getTxt(){
	//read from txt 
		try{
			BufferedReader in=new BufferedReader(new FileReader("biblewords.txt"));
			String words;
			LinkedList<String> ll=new LinkedList<String>();
			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


Report
Re: please help java linked lists Posted by JavaIsFun on 18 Jan 2012 at 1:13 PM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.