javaswingjtextfieldhamming-code

How to stop list from converting mix of 1s and 0s into all 0s?


I am creating a program to take in 1 or more 8 bit binary sequences and convert them into 12 bit hamming binary sequences. The code works and achieves everything right up until it tries to put the information into the JTextField at which point it converts the 8-bit stream into 1111 1111 and converts the 12 bit stream into 0000 0000 0000 (spacing for easier reading). I have tried stepping through the code and at every point it thinks the list is correct, right until i turn it into a string. Im not sure if im incorrectly using something but hopefully someone can help.

As an example for those who dont understand hamming codes. If you put in "10101010" no spaces, the system should spit out "Your original bit-stream was: 1010 1010 Your new Hamming bit-stream is: 1010 0101 1000"

But instead it will say "Your original bit-stream was: 1111 1111 Your new Hamming bit-stream is: 0000 0000 0000"

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

@SuppressWarnings("serial")
public class MainProgram extends JPanel {
private static final String INITIAL_TITLE = "Please enter your next 8 bits. "
        + "Do not enter more than 8 bits.\n"
        + "Press Enter when done";
private static final String ARE_YOU_FINISHED = "Are you finished entering streams?";
private static final String CALCULATING = "Just one moment while the code is generated...";
private static final String YES = "YES";
private static final String ENTER = "ENTER";
private static final String NO = "NO";
private static final String CONTINUE = "CONTINUE";
private static int GAP = 12;
private static final int COLUMNS = 35;
private static  int TEMP_STREAM = 0;
static int numberOfStreams = 0;
static int counter = 0;
static String OriBuild;
static String NewBuild;

// this is a JTextArea built to look like a JLabel
private JTextArea topTextArea = new JTextArea(2, COLUMNS);
private JTextField dataEntryField = new JTextField(COLUMNS);
private JTextArea dataPrinter = new JTextArea(2,2);
private JButton yesEnterButton = new JButton(ENTER);
private JButton noButton = new JButton(NO);
private JButton contButton = new JButton(CONTINUE);

private boolean enteringData = true;
private boolean dataValidYet = false;

java.util.List<Integer> streamSplit = new ArrayList<>();
java.util.List<Integer> tempEight = new ArrayList<>();
java.util.List<Integer> finalStream = new ArrayList<>();

public MainProgram(){
    yesEnterButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            yesEnterButtonActionPerfromed(e);
        }
    });

    noButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            noButtonActionPerfromed(e);
        }
    });

    contButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            contButtonActionPerfromed(e);
        }
    });

    topTextArea.setWrapStyleWord(true);
    topTextArea.setLineWrap(true);
    topTextArea.setFocusable(false);
    topTextArea.setEditable(false);
    topTextArea.setOpaque(false);
    topTextArea.setText(INITIAL_TITLE);

    JPanel innerButtonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
    innerButtonPanel.add(yesEnterButton);
    innerButtonPanel.add(contButton);
    innerButtonPanel.add(noButton);
    contButton.setVisible(false);
    noButton.setVisible(false);
    JPanel outerButtonPanel = new JPanel();
    outerButtonPanel.add(innerButtonPanel);

    setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
    setLayout(new BorderLayout(GAP, GAP));
    add(topTextArea, BorderLayout.PAGE_START);
    add(dataEntryField, BorderLayout.LINE_END);
    dataPrinter.setVisible(false);
    add(dataPrinter, BorderLayout.LINE_START);
    add(outerButtonPanel, BorderLayout.PAGE_END);

}

protected void noButtonActionPerfromed(ActionEvent e) {
    if(!dataValidYet){
        enteringData = true;
        topTextArea.setText(INITIAL_TITLE);
        noButton.setVisible(false);
        dataEntryField.setVisible(true);
        return;
    }
    // Pressing no allows more entry
}

private void yesEnterButtonActionPerfromed(ActionEvent e) {
    if (enteringData) {
        topTextArea.setText(ARE_YOU_FINISHED);
        yesEnterButton.setText(YES);
        yesEnterButton.setActionCommand(YES);
        noButton.setVisible(true);
        TEMP_STREAM = checkInput();
        enteringData = false;
        dataEntryField.setText("");
        dataEntryField.setVisible(false);
        streamAdd();//This function adds the stream (example: 10101010) to a list creating individual digits of 1,0,1,0,1,0,1,0
        return;
    }//pressing enter takes the value in box. Pressing yes causes the system to move on
    else{
        dataValidYet = true;
        yesEnterButton.setVisible(false);
        noButton.setVisible(false);
        logicLaunch();//converts the 8 digit binary into a 12 digit hamming code
        contButton.setVisible(true);
    }

}

private void contButtonActionPerfromed(ActionEvent e) {
    //This groups the 8 original individual digits, that were seperated into a list, back into a string for printing purposes
    if(counter < numberOfStreams) {
        dataPrinter.setVisible(true);
        dataEntryField.setVisible(false);
        OriBuild = ("Your original bit-stream was: "
                + streamSplit.get(counter * 0)
                + streamSplit.get(counter * 1)
                + streamSplit.get(counter * 2)
                + streamSplit.get(counter * 3) + " "
                + streamSplit.get(counter * 4)
                + streamSplit.get(counter * 5)
                + streamSplit.get(counter * 6)
                + streamSplit.get(counter * 7));
        NewBuild = ("Your new Hamming bit-stream is: "
                //This groups the 12 new individual digits, that were seperated into a list, back into a string for printing purposes
                + finalStream.get(counter * 0)
                + finalStream.get(counter * 1)
                + finalStream.get(counter * 2)
                + finalStream.get(counter * 3) + " "
                + finalStream.get(counter * 4)
                + finalStream.get(counter * 5)
                + finalStream.get(counter * 6)
                + finalStream.get(counter * 7) + " "
                + finalStream.get(counter * 8)
                + finalStream.get(counter * 9)
                + finalStream.get(counter * 10)
                + finalStream.get(counter * 11));
        System.out.println(OriBuild + " " + NewBuild);
        dataPrinter.setText(OriBuild + "\n" + NewBuild);
        counter++;
        //Prints the strings to the screen so that the user can retrieve wanted information. Then adds 1 to the counter incase more than 1 stream was entered to cycle
    } else {
        dataPrinter.setText("Program complete. Close and relaunch to re-use");
        contButton.setVisible(false);
        //Once out of streams program finishes on this informing user that its reached the end of its usefulness and require re-launching.
    }
}

private static void createAndShowGui() {
    MainProgram mainPanel = new MainProgram();

    JFrame frame = new JFrame("HammingCode");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGui();
        }
    });
}

public int checkInput()
{
    String temp1 = dataEntryField.getText();
    int temp = Integer.parseInt(temp1);
    return temp;
}

public void streamAdd(){
    do {
        streamSplit.add(TEMP_STREAM % 10);
        TEMP_STREAM /= 10;
    } while (TEMP_STREAM != 0);
}

public void logicLaunch(){
    topTextArea.setText(CALCULATING);
    int arrayLength = streamSplit.size();
    int bufferLength = 8 - arrayLength % 8;

    if (bufferLength != 8)
    {
        numberOfStreams = arrayLength / 8 + 1;
    } else
    {
        numberOfStreams = arrayLength / 8;
    }
    int tempStreams = numberOfStreams;
    System.out.println(numberOfStreams + "<Streams Buffer>" + bufferLength);
    while (bufferLength > 0 && bufferLength != 8)
    {
        streamSplit.add(0);
        bufferLength--;
    }
    while (tempStreams > 0)
    {
        for (int i = 0; i < 8; i++)
        {
            tempEight.add(streamSplit.get(i));
        }
        if ((tempEight.get(0) + tempEight.get(1) + tempEight.get(3) + tempEight.get(4) + tempEight.get(6)) % 2 == 0)
        {
            tempEight.add(0, 0);
        } else
        {
            tempEight.add(0, 1);
        }
        if ((tempEight.get(1) + tempEight.get(3) + tempEight.get(4) + tempEight.get(6) + tempEight.get(7)) % 2 == 0)
        {
            tempEight.add(1, 0);
        } else
        {
            tempEight.add(1, 1);
        }
        if ((tempEight.get(3) + tempEight.get(4) + tempEight.get(5) + tempEight.get(9)) % 2 == 0)
        {
            tempEight.add(3, 0);
        } else
        {
            tempEight.add(3, 1);
        }
        if ((tempEight.get(7) + tempEight.get(8) + tempEight.get(9) + tempEight.get(10)) % 2 == 0)
        {
            tempEight.add(7, 0);
        } else
        {
            tempEight.add(7, 1);
        }
        tempStreams--;
        for (int i = 0; i < 12; i++)
        {
            finalStream.add(tempEight.get(0));
            tempEight.remove(0);
        }
    }
    Collections.reverse(streamSplit);
}//Runs all the logic to generate the 12 bit code, this part is working     fine. Reverse is for formatting purposes
}

Solution

  • I haven't figured out the code entirely, but it appears your use of counter in contButtonActionPerfromed is incorrect. When counter is 0, it will get element 0 every time; when 1 it will get 0, 1, 2, etc.; when 2 it will get 0, 2, 4, etc. Then there's something about exiting the program after the first time, so maybe you don't go through it a second time (and a third time would likely generate an error attempting to access a non-existent element).

    You really ought to be able to debug that method...