@TheRicks2/

Autocomplete

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
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>();
    String in = "", choice = "", fileName = "words.txt", line;
    File file = new File(fileName);
    boolean on = true;

    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();

      choices.clear();
      for(int i = 0; i < words.size(); i++)
      {
        if(words.get(i).startsWith(in) && choices.size() < 5)
        {
          choices.add(words.get(i));
        }
      }
      console.printf(choices + "\n");
      
      choice = console.readLine("Choose:\n").toLowerCase();
      if(words.contains(choice))
      {
        if(words.indexOf(choice) != 0)
        {
          Collections.swap(words, words.indexOf(choice) - 1, words.indexOf(choice));
        }
      }
      else
      {
        words.add(choice);
      }

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

        case "n":
          on = false;
          break;

        default:
          on = true;
          break;
      }
    }

    try
    {
      FileWriter fileWriter = new FileWriter(fileName);

      BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

      for(int i = 0; i < words.size(); i++)
      {
        bufferedWriter.write(words.get(i));
        bufferedWriter.newLine();
      }

      bufferedWriter.close();
    }
    catch(FileNotFoundException ex) 
		{
      ex.printStackTrace(); 
    }
    catch(IOException ex) 
		{
      ex.printStackTrace();
    }
  }
}