Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ParityBitter;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.io.IOException;
- public class BitterV2 {
- @SuppressWarnings("empty-statement")
- public static void main(String[] args) throws IOException{
- Scanner in = new Scanner(System.in);
- int[] bitInput = new int[256];
- int check;
- int bitLength = 0;
- Boolean overMaximum = false;
- int currentPosition, originalValue, parityCount, bitCount, remainingValue, currentParity = 1, movementValue = 1;
- boolean moveOn = false;
- System.out.println("Would you like (a)utomatic input or (m)anual input?");
- //Automatic Input
- //Reading in text input
- String file_name = "E:/University/Term 1/CaCT/Week 5/CorrectParityChecker/src/ParityBitter/bitStreams.txt";
- String[] txtrfInput = new String[10];
- try {
- ReadFile file = new ReadFile(file_name);
- String[] txtInput = file.OpenFile();
- txtrfInput = txtInput;
- }
- catch (IOException e){
- System.out.println("Error. The file doesn't exist :( or I've messed up the code.");
- }
- for (int x = 0; x < txtrfInput[0].length(); x++){
- bitInput[x] = txtrfInput[0].charAt(x);
- bitLength = txtrfInput[0].length();
- System.out.println("bI: " + bitInput[x]);
- System.out.println("tI: " + txtrfInput[0].charAt(x));
- }
- //Manual Input
- //Check length
- // System.out.println("Please enter the length of the bit-stream");
- // bitLength = Integer.parseInt(in.nextLine());
- //
- // for (int x = bitLength-1; x >= 0; x--){
- // System.out.println("Enter bit value " + (bitLength - x));
- // bitInput[x] = Integer.parseInt(in.nextLine());
- // }
- //Working out paritys
- //1. check number of paritys to check (multiply 2 until over number?)
- originalValue = 1;
- parityCount = 0;
- bitInput[originalValue-1] = 3;
- do{
- if (originalValue <= bitLength){
- originalValue *= 2;
- //Remove Parity Bits for Checking
- bitInput[originalValue-1] = 3;
- parityCount++;
- }
- else{
- overMaximum = true;
- }
- }while (overMaximum == false);
- //Create intArray for parity types
- int[] parityType = new int[parityCount];
- //Reset
- overMaximum = false;
- currentPosition = currentParity;
- //2. x = 0, x < length, x += amount skipped (same as parity count?)
- //3. create virtual rule depending on current parity being checked
- for (int x = 0; x < parityCount; x++){ //for each parity
- //reset values
- bitCount = 0;
- moveOn = false;
- //Set movement
- movementValue = currentParity;
- //loop until exit required
- do{
- //if array value+val > op
- //System.out.println("X:" + x);
- //System.out.println("MV:" + movementValue);
- if ((currentPosition + movementValue) > bitLength){
- //System.out.println("cP:" + currentPosition);
- //calculate difference and use that for remaining
- remainingValue = (bitLength - currentPosition);
- //System.out.println("rV:" + remainingValue);
- if (remainingValue >= 0){
- //System.out.println("rV2");
- for (int i = remainingValue; i >= 0; i--){
- //check + add to count
- if (bitInput[currentPosition -1 + i] == 1){
- bitCount++;
- }
- //System.out.println("rV3");
- }
- }
- moveOn = true;
- }
- else{
- for (int i = 0; i < movementValue; i++){
- //check + add to count
- //System.out.println("bI:" + bitInput[currentPosition + i - 1]);
- if (bitInput[currentPosition + i - 1] == 1){
- bitCount++;
- }
- }
- }
- //next check position
- currentPosition += (movementValue + movementValue);
- }while (moveOn == false);
- //System.out.println("BIT: " + bitCount);
- //check for even or odd parity
- if (bitCount%2 == 0){
- parityType[x] = 0;
- //System.out.println("Even");
- }
- else{
- parityType[x] = 1;
- //System.out.println("Odd");
- }
- //Increase parity to next
- currentParity *= 2;
- currentPosition = currentParity;
- //System.out.println("cP:" + currentPosition);
- }
- //Placing corrected Paritys
- for(int x = 1, y = 0; x < bitLength; x*=2){
- bitInput[x-1] = parityType[y];
- y++;
- }
- //Display time :D
- for(int x = bitLength-1; x >= 0; x--){
- System.out.print(bitInput[x]);
- }
- //When displaying, display each character but place in parity bits
- // System.out.println(parityCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment