如何阻止列表将 1 和 0 的混合转换为全 0?
How to stop list from converting mix of 1s and 0s into all 0s?
我正在创建一个程序来接收 1 个或多个 8 位二进制序列并将它们转换为 12 位汉明二进制序列。该代码可以正常工作并实现所有功能,直到它尝试将信息放入 JTextField 中,此时它将 8 位流转换为 1111 1111 并将 12 位流转换为 0000 0000 0000(为了便于阅读而留有间距)。我试过单步执行代码,在它认为列表的每一点都是正确的,直到我把它变成一个字符串。我不确定我是否错误地使用了某些东西,但希望有人能提供帮助。
给不懂汉明码的人举个例子。如果你输入“10101010”没有空格,系统应该吐出"Your original bit-stream was: 1010 1010 Your new Hamming bit-stream is: 1010 0101 1000"
但是它会说 "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
}
我还没有完全弄清楚代码,但您在 contButtonActionPerfromed
中使用 counter
似乎不正确。当counter为0时,每次取元素0;当为 1 时,它将得到 0、1、2 等;当 2 时,它会得到 0、2、4 等。然后在第一次之后退出程序,所以也许你不会第二次经历它(第三次可能会在尝试访问时产生错误一个不存在的元素)。
你真的应该能够调试那个方法...
我正在创建一个程序来接收 1 个或多个 8 位二进制序列并将它们转换为 12 位汉明二进制序列。该代码可以正常工作并实现所有功能,直到它尝试将信息放入 JTextField 中,此时它将 8 位流转换为 1111 1111 并将 12 位流转换为 0000 0000 0000(为了便于阅读而留有间距)。我试过单步执行代码,在它认为列表的每一点都是正确的,直到我把它变成一个字符串。我不确定我是否错误地使用了某些东西,但希望有人能提供帮助。
给不懂汉明码的人举个例子。如果你输入“10101010”没有空格,系统应该吐出"Your original bit-stream was: 1010 1010 Your new Hamming bit-stream is: 1010 0101 1000"
但是它会说 "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
}
我还没有完全弄清楚代码,但您在 contButtonActionPerfromed
中使用 counter
似乎不正确。当counter为0时,每次取元素0;当为 1 时,它将得到 0、1、2 等;当 2 时,它会得到 0、2、4 等。然后在第一次之后退出程序,所以也许你不会第二次经历它(第三次可能会在尝试访问时产生错误一个不存在的元素)。
你真的应该能够调试那个方法...