我希望有人能帮助我,我需要将一个字符串从下面的方法传递到下面的方法。我查看了 interent 并让它在测试程序上运行,但似乎无法在我的程序上运行,已经 3 个小时,3 页 google 和一本书,哈哈。对不起,如果这很容易,但我真的不知道。
我需要做什么...我需要将变量“Hex”从方法“WMDBAudio”传递到方法“hexConverter”。我希望这是有道理的,感谢您提前提供帮助!
public class WMDBAudio{
public String WMDBAudio1(String fileInfo) throws IOException{
//code removed as there is quite a lot
int m = 0;
while (m != 1){
for (int count = 0; count < 3; count++){
hexIn = in.read();
s = Integer.toHexString(hexIn);
if(s.length() < 2){
s = "0" + Integer.toHexString(hexIn);
}
temp = temp + s;
}
if ("000000".equalsIgnoreCase(temp)){
m = 1;
hex = entry;
}
entry = entry + temp;
temp = "";
}
}
}
//十六进制转换方式
public class hexConverter{
public static void hexConverter(String t){
WMDBAudio w = new WMDBAudio();
String hex = "";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2){
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);
}
}
最佳答案
按照惯例,您以大写字母开头命名 Java 类。所以 hexConverter 应该重命名为 HexConverter。
您通常以这种格式从一个类中调用另一个类:
MyClass myClass = new MyClass();
之后您可以使用 myClass 对象访问 MyClass 的方法(非私有(private))。
按照我的评论更改以下两行。
public class WMDBAudio{
public String WMDBAudio1(String fileInfo) throws IOException{
//code removed as there is quite a lot
int m = 0;
while (m != 1){
for (int count = 0; count < 3; count++){
hexIn = in.read();
s = Integer.toHexString(hexIn);
if(s.length() < 2){
s = "0" + Integer.toHexString(hexIn);
}
temp = temp + s;
}
if ("000000".equalsIgnoreCase(temp)){
m = 1;
hex = entry;
}
entry = entry + temp;
temp = "";
}
//add these 2 lines
hexConverter hexConv = new hexConverter();
hexconv.hexConverter(hex);
}
关于Java,将字符串从一种方法传递到另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4824019/