gpt4 book ai didi

java - 字节组合生成器和定序器

转载 作者:行者123 更新时间:2023-12-01 11:11:59 27 4
gpt4 key购买 nike

首先,这个问题将是关于同一代码的两个问题打包成一个。另外,请原谅我对这门语言的无知,我在过去两周才刚刚学习 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类'compile搜索方法{0-1 , \\s}输入字符串中的字符,如果找到其中任何字符,则会设置 rawBinaryArrayOutput为真。有没有办法让它只设置rawBinaryArrayOutput如果找到所有这些值中的至少 1 个,则为 true?

2:在binarySequencer中方法我有一个StringBuilder inputSB自动取String input的值这样我就可以修改 StringBuilder 中的值。在第 75 行,我尝试将值设置为位置 inull这样,如果 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(); 。如果可能的话,我如何才能打印出堆栈跟踪?

最佳答案

  1. 对于rawBinaryArrayOutput,您可以尝试使用正则表达式:

    ^([01](,\s)?)+$

    它将接受具有 01 序列(一个或多个字符)的字符串由可选的 ,\s 部分分隔。然而对于位输入,也许更好的是:

    ^(?:(?:[01]{8})+(?:,\s)?)+$

    仅接受 8 个字符序列中的一个或多个 01?: 部分用于非正则表达式分组。

  2. 方法 (int, Object) 对于类型 StringBuilder 不明确据我所知, StringBuilder 类有两个类似的方法,在本例中是:insert(int, String)insert(int, char[]),而编译器不会知道你想调用哪个,因为作为第二个变量你使用了null,并且null引用可以转换为任何类类型的表达式。尝试使用:

    inputSB.insert(i, (char[]) null); 
    例如。那么你调用哪个方法就一目了然了,即使它是与案例无关。

  3. 我不会使用InputMismatchException() ,最好抛出另一个自定义异常。使用正确的正则表达式,将没有双真匹配的选项,而使用双假,您可以只打印有用的信息。

关于java - 字节组合生成器和定序器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32240157/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com