如何从java中的同一个包中的另一个类访问变量

时间:2016-01-28 05:32:17

标签: java arrays class bufferedreader

我想访问newrepeatedcount类中的类Totalnoofwords。 我想用

打印一个类Totalnoofwords megring的“a”
System.out.println("( "+file1.getName() +" )-" +"Total words counted:"+total);
newrepeatedcount。

所以我可以运行两个代码来获取System.out.println("( "+file1.getName() +" )-" +" Total no of words=" + a +"Total repeated words counted:"+total);

以下是我想要的1个输出的片段  (filenameBlog 39.txt) - 单词总数= 83,计算的重复单词总数:4

欢迎任何建议。 我是java的初学者。 以下是我的两个类代码。:)

Totalnoofwords.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import org.apache.commons.io.FileUtils;

  public class Totalnoofwords
  {
   public static void main(String[] args) 
  {
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    };

    File folder = new File("E:\\testfolder");
    File[] listOfFiles = folder.listFiles(filter);

    for (int i = 0; i < listOfFiles.length; i++) {
        File file1 = listOfFiles[i];
        try {
    String content = FileUtils.readFileToString(file1);

    } catch (IOException e) {
    e.printStackTrace();
        }
                BufferedReader ins = null;
        try {
            ins = new BufferedReader (
                    new InputStreamReader(
                        new FileInputStream(file1)));
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        String line = "", str = "";
        int a = 0;
        int b = 0;
        try {
                    while ((line = ins.readLine()) != null) {
    str += line + " ";
    b++;
    }
        } catch (IOException e) {
    e.printStackTrace();
    }
        StringTokenizer st = new StringTokenizer(str);
        while (st.hasMoreTokens()) {
        String s = st.nextToken();
        a++;
        }
               System.out.println(" Total no of words=" + a );
    }
        }
      }

newrepeatedcount.java

package ramki;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class newrepeatedcount  {
public static void main(String[] args){

FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    };
    File folder = new File("E:\\testfolder\\");
    File[] listOfFiles = folder.listFiles(filter);
for (int i = 0; i < listOfFiles.length; i++) {
        File file1 = listOfFiles[i];
        try {
                String content = FileUtils.readFileToString(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        BufferedReader ins = null;
        try {
                    ins = new BufferedReader ( new InputStreamReader(new FileInputStream(file1)));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    String st = null;
try {
    st = IOUtils.toString(ins);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 //split text to array of words
 String[] words=st.split("\\s");
//frequency array
 int[] fr=new int[words.length];
//init frequency array
 for(int i1=0;i1<fr.length;i1++)
   fr[i1]=-1;
 //count words frequency
 for(int i1=0;i1<words.length;i1++){
   for(int j=0;j<words.length;j++){
     if(words[i1].equals(words[j]))
       {
         fr[i1]++;

            }
        }
 }      
 //clean duplicates
   for(int i1=0;i1<words.length;i1++){
     for(int j=0;j<words.length;j++){
       if(words[i1].equals(words[j]))
       {
         if(i1!=j) words[i1]="";
       }
 }
 }   
//show the output
int total=0;
//System.out.println("Duplicate words:");
for(int i1=0;i1<words.length;i1++){
if(words[i1]!=""){
//System.out.println(words[i1]+"="+fr[i1]);
total+=fr[i1];
}
}
//System.out.println("Total words counted: "+total);    
//System.out.println("Total no of repeated words : "+total+" ");
System.out.println("( "+file1.getName() +" )-" +"Total repeated words counted:"+total);
}  

}}

我试图将这两个代码放入一个类中 但这两个变量都没有起作用 System.out.println("( "+file1.getName() +" )-" +" Total no of words=" + a +"Total repeated words counted:"+total);

当我没有“a”或“total”运行时(反之亦然)如果我改变了代码(变量)顺序。

任何人都告诉我应该如何获得变量输出? :)

这是我更新的code.below。

 package ramki;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.StringTokenizer;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 public class newrepeatedcount  {
 public static void main(String[] args){
 FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    };
    File folder = new File("E:\\testfolder\\");
    File[] listOfFiles = folder.listFiles(filter);
for (int i = 0; i < listOfFiles.length; i++) {
        File file1 = listOfFiles[i];
        try {
                String content = FileUtils.readFileToString(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        BufferedReader ins = null;
        try {
                    ins = new BufferedReader ( new InputStreamReader(new FileInputStream(file1)));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
      String line = "", str = "";
      String st = null;
        try {
    st = IOUtils.toString(ins);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
   //split text to array of words
   String[] words=st.split("\\s");
   //frequency array
   int[] fr=new int[words.length];
  //init frequency array
  for(int i1=0;i1<fr.length;i1++)
   fr[i1]=-1;
  //count words frequency
  for(int i1=0;i1<words.length;i1++){
   for(int j=0;j<words.length;j++){
     if(words[i1].equals(words[j]))
       {
         fr[i1]++;       
            }
        }
   }      
   //clean duplicates
    for(int i1=0;i1<words.length;i1++){
     for(int j=0;j<words.length;j++){
       if(words[i1].equals(words[j]))
       {
         if(i1!=j) words[i1]="";
       }
    }
   }    
     int a = 0;
    try {
    while ((line = ins.readLine()) != null) {
    str += line + " ";
    }   
} catch (IOException e) {
            e.printStackTrace();
}
StringTokenizer st1 = new StringTokenizer(str);
while (st1.hasMoreTokens()) {
String s = st1.nextToken();
a++;
}
int total=0;
   for(int i1=0;i1<words.length;i1++){
if(words[i1]!=""){
//System.out.println(words[i1]+"="+fr[i1]);
total+=fr[i1];
} 
}
 System.out.println("( "+file1.getName() +" )-" +"Total repeated words counted:"+total+","+"total no of words:"+a);
// System.out.println("total no of words:"+a);
}
}}

3 个答案:

答案 0 :(得分:1)

无法从其他类访问main函数内的变量。 所以你可以修改Totalnoofwords.java之类的东西。

package Packagename;

public class Totalnoofwords
{

    static int a = 1;
        public void somename(){
            Totalnoofwords A=new Totalnoofwords();
            A.a+=5;
            System.out.println("a"+A.a);
        }

}

,你的newrepeatedcount.java就像

package Packagename;

public class newrepeatedcount {

    public static void main(String[] args){
        Totalnoofwords B=new Totalnoofwords();
        B.somename();
        System.out.println("a:"+B.a);
    }

}

答案 1 :(得分:1)

package Packagename;

public class newrepeatedcount {

public static void main(String[] args){
    Totalnoofwords B=new Totalnoofwords();
    B.somename();
    System.out.println("a:"+B.a);
}

}

答案 2 :(得分:0)

看起来你在同一个软件包中有两种主要方法,我不确定你是否想要这种方式,但这不会起作用,因为你没有重载方法。例如,目前你在一个类中有public static void main(String [] args),如果你改变另一个类来接受一个额外的参数public static void main(String [] args1,String [] args2)。

同样为了访问你的第二堂课,如上所述,你会使用像

这样的东西
Totalnoofwords totalNoofWords = new Totalnoofwords();
totalNoofWords.accessSomething();

但这不会起作用,因为你没有构造函数