字节组合生成器和定序器
Byte combination generator and sequencer
首先,这个问题将把关于同一代码的 2 个问题合并为一个。另外,请原谅我对语言的无知,我最近两周才学 Java,这是我第一个主要项目的一部分。
考虑以下代码:
public class TESTCODE {
public static ArrayList<String> bytePossibalitiyGenerator(int bits, String current) throws Exception {
ArrayList<String> binaries = new ArrayList<>();
if (bits%8 != 0) {
int crash = bits%8;
throw new Exception("The bit count that you have entered is not divisable by 8:" + "\n" + "There is a remainder of: " + Integer.toString(crash));
} else {
if (current.length() == bits) {
binaries.add(current);
return binaries;
}
// pad a 0 and 1 in front of current;
binaries.addAll(bytePossibalitiyGenerator(bits, "0" + current));
binaries.addAll(bytePossibalitiyGenerator(bits, "1" + current));
System.out.println(binaries.toString());
}
return binaries;
}
//The method below is supposed to format out the whitespace between binary strings and arrange the
//data in such a way that each possible outcome is on a new line.
//TODO Add a parser for the size of the byte ex. if the binary string is comprised of all of the possible
//TODO outcomes for 1 byte than every 8 instead of appending a space, append a new line.
public static String binarySequencer(String input) {
StringBuffer toReturn = new StringBuffer();
//This StringBuilder is a safety precaution to ensure that if the algorithm is to be
//run again, the value of each previously read and appended string position is nullified
//so that it is not re-appended to the StringBuffer
StringBuilder inputSB = new StringBuilder(input);
//Setting the booleans for which type of string the input was (The raw binary array itself, or the array converted into a string)
boolean rawBinaryArrayOutput = Pattern.compile("^[0-1,\s]+$").matcher(input).find();
boolean stringBinary = Pattern.compile("^[0-1\t]+$").matcher(input).find();
//This boolean is to check whether the loop has previously passed over 1 byte for sequencing
boolean hasPassedAByte = false;
//Safety if statements, because who doesn't love Java Exceptions and stack traces...
if (rawBinaryArrayOutput == false && stringBinary == false || rawBinaryArrayOutput == true && stringBinary == true) {
System.out.println(rawBinaryArrayOutput);
System.out.println(stringBinary);
//TODO Find a way to print the stack trace...
throw new InputMismatchException();
} else {
int runLength = 0;
for (int i = 0; i < inputSB.length(); i++) {
int j = 0;
if (stringBinary == true && rawBinaryArrayOutput == false) {
if (runLength == 8 && (inputSB.charAt(j += i) == 0 || inputSB.charAt(j += 1) == 1)) {
toReturn.append(Character.toString(' '));
runLength = 0;
hasPassedAByte = true;
} else {
if (hasPassedAByte = true && runLength == 8 && (inputSB.charAt(j) != 0 || inputSB.charAt(j) != 1)) {
toReturn.append("\n");
runLength = 0;
hasPassedAByte = false;
}
while (i + 1 < inputSB.length() && (inputSB.charAt(i) == 0 || inputSB.charAt(i) == 1) && runLength != 8) {
runLength++;
toReturn.append(inputSB.charAt(i));
inputSB.insert(i, null);
i++;
}
}
} else {
if (rawBinaryArrayOutput == true && stringBinary == false) {
//Insert code for formatting the raw binary array output
System.out.println("You haven't added this code yet :p");
}
}
}
}
return toReturn.toString();
}
public static void main(String[] args) throws Exception {
String toBeSequenced = "";
for (String s : bytePossibalitiyGenerator(16, "")) {
toBeSequenced += s + "\t";
}
System.out.println(binarySequencer(toBeSequenced));}}
现在开始提问:
1:对于 boolean rawBinaryArrayOutput
我正在使用 java.util.regex.Pattern
class' compile
方法在输入的字符串中搜索 {0-1 , \s}
个字符,并且如果找到其中任何一个,它会将 rawBinaryArrayOutput
设置为 true。如果找到所有这些值中的至少 1 个,有没有办法让它仅将 rawBinaryArrayOutput
设置为 true?
2:在binarySequencer
方法中我有一个StringBuilder inputSB
自动获取String input
的值,这样我就可以修改StringBuilder
中的值。在第 75 行,我试图将位置 i
的值设置为 null
,这样如果 while 循环以某种方式在同一位置运行两次,它就不会将任何内容附加到 StringBuilder toReturn
,但是 Eclipse 在该行 The method (int, Object) is ambiguous for type StringBuilder
上给我一个编译错误。这是什么意思,我该如何解决?
3:我在 binarySequencer
方法的开头有一个 if 语句,它检查是否 rawBinaryArrayOutput == false && stringBinary == false || rawBinaryArrayOutput == true && stringBinary == true
,如果是,那么它将 throw new InputMismatchException();
。如果可能的话,我怎样才能让它也打印出堆栈跟踪?
对于 rawBinaryArrayOutput
你可以尝试使用正则表达式:
^([01](,\s)?)+$
将接受带有 0
或 1
序列的字符串(一个或多个字符)
由可选的 ,\s
部分分隔。但是对于位输入,也许
更好的是:
^(?:(?:[01]{8})+(?:,\s)?)+$
将只接受 0
或 8 个字符序列中的一个或多个
1
。 ?:
部分用于非正则表达式分组。
The method (int, Object) is ambiguous for type StringBuilder
据我所知,StringBuilder class 有两个相似的方法,在本例中是:
insert(int, String)
和 insert(int, char[])
,编译器不会
知道你想调用哪个,因为作为第二个变量你
使用 null
,空引用可以转换为任何 class 类型的表达式。尝试:
inputSB.insert(i, (char[]) null);
例如。那么很明显你调用了哪个方法,即使它是
与案例无关。
我不会用InputMismatchException(),不如再抛一个自定义异常。使用正确的正则表达式,将没有双真匹配选项,使用双假匹配,您可以只打印有用的信息。
首先,这个问题将把关于同一代码的 2 个问题合并为一个。另外,请原谅我对语言的无知,我最近两周才学 Java,这是我第一个主要项目的一部分。
考虑以下代码:
public class TESTCODE {
public static ArrayList<String> bytePossibalitiyGenerator(int bits, String current) throws Exception {
ArrayList<String> binaries = new ArrayList<>();
if (bits%8 != 0) {
int crash = bits%8;
throw new Exception("The bit count that you have entered is not divisable by 8:" + "\n" + "There is a remainder of: " + Integer.toString(crash));
} else {
if (current.length() == bits) {
binaries.add(current);
return binaries;
}
// pad a 0 and 1 in front of current;
binaries.addAll(bytePossibalitiyGenerator(bits, "0" + current));
binaries.addAll(bytePossibalitiyGenerator(bits, "1" + current));
System.out.println(binaries.toString());
}
return binaries;
}
//The method below is supposed to format out the whitespace between binary strings and arrange the
//data in such a way that each possible outcome is on a new line.
//TODO Add a parser for the size of the byte ex. if the binary string is comprised of all of the possible
//TODO outcomes for 1 byte than every 8 instead of appending a space, append a new line.
public static String binarySequencer(String input) {
StringBuffer toReturn = new StringBuffer();
//This StringBuilder is a safety precaution to ensure that if the algorithm is to be
//run again, the value of each previously read and appended string position is nullified
//so that it is not re-appended to the StringBuffer
StringBuilder inputSB = new StringBuilder(input);
//Setting the booleans for which type of string the input was (The raw binary array itself, or the array converted into a string)
boolean rawBinaryArrayOutput = Pattern.compile("^[0-1,\s]+$").matcher(input).find();
boolean stringBinary = Pattern.compile("^[0-1\t]+$").matcher(input).find();
//This boolean is to check whether the loop has previously passed over 1 byte for sequencing
boolean hasPassedAByte = false;
//Safety if statements, because who doesn't love Java Exceptions and stack traces...
if (rawBinaryArrayOutput == false && stringBinary == false || rawBinaryArrayOutput == true && stringBinary == true) {
System.out.println(rawBinaryArrayOutput);
System.out.println(stringBinary);
//TODO Find a way to print the stack trace...
throw new InputMismatchException();
} else {
int runLength = 0;
for (int i = 0; i < inputSB.length(); i++) {
int j = 0;
if (stringBinary == true && rawBinaryArrayOutput == false) {
if (runLength == 8 && (inputSB.charAt(j += i) == 0 || inputSB.charAt(j += 1) == 1)) {
toReturn.append(Character.toString(' '));
runLength = 0;
hasPassedAByte = true;
} else {
if (hasPassedAByte = true && runLength == 8 && (inputSB.charAt(j) != 0 || inputSB.charAt(j) != 1)) {
toReturn.append("\n");
runLength = 0;
hasPassedAByte = false;
}
while (i + 1 < inputSB.length() && (inputSB.charAt(i) == 0 || inputSB.charAt(i) == 1) && runLength != 8) {
runLength++;
toReturn.append(inputSB.charAt(i));
inputSB.insert(i, null);
i++;
}
}
} else {
if (rawBinaryArrayOutput == true && stringBinary == false) {
//Insert code for formatting the raw binary array output
System.out.println("You haven't added this code yet :p");
}
}
}
}
return toReturn.toString();
}
public static void main(String[] args) throws Exception {
String toBeSequenced = "";
for (String s : bytePossibalitiyGenerator(16, "")) {
toBeSequenced += s + "\t";
}
System.out.println(binarySequencer(toBeSequenced));}}
现在开始提问:
1:对于 boolean rawBinaryArrayOutput
我正在使用 java.util.regex.Pattern
class' compile
方法在输入的字符串中搜索 {0-1 , \s}
个字符,并且如果找到其中任何一个,它会将 rawBinaryArrayOutput
设置为 true。如果找到所有这些值中的至少 1 个,有没有办法让它仅将 rawBinaryArrayOutput
设置为 true?
2:在binarySequencer
方法中我有一个StringBuilder inputSB
自动获取String input
的值,这样我就可以修改StringBuilder
中的值。在第 75 行,我试图将位置 i
的值设置为 null
,这样如果 while 循环以某种方式在同一位置运行两次,它就不会将任何内容附加到 StringBuilder toReturn
,但是 Eclipse 在该行 The method (int, Object) is ambiguous for type StringBuilder
上给我一个编译错误。这是什么意思,我该如何解决?
3:我在 binarySequencer
方法的开头有一个 if 语句,它检查是否 rawBinaryArrayOutput == false && stringBinary == false || rawBinaryArrayOutput == true && stringBinary == true
,如果是,那么它将 throw new InputMismatchException();
。如果可能的话,我怎样才能让它也打印出堆栈跟踪?
对于
rawBinaryArrayOutput
你可以尝试使用正则表达式:^([01](,\s)?)+$
将接受带有
0
或1
序列的字符串(一个或多个字符) 由可选的,\s
部分分隔。但是对于位输入,也许 更好的是:^(?:(?:[01]{8})+(?:,\s)?)+$
将只接受
0
或 8 个字符序列中的一个或多个1
。?:
部分用于非正则表达式分组。The method (int, Object) is ambiguous for type StringBuilder
据我所知,StringBuilder class 有两个相似的方法,在本例中是:insert(int, String)
和insert(int, char[])
,编译器不会 知道你想调用哪个,因为作为第二个变量你 使用null
,空引用可以转换为任何 class 类型的表达式。尝试:inputSB.insert(i, (char[]) null);
例如。那么很明显你调用了哪个方法,即使它是 与案例无关。
我不会用InputMismatchException(),不如再抛一个自定义异常。使用正确的正则表达式,将没有双真匹配选项,使用双假匹配,您可以只打印有用的信息。