-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task2.java
160 lines (134 loc) · 4.6 KB
/
Task2.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// WORD COUNTER
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class Task2 {
// Global
static Scanner scanner = new Scanner(System.in);
static File myFile = new File("text.txt");
//Message for user to use in convenient way
static void msg() {
System.out.println("\n\t WORD COUNTER");
System.out.println("Which text do you want to count : ");
System.out.println("1. System Input(Press 1)");
System.out.println("2. Text file(Press 2)\n");
System.out.print("Operation : ");
}
static void msg1() {
System.out.println("\n1. Write in text file");
System.out.println("2. Read text file");
System.out.print("\nSelect : ");
}
// Operation Select
static int Operation() {
int op = scanner.nextInt();
if (op == 1) {
return 1;
} else if (op == 2) {
return 2;
} else {
System.out.println("\nEnter Correct Value");
System.out.println("Try again");
}
return -1;
}
// Operation 1 : Read user Input's and Count Word
static int wordCounter(String text) {
int count = 0;
for(int i=0; i<text.length(); i++) {
if(text.charAt(i) == ' ') {
count++;
}
}
if(text.isEmpty()) {
return 0;
} else if (count >= 1) {
return count+1;
} else if (text.charAt(0) != ' ') {
return 1;
}
return -1;
}
// Operation 2
// 2.1 : File checking
static void file() {
try {
if(myFile.createNewFile()) {
System.out.println("\nFile created Successfully ");
}
} catch (IOException e) {
System.out.println("Error Occur : During File Creation");
e.printStackTrace();
}
}
// 2.2 : Write in file
static void writeFile() {
Scanner sc = new Scanner(System.in);
try {
FileWriter myFiles = new FileWriter(myFile);
String str = sc.nextLine();
myFiles.write(str);
myFiles.close();
char ask = 'n';
System.out.print("Want to ask Word(y/n) : ");
ask = sc.next().charAt(0);
if(ask == 'y') {
ReadFile();
}
sc.close();
} catch (IOException e) {
System.out.println("Error occur : Writing File");
e.printStackTrace();
}
}
// 2.3 : Count Word of external text file
static void ReadFile() {
try {
int line = 0, fResult = 0;
Scanner fileReader = new Scanner(myFile);
while(fileReader.hasNextLine()) {
String text = fileReader.nextLine();
line++;
int temp = 0;
do {
temp = wordCounter(text);
fResult += temp;
} while (wordCounter(text) == 0);
}
System.out.println("Lines : "+line);
System.out.println("Words : "+fResult);
fileReader.close();
} catch(FileNotFoundException e) {
System.out.println("Error Occur : Read File ");
e.printStackTrace();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Message
msg();
// Operation
int ops = Operation();
if(ops == 1) {
// For Better Look
System.out.println();
String text = scanner.nextLine();
System.out.println("Word : "+wordCounter(text));
} else if(ops == 2) {
// Creation of file
file();
// Message 1 function
msg1();
int op = scanner.nextInt();
System.out.println();
switch (op) {
case 1 -> writeFile();
case 2 -> ReadFile();
default -> System.out.println("Choose Correct option as mentioned\nTry again");
}
}
scanner.close();
}
}