Guest User

Java Array Issue

a guest
Nov 6th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. package ParityBitter;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. import java.io.IOException;
  5.  
  6. public class BitterV2 {
  7.  
  8.     @SuppressWarnings("empty-statement")
  9.     public static void main(String[] args) throws IOException{
  10.         Scanner in = new Scanner(System.in);
  11.         int[] bitInput = new int[256];
  12.         int check;
  13.         int bitLength = 0;
  14.         Boolean overMaximum = false;
  15.         int currentPosition, originalValue, parityCount, bitCount, remainingValue, currentParity = 1, movementValue = 1;
  16.         boolean moveOn = false;
  17.        
  18.        
  19.         System.out.println("Would you like (a)utomatic input or (m)anual input?");
  20.         //Automatic Input
  21.         //Reading in text input
  22.         String file_name = "E:/University/Term 1/CaCT/Week 5/CorrectParityChecker/src/ParityBitter/bitStreams.txt";
  23.         String[] txtrfInput = new String[10];
  24.         try {
  25.             ReadFile file = new ReadFile(file_name);
  26.             String[] txtInput = file.OpenFile();
  27.             txtrfInput = txtInput;
  28.         }
  29.         catch (IOException e){
  30.             System.out.println("Error. The file doesn't exist :( or I've messed up the code.");
  31.         }
  32.        
  33.         for (int x = 0; x < txtrfInput[0].length(); x++){
  34.         bitInput[x] = txtrfInput[0].charAt(x);
  35.         bitLength = txtrfInput[0].length();
  36.         System.out.println("bI: " + bitInput[x]);
  37.         System.out.println("tI: " + txtrfInput[0].charAt(x));
  38.         }
  39.        
  40.         //Manual Input
  41.         //Check length
  42. //        System.out.println("Please enter the length of the bit-stream");
  43. //        bitLength = Integer.parseInt(in.nextLine());
  44. //        
  45. //        for (int x = bitLength-1; x >= 0; x--){
  46. //            System.out.println("Enter bit value " + (bitLength - x));
  47. //            bitInput[x] = Integer.parseInt(in.nextLine());
  48. //        }
  49.        
  50.         //Working out paritys
  51.        
  52.         //1. check number of paritys to check (multiply 2 until over number?)
  53.         originalValue = 1;
  54.         parityCount = 0;
  55.         bitInput[originalValue-1] = 3;
  56.         do{
  57.             if (originalValue <= bitLength){
  58.                 originalValue *= 2;
  59.                 //Remove Parity Bits for Checking
  60.                 bitInput[originalValue-1] = 3;
  61.                 parityCount++;
  62.             }
  63.             else{
  64.                 overMaximum = true;
  65.             }
  66.         }while (overMaximum == false);
  67.         //Create intArray for parity types
  68.         int[] parityType = new int[parityCount];
  69.         //Reset
  70.         overMaximum = false;
  71.        
  72.         currentPosition = currentParity;
  73.         //2. x = 0, x < length, x += amount skipped (same as parity count?)
  74.         //3. create virtual rule depending on current parity being checked
  75.         for (int x = 0; x < parityCount; x++){ //for each parity
  76.             //reset values
  77.             bitCount = 0;
  78.             moveOn = false;
  79.             //Set movement
  80.             movementValue = currentParity;
  81.             //loop until exit required
  82.             do{
  83.                 //if array value+val > op
  84.                 //System.out.println("X:" + x);
  85.                 //System.out.println("MV:" + movementValue);
  86.                 if ((currentPosition + movementValue) > bitLength){
  87.                     //System.out.println("cP:" + currentPosition);
  88.                     //calculate difference and use that for remaining
  89.                     remainingValue = (bitLength - currentPosition);
  90.                     //System.out.println("rV:" + remainingValue);
  91.                     if (remainingValue >= 0){
  92.                         //System.out.println("rV2");
  93.                         for (int i = remainingValue; i >= 0; i--){
  94.                             //check + add to count
  95.                             if (bitInput[currentPosition -1 + i] == 1){
  96.                                 bitCount++;
  97.  
  98.                             }
  99.                             //System.out.println("rV3");
  100.                         }
  101.                     }
  102.                     moveOn = true;
  103.                 }
  104.  
  105.                 else{
  106.                     for (int i = 0; i < movementValue; i++){
  107.                         //check + add to count
  108.                         //System.out.println("bI:" + bitInput[currentPosition + i - 1]);
  109.                         if (bitInput[currentPosition + i - 1] == 1){
  110.                             bitCount++;
  111.                         }
  112.                     }
  113.                 }
  114.                 //next check position
  115.                 currentPosition += (movementValue + movementValue);
  116.             }while (moveOn == false);
  117.  
  118.             //System.out.println("BIT: " + bitCount);
  119.             //check for even or odd parity
  120.             if (bitCount%2 == 0){
  121.                 parityType[x] = 0;
  122.                 //System.out.println("Even");
  123.             }
  124.             else{
  125.                 parityType[x] = 1;
  126.                 //System.out.println("Odd");
  127.             }
  128.  
  129.             //Increase parity to next
  130.             currentParity *= 2;
  131.             currentPosition = currentParity;
  132.             //System.out.println("cP:" + currentPosition);
  133.         }
  134.        
  135.         //Placing corrected Paritys
  136.         for(int x = 1, y = 0; x < bitLength; x*=2){
  137.             bitInput[x-1] = parityType[y];
  138.             y++;
  139.         }
  140.         //Display time :D    
  141.         for(int x = bitLength-1; x >= 0; x--){
  142.             System.out.print(bitInput[x]);
  143.         }
  144.        
  145.        
  146.         //When displaying, display each character but place in parity bits
  147.        // System.out.println(parityCount);
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment