我正在创建一个程序来接收一个或多个8位二进制序列并将它们转换为12位汉明二进制序列。代码工作并实现一切,直到它试图将信息放入JTextField,此时它将8位流转换为1111 1111并将12位流转换为0000 0000 0000(间隔以便于阅读)。我已经尝试逐步执行代码,并且在每个点上它认为列表是正确的,直到我把它变成一个字符串。我不确定我是否错误地使用了某些东西,但希望有人可以提供帮助。
作为那些不懂汉明码的人的榜样。如果你输入" 10101010"没有空格,系统应该吐出"你的原始比特流是:1010 1010你的新汉明比特流是:1010 0101 1000"
但相反它会说"你原来的比特流是:1111 1111你的新汉明比特流是: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
}
答案 0 :(得分:2)
我还没有完全弄清楚代码,但是counter
中使用contButtonActionPerfromed
似乎不正确。当counter为0时,每次都会得到元素0; 1时它会得到0,1,2等; 2时它会得到0,2,4等等。然后有一些关于第一次退出程序的东西,所以也许你不会再次通过它(第三次可能会产生错误试图访问一个不存在的元素)。
你真的应该能够调试那种方法......