ERROR CHECKING BY PARITY BIT


import java.util.*;
public class Parity_bit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the data bit");
String abc = sc.nextLine();
int len = abc.length();
int count1 = 0;
for (int i = 0; i < len; i++) {
if (abc.charAt(i) == '1') {
count1++;
}
}
System.out.println("No of 1 in string is " + count1);
if (count1 % 2 == 0) {
System.out.println("No of 1 is even ");
}
if (count1 % 2 != 0) {
System.out.println("No of 1 is odd ");
}
System.out.println("Enter the parity bit");
String new_bit = sc.nextLine();
String bitnew = abc + new_bit;
System.out.println("The binary data after parity bit addition is " + bitnew);
int rn = 0 + (int) (Math.random() * (len - 0) + 1);
System.out.println("random position is " + rn);
String rn_pos_chg = bitnew.substring(0, rn - 1) + "1" + bitnew.substring(rn);
System.out.println("The random bit is genrated data is:" + rn_pos_chg);
int count = 0;
for (int i = 0; i < len + 1; i++) {
if (rn_pos_chg.charAt(i) == '1') {
count++;
}
}
System.out.println("No of 1 in string is " + count);
if (count % 2 == 0) {
System.out.println("No if 1 is even thus it does not contain error");
}
if (count % 2 != 0) {
System.out.println("No if 1 is odd so it contain error");
}
}
}

No comments:

Post a Comment