Reputation: 259
I'm new to Java programming, I just have a easy question but I can't do it because I didnt know how to write it on Java. Thanks in Advance.
I want to call variable values in another method.
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
}
Upvotes: 1
Views: 16348
Reputation: 466
Not sure what you exactly need. Anyway try this:
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);
}
btw following the naming convention.
Edit: the following code should compile successfully, in 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);
}
}
Upvotes: 1
Reputation: 38
You can't call variables like that. One way you can do is to declare the variable values globally (Outside of your main or any other functions and right at the top below your packages and imports). This way all other functions will be able to use the variable values. Another way is to call the checkStatus function from main sending the variable values. Example below
in main function:
checkStatus(values);
Call the function checkStatus passing the variable values. JAVA is a strict pass by value only language. What happens is that the function checkStatus is called and a copy of the variable values is created with the same values in it and passed to the function.
function:
public static void checkStatus(String[] values)
{
// call variable 'values' here
// Here you can make use of the variable values
}
If you plan to modify the variable values in the function checkStatus and want it to be modified in main altogether then you can either return the values back to main like so.
in main
String[] receive;
receive = checkStatus(values);
in the function:
public static String checkStatus(String[] values)
{
//Once you are done with your codes
return values;
}
Hope this helps. please do comment if you need further clarification or help.
Upvotes: 0
Reputation: 318
You can't access method variables outside the method that owns them. If the variable belongs directly to a class, the variables can be accessed by other methods.
Upvotes: 1