我是Java编程的新手,我只是有一个简单的问题,但由于我不知道如何在Java上编写它,所以无法做到。提前致谢。
我想在另一个方法中调用变量值。
public static void ReadIN() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(","); // want to be call
for (String inputIN : values) {
inputIN = values[2];
}
}
br.close();
}
public static void checkStatus() {
// call variable 'values' here
}
最佳答案
不知道您到底需要什么。无论如何尝试:
public static void readIN() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(","); // want to be call
for (String inputIN : values) {
inputIN = values[2];
}
checkStatus(values);
}
br.close();
}
public static void checkStatus(String[] values) {
// call variable 'values' here
System.out.println(values);
}
顺便说一句,遵循命名约定。
编辑:以下代码应在Test.java中成功编译
import java.io.*;
public class Test
{
public static void main(String[] args) throws Exception {
Test.readIN();
}
public static void readIN() throws Exception {
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
Test.checkStatus(values);
}
br.close();
}
public static void checkStatus(String[] values) {
// call variable 'values' here
System.out.println(values);
}
}