如何从另一个类获取字符串变量

时间:2014-02-26 11:27:54

标签: java variables main

2级中有2 main

  • New_String用于从我的文件中读取文本文件。
  • 课程Write用于显示S1(=ar[1])课程的阅读价值New_String

但是,无论我如何尝试,Write类只显示null,否则会引发NullPointerException错误。

因为程序在我的下一阶段有进一步的功能,所以我不能只将2类合二为一。请告诉我如何调整。

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];
     }
}

3 个答案:

答案 0 :(得分:0)

实例化类时,不会自动调用
main方法。您需要显式调用类的方法才能完成工作。

你必须做这样的事情......

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;
    }

}

然后

public class Write
{

   public static void main(String args[]) throws IOException
   {
     New_string file = new New_string();

     System.out.println(file.read());
   }
}

另外,请遵循 Java命名约定

答案 1 :(得分:0)

最简单的解决方案是在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];
     }
}

答案 2 :(得分:0)

错误的代码,但以下只是做你的脏工作:)

public class write
{

    public static void main(String args[]) throws IOException  {
        New_string.main(mull);
        System.out.println(New_string.S1);
    }
}