简体   繁体   English

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

[英]Java, Passing a String from one method to another

I hope someone could help me please, I need to pass a String from the method below to the method below that. 我希望有人可以帮助我,我需要将String从下面的方法传递给下面的方法。 I have looked on the interent and got it working on test programs but can't seem to get it working on mine, it's been 3 hours, 3 pages of google and a book lol. 我看过Interent并使其在测试程序上工作,但似乎无法使其在我的程序上工作,这已经3个小时了,有3页的google和一本书,哈哈。 Sorry if this is easy but I really have no idea. 抱歉,这很简单,但我真的不知道。

What I need to do... I need to pass the variable "Hex" from the method "WMDBAudio" to the method "hexConverter". 我需要做的...我需要将变量“ Hex”从方法“ WMDBAudio”传递到方法“ hexConverter”。 I hope this makes sense, thanks for your help in advance it's is apperciated! 我希望这是有道理的,谢谢您的帮助,感谢!

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 = "";
                    }

}
}

//Hex Converter method //十六进制转换器方法

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);
    }
}

By convention you name Java classes starting with upper cases. 按照约定,您可以使用大写字母命名Java类。 So hexConverter should be renamed to HexConverter. 因此hexConverter应该重命名为HexConverter。

You generally invoke another class from a class in this format: 通常,您可以使用以下格式从类中调用另一个类:

MyClass myClass = new MyClass(); MyClass myClass =新的MyClass();

after that you can use myClass object to access methods (not private) of MyClass. 之后,您可以使用myClass对象访问MyClass的方法(非私有)。

Make the following 2 lines change as I have commented. 根据我的评论,更改以下两行。

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); 

} } }}

You could set hex as a private attribute of the class, thus being acessible to both methods (and all others of the same class). 您可以将hex设置为该类的私有属性,从而使这两种方法(以及同一类的所有其他方法)都可以使用。

This assuming that calling the first one doesn't necessarily require calling the second one. 这假设调用第一个并不一定需要调用第二个。 If that's the case then you could just call hexConverter from WMDBAudio with an extra parameter for the hex String. 如果是这种情况,那么您可以从WMDBAudio调用hexConverter,并为hex String提供一个额外的参数。


EDIT: Nvm that just saw they are two different classes. 编辑: Nvm刚刚看到它们是两个不同的类。 Well, you could save the hex as a private variable on both classes and have a GetHex() method on the WMDBAudio class. 好了,您可以将十六进制保存为两个类的私有变量,并在WMDBAudio类上具有GetHex()方法。 You then use the value returned by that method to create a hexConverter class that takes Hex as a parameter to its constructor thus allowing something of the sort: 然后,您可以使用该方法返回的值来创建一个hexConverter类,该类将Hex作为其构造函数的参数,从而允许进行以下操作:

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

Or just supply an additional parameter to the hexConverter function allowing you to write something like this: 或者只是向hexConverter函数提供一个附加参数,使您可以编写如下内容:

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

Since hexConverter is a static method in hexConverter class, you can access the method as 由于hexConverter是hexConverter类中的静态方法,因此您可以按以下方式访问该方法

hexConverter.hexConverter(hex);

You need not create a new object to access the method. 您无需创建新对象即可访问该方法。 The method performs a common operation and does not change the state of the object. 该方法执行通用操作,并且不更改对象的状态。 Hence, you can use it as above, pass the String and get the result. 因此,您可以像上面一样使用它,传递String并获得结果。 You might also need to import hexConverter class if it is in a different package. 如果hexConverter类位于其他软件包中,则可能还需要导入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM