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

拉姆·基

我想访问Totalnoofwords类newrepeatedcount中的类。我想打印类的“一” Totalnoofwordsmegring与

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”都不起作用。(反之亦然)如果我更改代码(变量)顺序。

有人告诉我如何同时获取变量的输出吗?:)

这是我的更新代码。

 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);
}
}}
用户名
package Packagename;

public class newrepeatedcount {

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

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从Java中的同一包中导入类?

来自分类Dev

在JAVA中访问另一个类变量

来自分类Dev

使变量只能在同一包中的另一个类访问

来自分类Dev

从另一个类访问一个类中的变量

来自分类Dev

OSGi:导出一个包,而不导出同一包中的几个类

来自分类Dev

另一个文件中同一包中的golang参考结构

来自分类Dev

从Java中的另一个类访问变量

来自分类Dev

Java / IntelliJ将类保留在另一个文件夹中,但在同一包中

来自分类Dev

如何访问在属于另一个Java类的方法中声明的变量?

来自分类Dev

Java导入类在同一包中

来自分类Dev

为什么Java类可以在不显式导入该类的情况下直接在同一包下使用另一个类?

来自分类Dev

如何从同一类的另一个函数访问一个函数中的变量?

来自分类Dev

访问同一包的另一个类中的嵌套类

来自分类Dev

如何创建一个可执行的jar文件,该文件可以执行同一包中其他Java类的方法

来自分类Dev

从同一类中的另一个构造函数访问构造函数变量

来自分类Dev

如何将Java类(从一个包中)继承到另一个类(在另一个包中)

来自分类Dev

如何使一个Java类仅对包中的另一个类可用

来自分类Dev

如何在Java中的另一个类中从主类访问int?

来自分类Dev

Java:如何从不在同一文件中但在同一包中的另一个Jframe打开一个Jframe;

来自分类Dev

一个Java类在同一包中找不到另一个

来自分类Dev

将一个方法中的变量访问到同一类中的另一个方法

来自分类Dev

从同一类中的另一个方法访问变量

来自分类Dev

如何在JAVA的同一包中调用方法或访问其他类中的字段

来自分类Dev

在Python中从一个类到另一个类访问变量

来自分类Dev

快速访问另一个类中的变量

来自分类Dev

如何访问另一个DLL中的类?

来自分类Dev

从Java中的另一个类访问变量

来自分类Dev

从另一个类访问一个类,两个类都在同一个包中

来自分类Dev

如何访问同一实例中另一个类中的函数?

Related 相关文章

  1. 1

    如何从Java中的同一包中导入类?

  2. 2

    在JAVA中访问另一个类变量

  3. 3

    使变量只能在同一包中的另一个类访问

  4. 4

    从另一个类访问一个类中的变量

  5. 5

    OSGi:导出一个包,而不导出同一包中的几个类

  6. 6

    另一个文件中同一包中的golang参考结构

  7. 7

    从Java中的另一个类访问变量

  8. 8

    Java / IntelliJ将类保留在另一个文件夹中,但在同一包中

  9. 9

    如何访问在属于另一个Java类的方法中声明的变量?

  10. 10

    Java导入类在同一包中

  11. 11

    为什么Java类可以在不显式导入该类的情况下直接在同一包下使用另一个类?

  12. 12

    如何从同一类的另一个函数访问一个函数中的变量?

  13. 13

    访问同一包的另一个类中的嵌套类

  14. 14

    如何创建一个可执行的jar文件,该文件可以执行同一包中其他Java类的方法

  15. 15

    从同一类中的另一个构造函数访问构造函数变量

  16. 16

    如何将Java类(从一个包中)继承到另一个类(在另一个包中)

  17. 17

    如何使一个Java类仅对包中的另一个类可用

  18. 18

    如何在Java中的另一个类中从主类访问int?

  19. 19

    Java:如何从不在同一文件中但在同一包中的另一个Jframe打开一个Jframe;

  20. 20

    一个Java类在同一包中找不到另一个

  21. 21

    将一个方法中的变量访问到同一类中的另一个方法

  22. 22

    从同一类中的另一个方法访问变量

  23. 23

    如何在JAVA的同一包中调用方法或访问其他类中的字段

  24. 24

    在Python中从一个类到另一个类访问变量

  25. 25

    快速访问另一个类中的变量

  26. 26

    如何访问另一个DLL中的类?

  27. 27

    从Java中的另一个类访问变量

  28. 28

    从另一个类访问一个类,两个类都在同一个包中

  29. 29

    如何访问同一实例中另一个类中的函数?

热门标签

归档