Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class Lab10_1
  4. {
  5.   public static void main(String[]args)
  6.   {
  7.     //load dictionary file
  8.     String[] dict = new String[20];
  9.     Scanner inputStream = null;
  10.     try
  11.       {
  12.         inputStream = new Scanner (new File ("mydictionary11.txt"));
  13.       }
  14.       catch (FileNotFoundException e)
  15.       {
  16.         System.out.println ("Error opening the file");
  17.         System.exit (0);
  18.       }
  19.      
  20.       //step 1: load the words from the dictionary file into an array
  21.       int i = 0;
  22.       while(inputStream.hasNextLine())
  23.       {
  24.         dict[i] = inputStream.nextLine();
  25.         i++;
  26.       }
  27.      
  28.       //step 2: ask user for input then check it against dictionary
  29.       Scanner keyboard = new Scanner(System.in);
  30.       int elems = dict.length;
  31.       for(;;)
  32.       {
  33.         System.out.println("Enter a search term");
  34.         String input = keyboard.nextLine();
  35.         if(input == "exit")
  36.           break;
  37.         else
  38.         {
  39.           int j;
  40.           for(j=0; j<elems; j++)    
  41.           {
  42.             if(dict[j] == input)    
  43.               System.out.println("'" + input + "' was found in position " + j+1);
  44.             else if(j == elems)
  45.             {
  46.               System.out.println("'" + input + "' was not found");
  47.               break;
  48.             }
  49.           }  
  50.         }
  51.       }
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement