Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Lab10_1
- {
- public static void main(String[]args)
- {
- //load dictionary file
- String[] dict = new String[20];
- Scanner inputStream = null;
- try
- {
- inputStream = new Scanner (new File ("mydictionary11.txt"));
- }
- catch (FileNotFoundException e)
- {
- System.out.println ("Error opening the file");
- System.exit (0);
- }
- //step 1: load the words from the dictionary file into an array
- int i = 0;
- while(inputStream.hasNextLine())
- {
- dict[i] = inputStream.nextLine();
- i++;
- }
- //step 2: ask user for input then check it against dictionary
- Scanner keyboard = new Scanner(System.in);
- int elems = dict.length;
- for(;;)
- {
- System.out.println("Enter a search term");
- String input = keyboard.nextLine();
- if(input == "exit")
- break;
- else
- {
- int j;
- for(j=0; j<elems; j++)
- {
- if(dict[j] == input)
- System.out.println("'" + input + "' was found in position " + j+1);
- else if(j == elems)
- {
- System.out.println("'" + input + "' was not found");
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement