Volodya wrote:
> Michael Rogers wrote:
>> Thanks! Here's the Java version of the poem generator (same dictionaries
>> as before).
> 
>> Cheers,
>> Michael
> 
> Have you tried to give it enough words to represent a full key? I have a 
> feeling that what might be needed to
> not to load the whole file, but rather use it sax style. So you know that you 
> need 1047th line, and that is the
> only one you actually store in the memory. I'll see if i can improve on your 
> code a bit. I really loved it. q;-)
> 
>                  - Volodya

This is basically the same thing but it doesn't load anything in the memory 
that doesn't need to be there. Do
with it as you will.

                    - Volodya

-- 
http://freedom.libsyn.com/       Voice of Freedom, Radical Podcast
http://eng.anarchopedia.org/     Anarchopedia, A Free Knowledge Portal

 "None of us are free until all of us are free."    ~ Mihail Bakunin
import java.io.BufferedReader;
import java.io.FileReader;

import java.io.FileNotFoundException;
import java.io.IOException;

public class PoemKey {
	public static int WORDS_PER_KEY = 4;
	public static int BYTES_PER_WORD = 2;
	
	private static String[][] words;
	
	static
	{
		words = new String[WORDS_PER_KEY][];
		final int numOfWords = (int)Math.pow(2, BYTES_PER_WORD * 8);
		
		for(int i=0; i<WORDS_PER_KEY; i++)
		{
			words[i] = new String[numOfWords];
			try
			{
				BufferedReader fin = new BufferedReader(new FileReader("words-"+i+".dic"));
				for(int j=0; j < numOfWords; j++)
				{
					words[i][j] = fin.readLine();
				}
				fin.close();
			}
			catch(FileNotFoundException e)
			{
				//Well i don't really know what you'd want to do then.
			}
			catch(IOException e)
			{
				//Well i don't really know what you'd want to do then.
			}
		}
		
	}
	
	public static int[] poemToKey(String poem[])
	{
		int result[] = new int[WORDS_PER_KEY];
		
		return result;
	}
	
	public static String[] keyToPoem(int[] key)
	{
		String result[] = new String[WORDS_PER_KEY];
		
		for(int i=0; i<WORDS_PER_KEY; i++)
		{
			result[i] = words[i][key[i]];
		}
		
		return result;
	}
	
	public static void main(String args[])
	{
		int key[] = new int[WORDS_PER_KEY];
		
		System.out.print("The random key with "+WORDS_PER_KEY+" blocks "+BYTES_PER_WORD+" bytes each is: ");
		
		for(int i=0; i<WORDS_PER_KEY; i++)
		{
			key[i]=(int)(Math.random()*8*BYTES_PER_WORD);
			System.out.print(Integer.toHexString(key[i])+" ");
		}
		
		System.out.println("");
		
		System.out.print("The 'Poem' key is: ");
		String[] poem = keyToPoem(key);
		for(int i=0; i<WORDS_PER_KEY; i++)
		{
			System.out.print(poem[i]+" ");
		}
		System.out.println("");
	}
}
_______________________________________________
Devl mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/devl

Reply via email to