Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class WordLadder {
  5.  
  6.     private String lastWord = "";
  7.  
  8.     private Lexicom lexicom = new Lexicom("english.dat");
  9.  
  10.     private boolean running = true;
  11.  
  12.     public WordLadder() {
  13.         Scanner scanner = new Scanner(System.in);
  14.         while (running) {
  15.             checkWord(scanner.readLine());
  16.         }
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         new WordLadder():  
  21.     }
  22.  
  23.     private void checkWord(String word) {
  24.         if (word.length() == 0) {
  25.             System.out.println();
  26.             running = false;
  27.             return;    
  28.         }
  29.         System.out.println(word);
  30.         if ((lastWord.length() != 0 && word.length() != lastWord.length()) || !lexicom.isEnglishWord(word)) {
  31.             System.out.println("Invalid word");
  32.             return;
  33.         }
  34.         int occurrences = 0;
  35.         char[] currWord = word.toCharArray();
  36.         char[] lastWord = this.lastWord.toCharArray();
  37.         for (int i = 0; i < currWord.length; i++) {
  38.             for (int j = 0; j < lastWord.length; j++) {
  39.                 if (currWord[i] == lastWord[i]) {
  40.                     occurrences++;             
  41.                 }          
  42.             }      
  43.         }
  44.         if (occurrences > 1) {
  45.             System.out.println("Invalid word");    
  46.         } else {
  47.             lastWord = word;       
  48.         }
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement