-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathConsecutiveOne.java
More file actions
85 lines (61 loc) · 2 KB
/
Copy pathConsecutiveOne.java
File metadata and controls
85 lines (61 loc) · 2 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
import java.util.Scanner;
public class ConsecutiveOne {
static int counter = 0;
static void acceptConsecutiveOne() {
Scanner in = new Scanner(System.in);
System.out.println("Welcome In the Finite State Machine !!!");
System.out.println("Enter the Binary String : ");
String input = in.next();
boolean validInfo = checkValidString(input);
boolean ans = false;
if (validInfo) {
ans = working(input);
if(ans){
System.out.println("\n\n\n Congratulations , Your String is Accepted $$$ \n\n\n");
}
else{
System.out.println("Your String is Rejected !!!!");
}
} else {
counter++;
if (counter <= 1) {
System.out.println("\n\n\nPlease Enter the Only Binary String ....");
acceptConsecutiveOne();
}
else if (counter <= 3) {
System.out.println("\n\n\nDhayan se Binary String Dal...\n\n\n");
acceptConsecutiveOne();
} else {
System.out.println("Exiting the Program YOU ARE NOT UNDERSTAND AUTOMATA LANGUAGE ..//");
}
}
}
static boolean working(String s) {
int i = 0, l = s.length();
while (i < l) {
if (s.charAt(i) == '1') {
i++;
if (i < l && s.charAt(i) == '1') {
i++;
if (i < l && s.charAt(i) == '1') {
return true;
}
}
}
i++;
}
return false;
}
static boolean checkValidString(String s) {
boolean flag = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '0' && s.charAt(i) != '1') {
return false;
}
}
return flag;
}
public static void main(String[] args) {
acceptConsecutiveOne();
}
}