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

标签 java string methods

我希望有人能帮助我,我需要将一个字符串从下面的方法传递到下面的方法。我查看了 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/

相关文章:

java - 如何在使用方法输出并返回结果时计算字符串中的元音和辅音并将首字母大写

java - ClassLoad 一个 Enum 类型

java - 检查扫描仪输入是否为正整数

Python 字符串格式 - 旧 `%` 与新 `str.format`

Java 字符串匹配正则表达式

coding-style - 如何命名类似工厂的方法?

Javascript 在构造函数中调用原型(prototype)函数

java - LibGDX 玩家移动

java - 从集合中获取随机元素

string - 如何处理字符串数组?