Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java,将字符串从一个方法传递到另一个方法_Java_String_Methods - Fatal编程技术网

Java,将字符串从一个方法传递到另一个方法

Java,将字符串从一个方法传递到另一个方法,java,string,methods,Java,String,Methods,我希望有人能帮助我,我需要将字符串从下面的方法传递到下面的方法。我已经在互联网上看过了,并在测试程序中得到了它的工作,但似乎无法在我的程序中得到它的工作,这是3个小时,3页谷歌和一本书lol。对不起,如果这是容易的,但我真的不知道 我需要做的是。。。我需要将变量“Hex”从方法“WMDBAudio”传递到方法“hexConverter”。我希望这是有道理的,谢谢你的帮助,提前它的安抚 public class WMDBAudio{ public String WMDBAudio1(String

我希望有人能帮助我,我需要将字符串从下面的方法传递到下面的方法。我已经在互联网上看过了,并在测试程序中得到了它的工作,但似乎无法在我的程序中得到它的工作,这是3个小时,3页谷歌和一本书lol。对不起,如果这是容易的,但我真的不知道

我需要做的是。。。我需要将变量“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 = "";
                    }

}
}
公共类WMDBAudio{
公共字符串WMDBAudio1(字符串fileInfo)引发IOException{
//代码删除,因为有很多
int m=0;
而(m!=1){
对于(int count=0;count<3;count++){
hexIn=in.read();
s=整数.toHexString(hexIn);
如果(s.长度()<2){
s=“0”+整数.toHexString(hexIn);
}
温度=温度+s;
}
如果(“000000”。相等信号情况(临时)){
m=1;
十六进制=输入;
}
入口=入口+温度;
温度=”;
}
}
}
//十六进制变换器法

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);
    }
}
公共类转换器{
公共静态转换器(字符串t){
WMDBAudio w=新的WMDBAudio();
字符串十六进制=”;
StringBuilder输出=新的StringBuilder();
对于(int i=0;i
您可以将hex设置为类的私有属性,因此这两个方法(以及同一类的所有其他方法)都可以访问

这假设调用第一个并不一定需要调用第二个。如果是这种情况,那么您可以从WMDBAudio调用hexConverter,并为十六进制字符串添加一个额外的参数


EDIT:Nvm刚刚看到它们是两个不同的类。好的,您可以将hex保存为两个类上的私有变量,并在WMDBAudio类上使用GetHex()方法。然后,使用该方法返回的值创建一个hexConverter类,该类将Hex作为其构造函数的参数,从而允许以下内容:

WMDBAudio audio = new WMDBAudio()
...
hexConverter hexconv = new hexConverter(audio.GetHex())
或者只向hexConverter函数提供一个附加参数,允许您编写如下内容:

WMDBAudio audio = new WMDBAudio()
...
hexConverter hexconv = new hexConverter()
hexconv.hexConverter(audio.GetHex())

按照惯例,可以从大写字母开始命名Java类。所以hexConverter应该重命名为hexConverter

您通常以以下格式从类中调用另一个类:

MyClass MyClass=新的MyClass()

之后,您可以使用myClass对象访问myClass的方法(非私有)

如我所评论的,更改以下两行

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); 
公共类WMDBAudio{
公共字符串WMDBAudio1(字符串fileInfo)引发IOException{
//代码删除,因为有很多
int m=0;
而(m!=1){
对于(int count=0;count<3;count++){
hexIn=in.read();
s=整数.toHexString(hexIn);
如果(s.长度()<2){
s=“0”+整数.toHexString(hexIn);
}
温度=温度+s;
}
如果(“000000”。相等信号情况(临时)){
m=1;
十六进制=输入;
}
入口=入口+温度;
温度=”;
}
//加上这两行
hexConverter hexConv=新的hexConverter();
六角转换器(hex);
}
}

由于hexConverter是hexConverter类中的静态方法, 您可以按以下方式访问该方法:

hexConverter.hexConverter(hex);
您无需创建新对象即可访问该方法。该方法执行公共操作,不更改对象的状态。因此,您可以如上所述使用它,传递字符串并获得结果。
如果hexConverter类位于不同的包中,您可能还需要导入它。

这似乎是一项简单的任务,除非您所做的事情与所描述的完全不同。只要调用hexConverter.hexConverter(Hex)就可以了。你能粘贴一下你遇到的异常情况吗。