-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
106 lines (92 loc) · 3.67 KB
/
Main.java
File metadata and controls
106 lines (92 loc) · 3.67 KB
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
package org.example;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static File createFileFromUserInput() {
File file = new File("data.txt");
try (Scanner userInput = new Scanner(System.in)) {
FileWriter writer = new FileWriter("data.txt", true);
if (file.createNewFile()) {
System.out.println("Dokumenti u krijua: " + file.getName());
System.out.println("Shkruaj cfare ka data.txt: ");
writer.write(userInput.nextLine());
writer.close();
} else {
System.out.println("Dokumenti ekziston.");
System.out.println("Shkruaj cfare do ti shtosh dokumentit data.txt: ");
writer.append("\n").append(userInput.nextLine()).append("\n");
writer.close();
}
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return file;
}
public static void readFile(File file) {
try (Scanner fileScanner = new Scanner(file)) {
System.out.println("Teksti i dokumentit data.txt: \n");
while (fileScanner.hasNextLine()) {
String text = fileScanner.nextLine();
System.out.println(text);
}
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public static void countFileLines(File file) {
try (Scanner fileScanner = new Scanner(file)) {
int lineCount = 0;
while (fileScanner.hasNextLine()) {
lineCount++;
fileScanner.nextLine();
}
System.out.println("Dokumenti ka " + lineCount + (lineCount == 1 ? " rresht." : " rreshta."));
} catch (Exception e) {
System.out.println("An error occurred.");
throw new RuntimeException(e);
}
}
public static void divider(int numeruesi, int emeruesi) {
try {
if (numeruesi == 0 || emeruesi == 0) {
throw new ArithmeticException("Error, " + (numeruesi == 0 ? "numeruesi" : "emeruesi") + " nuk mund te jete 0. ");
}
double result = (double) numeruesi / emeruesi;
System.out.println(numeruesi + " pjesetim me " + emeruesi + " eshte " + result);
} catch (ArithmeticException e) {
System.out.println("Nuk mund te pjesetosh me 0!" + e);
}
}
public static void validateNumber(int number) {
try {
if (number < 20) {
throw new IllegalArgumentException("Numri eshte me i vogel se 20!");
}
System.out.println(number + " eshte numer i vlefshem.");
} catch (IllegalArgumentException e) {
System.out.println("Numri " + number + " nuk eshte i vlefshem! " + e);
}
}
public static void main(String[] args) {
//Ushtrimi 1, 2 dhe 3
System.out.println("Krijimi, leximi dhe numerimi i rreshtave ne dokumentin data.txt:");
File file = createFileFromUserInput();
readFile(file);
countFileLines(file);
//Ushtrimi 4, 5
System.out.println("\nMetoda per pjesetimin e numrave. ");
divider(0, 0);
divider(2, 4);
System.out.println("\nMetoda per vleresimin e numrit. ");
validateNumber(10);
validateNumber(20);
//Programi FileManipulator
System.out.println("\nProgrami FileManipulator ");
FileManipulator test1 = new FileManipulator("data.txt", 10);
test1.manipulate();
}
}