-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPAddressBackEnd.java
288 lines (268 loc) · 6.59 KB
/
IPAddressBackEnd.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.util.Scanner;
public class IPAddressBackEnd
{
static String IPv4var =""; //IPv4var ="";error=0;int f1,f2,f3,f4=0;type="";snm="";j=0,max=12;
static int error=0;
static String fill1="";
static String fill2="";
static String fill3="";
static String fill4="";
static int f1,f2,f3,f4;
static String type="";
static String snm="";
static int j=0,max=12;
static boolean notNumber=false;
static String arr[]=new String[max];
static String IPv4;
public static void main(String arg [])
{
System.out.println("This is the checker");
checker(IPv4var);
defaultInitializer();
}
static void checker(String IPv4var)
{
//Scanner sc=new Scanner(System.in);
System.out.println("IP address checker");
//System.out.println("Enter the IPv4:");
//String IPv4=sc.next();
IPv4=IPv4var; //alteration requirement
System.out.println("U entered :"+IPv4);
int len=IPv4.length();
numberChecker(IPv4); //calling the functions
if(notNumber==false)
{
dotChecker(IPv4);
ipDivider(IPv4);
emptyFillChecker();
startNumberZero();
addressGenerator();
if(fill1!="" && fill2!="" && fill3!="" && fill4!="")
rangeChecker();
if(fill1!="")
ipClassType();
output();
}
}
//-----------------------------------------------------------------------------------
public static void numberChecker(String IPv4) //numberChecker
{
int a=error;
for(int i=0; i<IPv4.length();i++)
{
if(IPv4.charAt(i)=='.'|| IPv4.charAt(i)>='0' && IPv4.charAt(i)<='9')
{} // or just ; can be used
else
{
System.out.println("Error:number not present at the address fill");
notNumber=true;
error++;
arr[j]="Non-numeric charecter found:correct this to proceed!";
j++;
break;
}
}
if(a==error)
System.out.println("Number checker passed !");
}
public static void dotChecker(String IPv4) //Dot Checker
{
int dotC=0;
for(int i=0; i<IPv4.length();i++)
{
if(IPv4.charAt(i)=='.')
dotC++;
}
if(dotC!=3)
{
System.out.println("Less/More no. of host/network addresses");
error++;
arr[j]="Less/More no. of host/network addresses (more/less than 3 dots)";
j++;
}
else
System.out.println("Dot count filter passed !");
}
public static void ipDivider(String IPv4) //ipDivider : generates fills in "String form"
{
int dotC=0;
for(int i=0; i<IPv4.length();i++)
{
if(IPv4.charAt(i)!='.')
{
if(dotC==0)
fill1=fill1+IPv4.charAt(i);
else if(dotC==1)
fill2=fill2+IPv4.charAt(i);
else if(dotC==2)
fill3=fill3+IPv4.charAt(i);
else if(dotC==3)
fill4=fill4+IPv4.charAt(i);
}
else
dotC++;
}
System.out.println("FILLS ARE :-");
System.out.println("fill1="+fill1);
System.out.println("fill2="+fill2);
System.out.println("fill3="+fill3);
System.out.println("fill4="+fill4);
}
//emptyFillChecker
public static void emptyFillChecker()
{
if(fill1==null||fill2==null||fill3==null||fill4==null)
{
System.out.println("Empty fill found");
error++;
arr[j]="Empty fill found";
j++;
}
else
System.out.println("Empty fill filter passed !");
}
//statrNumberZero
public static void startNumberZero()
{
if((fill1.length()!=0 && fill1.charAt(0)=='0'&&fill1.length()>1) || ( fill2.length()!=0 &&fill2.charAt(0)=='0'&&fill2.length()>1 ) ||( fill3.length()!=0 &&fill3.charAt(0)=='0'&&fill3.length()>1 ) || ( fill4.length()!=0 && fill4.charAt(0)=='0'&&fill4.length()>1))
{
System.out.println("0 at first position found of one of the fills");
error++;
arr[j]="0 as MSD in 2/3 digit number";
j++;
}
else
System.out.println("0@first filter passed !");
}
public static void addressGenerator() //addressGenerator
{
if( fill1.length()!=0)
f1=Integer.parseInt(fill1);
else
{
System.out.println("1st fill is empty");
f1=-1;
error++;
arr[j]="1st fill is empty";
j++;
}
if( fill2.length()!=0)
f2=Integer.parseInt(fill2);
else
{
System.out.println("2nd fill is empty");
f2=-1;
error++;
arr[j]="2nd fill is empty";
j++;
}
if( fill3.length()!=0)
f3=Integer.parseInt(fill3);
else
{
System.out.println("3rd fill is empty");
f3=-1;
error++;
arr[j]="3rd fill is empty";
j++;
}
if( fill4.length()!=0)
f4=Integer.parseInt(fill4);
else
{
System.out.println("4th fill is empty");
f4=-1;
error++;
arr[j]="4th fill is empty";
j++;
}
}
public static void rangeChecker() //rangeChecker
{
if((0<=f2 && f2<=255) && (0<=f3 && f3<=255) && (0<=f4 && f4<=255))
{
System.out.println("fill 2,3,4 filter passed !");
}
else
{
System.out.println("Invalid range for fill 2/3/4");
error++;
arr[j]="Invalid range for fill 2/3/4";
j++;
}
}
public static void ipClassType() //ipClassType
{
if(f1>=0 && f1<=127)
{
type="Class A";
snm="255.0.0.0";
}
else if(f1>=128 && f1<=191)
{
type="Class B";
snm="255.255.0.0";
}
else if(f1>=192 && f1<=223)
{
type="Class C";
snm="255.255.255.0";
}
else if(f1>=224 && f1<=239)
{
type="Class D";
}
else if(f1>=240 && f1<=255)
{
type="Class E";
}
else
{
System.out.println("fill1 number greater than the range");
error++;
arr[j]="Fill 1's number greater than the range";
j++;
}
}
static void output() //output
{
//Printing the result
System.out.println("\nNumber of errors:"+error+"\n");
System.out.println("---------------------------------------------\n");
System.out.println("\t RESULT :");
if(error>0)
{
System.out.println("\nErrors in IP:\n");
max=j;
for(int i=0;i<max;i++)
{
System.out.println((i+1)+"."+arr[i]);
}
}
else
{
System.out.println("\nIP address "+IPv4+" is valid");
System.out.println("Class :"+type);
if(snm!="")
System.out.println("Subnet Mask :"+snm);
}
System.out.println("j="+j);
System.out.println("---------------------------------------------");
}
static void defaultInitializer()
{
fill1="";
fill2="";
fill3="";
fill4="";
type="";
snm="";
IPv4var ="";
error=0;
notNumber=false;
int f1,f2,f3,f4=0;
j=0;
max=12;
IPv4var ="";
}
}