Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 19th, 2006, 3:48 AM   #1
xenop
Newbie
 
Join Date: Jan 2006
Location: Lancaster, California
Posts: 11
Rep Power: 0 xenop is on a distinguished road
Send a message via AIM to xenop Send a message via Yahoo to xenop
Need help will bug: function parameter.

Question
Im having a problem figuring out why the parameter in the scram() function is always null. Then causing a null pointer exception in the for loop.

Purpose of program
This program is supposed to read in from a file of strings scramble the string and then try to find it in a dictionary file. Then split the string if its long enougth and find matching smaller strings.

Im using Eclipse 3.1 with jdk 6.1

I appriciate any help.

import java.io.*;    // for BufferedReader
import java.util.*;
    public class scramble  {
   
       public static void main(String[] args) throws IOException {
      
         String fileName = "inputstrings.txt", wordString, input;
         BufferedReader inFile = new BufferedReader(new FileReader(fileName));
         wordString = inFile.readLine();
         while (wordString != null) {
            System.out.println("Input word: " + wordString);
            ScrambleWord dummy = new ScrambleWord(wordString, "wordlist.txt");
            dummy.OutputData();
            wordString = inFile.readLine();
         }
         inFile.close();
      }
   }
    
    class ScrambleWord {
    	  
    	
    	 private Random generator = new Random();
    	 private String dictFile;
    	 private String input;
    	 private String scrWord;
    	 private Vector tempVector = new Vector();
    	 private String[] dictionaryArray;
    	 
    	ScrambleWord(String word, String fileName) throws IOException {
   	           
    		   dictFile=fileName;
    		   
    		  input = word;
    		  scram(input);
   	           
    		   
   	           
   	           BufferedReader inFile = 
   	        	   new BufferedReader(new FileReader(dictFile));
   	           while(!(inFile.readLine()== null)){
     	        	   tempVector.add(inFile.readLine());
   	           }
   	        String[] dictionaryArray = new String[tempVector.size()]; 
   	       	
   	         tempVector.copyInto(dictionaryArray);
   	        this.dictionaryArray = dictionaryArray;
     	   	        	
    	}
 		
    		private void scram(String input){
    			 String a = "";
    			 System.out.println(input);
    			for (int i = 0 ;i < (input.length()-1);i++){
    			if (generator.nextBoolean())
    				a = input.charAt(i) + a;
    				
    				else 
    					a = a + input.charAt(i);
        		}
    				scrWord = a;
    	}
    	
    	public void OutputData() {
    		
    		//System.out.println("testS2 " + scrWord);
    		
    		long start = System.currentTimeMillis();
    		// exit condition for while loop
    		boolean reportedSingle = false; 
    		boolean reportedDouble = false; 
    		boolean reportedTriple = false; 
    		//loops for a the a set time or if the word has been found
    		String b;
    		while((System.currentTimeMillis()-start) < 60000 ){
    			//System.out.println(System.currentTimeMillis()-start);
    			
    			 b = search(input, dictionaryArray);
    			
    		         if (b == null ){
    		        	 scram(b);
    		        	 //System.out.println(scrWord + "not found");
    	
    		         }
      				
    		         else if  (!reportedSingle){
    		        	 
    		        	 System.out.println("single:" + b);
    		        	reportedSingle = true;
    		        		
    		         }
    		         
    		         if (b.length() > 6 && !reportedDouble){
    		        	 int mid = b.length() / 2;
    		        	 String firstWord = b.substring(0, mid-1);
    		        	 String secondWord = 
    		        		 	b.substring(mid, b.length()-1);
    		        	 
    		        	scram(firstWord);
    		        	  String c = scrWord;
    		        	 scram(secondWord);
    		        	 	String d = scrWord;
    		        	 
    		        	 if ((search(firstWord, dictionaryArray)== null ))
    		        		 scram(firstWord);
    		        	        
    		        	 	else {
    		        	 		search(firstWord, dictionaryArray);
    		        	 		System.out.println("first word:" + firstWord); 
    		        	 	}
    		        	 if ((search(secondWord, dictionaryArray)== null ))
    		        		 scram(secondWord);
    		        	 	else {
    		        	 		search(secondWord, 
    		        					 			 dictionaryArray);
    		        			 System.out.println("first word:" + firstWord);
   
    		        	 }
    		        	
    		        }
    		         reportedDouble = true;
    		         
    		}
    	
    	}
    		
    	static String search(String targetWord, String[] sArray){
    		//System.out.println("index: "+ (sArray.length-1) );
    	   return searchHelper(targetWord, sArray, 0, (sArray.length-1));
    	}
    	
    	static String searchHelper(String targetWord,
    							   String[] sArray, int low, int high){
    		String rtn;
    		if (low > high){
    			
    			rtn = null;
    		}
    		else{
    			int middle = (low + high) / 2;
    			
    			if(sArray[middle].equals(targetWord))
    				rtn =  sArray[middle];
    			else if (sArray[middle].compareTo(targetWord) < 0)
    				rtn = searchHelper(targetWord, sArray, middle+1, high);
    			else 
    				rtn = searchHelper(targetWord, sArray, low, middle - 1);
     		 
    		}	
    		return rtn;
   	   }
  }
xenop is offline   Reply With Quote
Old Sep 19th, 2006, 4:44 AM   #2
xenop
Newbie
 
Join Date: Jan 2006
Location: Lancaster, California
Posts: 11
Rep Power: 0 xenop is on a distinguished road
Send a message via AIM to xenop Send a message via Yahoo to xenop
Never mind I figured it out
b = search(input, dictionaryArray);

if (b == null ){
scram(b);
guess thats what i get for writing sloppy code
xenop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
libraries matko C 1 Jan 22nd, 2006 2:12 PM
Php Postgresql Class Pizentios Show Off Your Open Source Projects 15 Jun 28th, 2005 9:55 AM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC