开发者

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. 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. 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". 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. So hexConverter should be renamed to HexConverter.

You generally invoke another class from a class in this format:

MyClass myClass = new MyClass();

after that you can use myClass object to access methods (not private) of 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).

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.


EDIT: Nvm that just saw they are two different classes. Well, you could save the hex as a private variable on both classes and have a GetHex() method on the WMDBAudio class. 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:

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:

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(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. You might also need to import hexConverter class if it is in a different package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜