Error Detection Using Parity Bit


Error Detection Using Parity Bit import java.util.*;
class Parity
{
public static String count(String x)
{
int j=0;
for(int i=0;i<x.length();i++)
{
if(x.charAt(i)=='1')
j++;
}
StringBuffer s=new StringBuffer(x);
if(j%2==0)
{

s.append('0');
}
else
s.append('1');
System.out.println("parity: "+j);
return s.toString();
}

}
class Check
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
System.out.println("enter the string");
String s1=src.nextLine();
StringBuffer sb1=new StringBuffer(s1);
Random rb=new Random();
String s2="";
StringBuffer sb2=new StringBuffer(s2);
for(int i=0;i<s1.length();i++)
{
if(rb.nextBoolean()==true)
sb2.append('1');
else
sb2.append('0');
}
System.out.println(" random number without parity"+sb2);
String a;
a=Parity.count(sb1.toString());
String b;
b=Parity.count(sb2.toString());
System.out.println("Numbers with parity:");
System.out.println(a+"\n"+b);
char ch1=a.charAt(a.length()-1);
char ch2=b.charAt(b.length()-1);
if(ch1==ch2)
{
System.out.println("No error");
}
else
System.out.println("ERROR");
}
}

0 comments :