-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSort.java
More file actions
121 lines (102 loc) · 2.93 KB
/
DataSort.java
File metadata and controls
121 lines (102 loc) · 2.93 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package helloWorld;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DataSort {
public static final String VALIDATION = "([a-zA-Z]:)?(\\\\[^<>:\"\\/|\\?\\*].+)+\\\\?";
public static void main(String[] args){
People[] array =new People[50];
Scanner scan=new Scanner(System.in);
String nameOfFile=getFileName(scan);
String readFromFile=null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(nameOfFile));
while((readFromFile = br.readLine()) != null){
array=createPeople(readFromFile,array);
array=sortPeople(array);
}
br.close();
bw = new BufferedWriter(new FileWriter(nameOfFile));
bw.write(combinePeople(array));
bw.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}catch (NumberFormatException e){
System.out.println("Data from file is corrupted!");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("Data from file is corrupted!");
}finally{
try {
if(br !=null)
br.close();
if(bw != null)
bw.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static String getFileName(Scanner console){
String nameOfFile;
Pattern pattern = Pattern.compile(VALIDATION);
Matcher matcher;
do{
System.out.println("Enter file absolute path");
nameOfFile = console.nextLine();
matcher = pattern.matcher(nameOfFile);
}while(matcher.matches() == false);
return nameOfFile;
}
public static People[] createPeople(String readFromFile, People[] array){
String[] splitted = readFromFile.split("\\*");
int c=0;
for(int i=0;i<splitted.length;i=i+4){
array[c]=new People(Integer.parseInt(splitted[i]), splitted[i+1]+"*"+splitted[i+2] ,Integer.parseInt(splitted[i+3]));
c++;
}
return array;
}
public static People[] sortPeople(People[] array){
int p=0;
for(int j=0;j<array.length;j++){
if(array[j]!=null)
p++;
}
int c, d;
People swap;
int n=p;
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d].getNumber() > array[d+1].getNumber())
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
return array;
}
public static String combinePeople(People[] array){
String str=new String();
int p=0;
for(int j=0;j<array.length;j++){
if(array[j]!=null)
p++;
}
for(int i=0;i<p;i++){
str=str+array[i].getNumber()+"*"+array[i].getName()+"*"+array[i].getBirthDate()+"*";
}
return str;
}
}