[英]How to get a string variable from another class
There are 2 main
in the 2-class: 2类中有2个main
:
New_String
is used to read text file from my file. New_String
类用于从我的文件中读取文本文件。 Write
is used to show the read value S1(=ar[1])
from the New_String
class. Write
类用于显示从New_String
类读取的值S1(=ar[1])
。 However, no matter how I tried, the Write
class is only showing null
,or it throws a NullPointerException
error. 但是,无论我如何尝试, Write
类仅显示null
,否则将引发NullPointerException
错误。
Because the program has further function in my next stage, I can't just combine the 2 class in one. 由于该程序在下一步中具有进一步的功能,因此我不能仅将2类组合在一起。 Please tell me how to adjust. 请告诉我如何调整。
write
public class write
{
//public static String getar=get.ar[1];
//getar = get.ar[];
public static void main(String args[]) throws IOException
{
New_string file = new New_string();
//site.readline
System.out.println(file.S1);
//String S1 = ar[1];
}
}
New_string
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class New_string
{
public static String S1;
public static int a=0;
public static String ar[];
public static int lnum=0;
public static String line=null;
public static void main(String args[]) throws IOException
{
FileReader fr= new FileReader("read_listflow.txt");
BufferedReader br=new BufferedReader(fr);
while ((line=br.readLine())!=null)
{
lnum=lnum+1;
}
FileReader fr1= new FileReader("read_listflow.txt");
BufferedReader br1=new BufferedReader(fr1);
ar=new String[lnum];
while ((line=br1.readLine())!=null)
{
ar[a]=line;
a=a+1;
}
S1 = ar[1];
}
}
main method will not automatically be called when you instantiate a class. 实例化类时,不会自动调用main方法。 You need to explicitly call the method of class to get the work done. 您需要显式调用class方法来完成工作。
You have to do something like this.. 你必须做这样的事..
public class NewString
{
public static String S1;
public static int a=0;
public static String ar[];
public static int lnum=0;
public static String line=null;
public String read() throws IOException
{
FileReader fr= new FileReader("read_listflow.txt");
BufferedReader br=new BufferedReader(fr);
while ((line=br.readLine())!=null)
{
lnum=lnum+1;
}
FileReader fr1= new FileReader("read_listflow.txt");
BufferedReader br1=new BufferedReader(fr1);
ar=new String[lnum];
while ((line=br1.readLine())!=null)
{
ar[a]=line;
a=a+1;
}
S1 = ar[1];
return S1;
}
}
and then 接着
public class Write
{
public static void main(String args[]) throws IOException
{
New_string file = new New_string();
System.out.println(file.read());
}
}
Also, please follow Java Naming conventions 另外,请遵循Java命名约定
Simplest solution is to add a method read in your New_string class and call that method in your write class I have done the changes 最简单的解决方案是在New_string类中添加一个读取的方法,并在您的write类中调用该方法,我已经完成了更改
public class write
{
//public static String getar=get.ar[1];
//getar = get.ar[];
public static void main(String args[]) throws IOException
{
New_string file = new New_string();
file.read()
//site.readline
System.out.println(file.S1);
//String S1 = ar[1];
}
}
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class New_string
{
public static String S1;
public static int a=0;
public static String ar[];
public static int lnum=0;
public static String line=null;
public read(String args[]) throws IOException
{
FileReader fr= new FileReader("read_listflow.txt");
BufferedReader br=new BufferedReader(fr);
while ((line=br.readLine())!=null)
{
lnum=lnum+1;
}
FileReader fr1= new FileReader("read_listflow.txt");
BufferedReader br1=new BufferedReader(fr1);
ar=new String[lnum];
while ((line=br1.readLine())!=null)
{
ar[a]=line;
a=a+1;
}
S1 = ar[1];
}
}
Bad code, but the following just does your dirty job :) 错误的代码,但是下面的代码会很脏:)
public class write
{
public static void main(String args[]) throws IOException {
New_string.main(mull);
System.out.println(New_string.S1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.