package com.example.words;
import java.io.*;
import java.util.HashSet;
public class Dictionary {
HashSet<String> hashSet = new HashSet<String>();
public Dictionary(InputStream is) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line1;
int numWords = 0;
try {
do {
line1 = br.readLine();
hashSet.add(line1);
numWords++;
} while(line1 != null);
} catch(IOException exn) {
line1 = "ERROR!";
}
}
public static void testWord(Dictionary d, String word) {
System.out.println(word + ": " + d.hashSet.contains(word));
}
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Hello.");
Dictionary d = new Dictionary(new FileInputStream("/usr/share/dict/words"));
testWord(d, "elephant");
testWord(d, "anvil");
testWord(d, "koala");
testWord(d, "aroceuhalcue");
testWord(d, "junkkkk");
testWord(d, "helllllllllllllllllllllllllo");
System.out.println(d.hashSet.size() + "words");
}
}
package com.example.words;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView helloText = (TextView) findViewById(R.id.helloText);
helloText.setText("Hello from Java.");
InputStream is = getResources().openRawResource(R.raw.words);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line1;
int numWords = 0;
try {
do {
line1 = br.readLine();
numWords++;
} while(line1 != null);
} catch(IOException exn) {
line1 = "ERROR!";
}
helloText.setText("Word count is: " + numWords);
}
}