import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

// This class uses Google to define words.
public class GoogleDefine {
	public static void main(String[] args) {
		System.out.println("Enter a term to define:");
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		String word = null;
		try {
			word = in.readLine();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String urlStr = "http://www.google.com/search?hl=en&q=define%3A" + word + "&btnG=Search";
		
		try {
			URL url = new URL(urlStr);
			URLConnection conn = url.openConnection();
			conn.setRequestProperty("User-agent", "my agent name");
			
			String content = "";
			BufferedInputStream pageIn = new BufferedInputStream(conn.getInputStream());
			byte[] buffer = new byte[5000];
			while (true) {
				int length = pageIn.read(buffer);
				if (length == -1) {
					break;
				}
				content += new String(buffer, 0, length);
			}
			pageIn.close();
			
			int index = content.indexOf("<li>");
			
			int count = 1;
			while (index != -1) {
				int endIndex = content.indexOf("<", index + 1);
				if (endIndex == -1) {
					endIndex = content.length();
				}
				String def = content.substring(index + 4, endIndex).trim();
				System.out.println(count + ": " + def + "\n");
				index = content.indexOf("<li>", endIndex);
				count++;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

