3 Replies - 1048 Views - Last Post: 09 October 2012 - 07:08 PM Rate Topic: -----

#1 TenthDoctor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-September 12

Collections Assignment

Posted 09 October 2012 - 03:48 PM

So for this assignment in my Java course, we're working with collections. This program reads a text file into an ArrayList and then accepts user input that is then cross-checked with said list.

The trouble I'm having involves my user input. No matter what I try, (moved input statements into main method, changed scope of my input variable, changed my use of the Scanner) I cannot get the input to cooperate with the rest of the program. The exception to this would be when I hard-code a value I know is in the list into binarySearch. For testing, I've been using "word" (which IS in this list).

This is the error I have been getting consistently:

Please enter the word you would like to search for:
word
Exception in thread "main" java.lang.NullPointerException
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.Scanner$1.create(Unknown Source)
at java.util.Scanner$1.create(Unknown Source)
at sun.misc.LRUCache.forName(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at com.jeffstateonline.tfinnega.WordCheck.input(WordCheck.java:48)
at com.jeffstateonline.tfinnega.WordCheck.go(WordCheck.java:20)
at com.jeffstateonline.tfinnega.WordCheck.main(WordCheck.java:15)

SO. My question is...what have I overlooked? I feel like I'm really close but missing something obvious. Any help would be much appreciated.

Attached is my code as well as the text file.

import java.io.*;
import java.util.*;

public class WordCheck {
	ArrayList<String> wordList = new ArrayList<String>();
	String word;
	
	public static void main(String[] args) {
		new WordCheck().go();
	}
	
	public void go(){
		getWords();
		input();
		int  i = Collections.binarySearch(wordList, word);
		if(i > 0){
			System.out.println("This word DOES exist within this list.");
		}
		else{
			System.out.println("This word does NOT exist in this list.");
		}
	}
	
	public void getWords(){
		try{
			//Collection statements go here
			File file = new File("wordlist.txt");
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = null;
			while((line = reader.readLine()) != null){
				wordList.add(line);
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void input(){
		Scanner kb = new Scanner(System.in);
		System.out.println("Please enter the word you would like to search for: ");
		kb.next();
		kb.next(word);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Collections Assignment

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,655
  • Joined: 20-September 08

Re: Collections Assignment

Posted 09 October 2012 - 04:20 PM

Quote

kb.next();
kb.next(word);

Try instead
if(kb.hasNext()) {
  word = kb.next(word);
}

Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Collections Assignment

Posted 09 October 2012 - 06:23 PM

And not a good idea at all to create a new Scanner object for each input

  public void input(){  
         Scanner kb = new Scanner(System.in);  



When you have a phone conversation with a friend do you hang up and redial at each sentence ?
Assign ONE Scanner to System.in and use it through your whole application
Was This Post Helpful? 1
  • +
  • -

#4 TenthDoctor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-September 12

Re: Collections Assignment

Posted 09 October 2012 - 07:08 PM

Thank you both so much! Using a combination of your suggestions cleared up the problem entirely.

By removing the variable from the parameter list for hasNext and then moving my Scanner object up top to make it global got my methods communicating properly (I think).

Again, thanks for the assist. I'll keep this solution in mind for future reference so I won't run into the same problems. I'm still very much a novice programmer but learning through little problems like this always reaffirms my thirst for knowledge and my love of problem-solving. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1