@TheRicks2/

Autocorrect

Java

No description

fork
loading
Files
  • Main.java
  • words.txt
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.*;
import java.util.*;

class Main {
  public static void main(String[] args) {
    Console console = System.console();
    List<String> words = new ArrayList<String>();
    List<String> choices = new ArrayList<String>();
    List<String> inSplit = new ArrayList<String>();
    String in = "", fileName = "words.txt", line;
    File file = new File(fileName);
    boolean on = true;
    int x = 0;

    try
		{
      FileReader fileReader = new FileReader(fileName);

      BufferedReader bufferedReader = new BufferedReader(fileReader);

      Scanner scanner = new Scanner(file);

      while(scanner.hasNextLine())
      {
        line = scanner.nextLine();
        words.add(line);
      }
          
      bufferedReader.close();
    }
    catch(FileNotFoundException ex) 
		{
      ex.printStackTrace(); 
    }
    catch(IOException ex) 
		{
      ex.printStackTrace();
    }
    
    while(on)
    {
      in = console.readLine("Input:\n").toLowerCase();

      inSplit.clear();
      for(int i = 0; i < in.length(); i++)
      {
        inSplit.add(in.substring(i, i + 1));
      }
      //console.printf(inSplit + "\n");

      choices.clear();
      for(int i = 0; i < words.size(); i++)
      {
        x = 0;
        for(int m = 0; m < words.get(i).length(); m++)
        {
          for(int n = 0; n < inSplit.size(); n++)
          {
            if(words.get(i).substring(m, m + 1).equals(inSplit.get(n)))
            {
              x++;
            }
          }
        }
        
        if(x >= (inSplit.size() * 0.6))
        {
          choices.add(words.get(i));
        }
      }

      for(int i = choices.size() - 1; i >= 0; i--)
      {
        if(!choices.get(i).startsWith(inSplit.get(0)))
        {
          choices.remove(i);
        }
      }

      for(int i = choices.size() - 1; i >= 0; i--)
      {
        if(choices.get(i).length() > inSplit.size())
        {
          choices.remove(i);
        }
      }
      console.printf(choices + "\n");

      switch(console.readLine("Again?(y/n)").toLowerCase())
      {
        case "y":
          on = true;
          break;

        case "n":
          on = false;
          break;

        default:
          on = true;
          break;
      }
    }
  }
}