简体   繁体   English

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

[英]How to access one variable from one class to another of the same package

I want to access the integer variable X[0] from java class count in another java class radioques which are of same package. 我想从具有相同包的另一个Java类不透明对象中的Java类计数中访问整数变量X [0]。 I want to calculate average of X[0] from first java class and O from the second java class. 我想从第一个Java类计算X [0]和从第二个Java类计算O的平均值。 The classes are: 这些类是:

package abc;

import java.io.*;
import java.util.*;

public class count {

public static File f = new File("C:/Users/Prashant/Desktop/OCEAN/f.txt");
public static File[] fileList = new File("C:/Users/Prashant/Desktop/OCEAN/Clusters/").listFiles();

public int countWord(String word, File file) throws FileNotFoundException {
    int c = 0;
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
        String nextToken = scanner.next();
        if (nextToken.equalsIgnoreCase(word)) {
            c++;
        }
    }
    scanner.close();
    return c;
}

public static void main(String[] args) throws IOException, FileNotFoundException //, NoSuchElementException
{
    count c = new count();
    c.xyz();
}

public void xyz() throws IOException, FileNotFoundException //, NoSuchElementException
{
    int res = 0;
    int X[] = new int[10];
    for (int i = 0; i < 10; i++) {
        Scanner scanner1 = new Scanner(fileList[i]);
        res = 0;
        while (scanner1.hasNext()) {
            String nextToken1 = scanner1.next();
            count d = new count();
            int s = d.countWord(nextToken1, f);
            res += s;
        }
        System.out.println(res);
        X[i]=res;
        File fl = new File("C:/Users/Prashant/Desktop/OCEAN/res.txt");
        if (!fl.exists()) {
            fl.createNewFile();
        }
        FileWriter fw = new FileWriter(fl, true);
        BufferedWriter bw = new BufferedWriter(fw);
        //bw.write(res+"\n");
        bw.write(fileList[i].getName().toString() + "\t" + res + "\n");
        //bw.write(System.getProperty("line.separator"));
        bw.close();
        scanner1.close();
    }
}
}

The other class is: 另一类是:

package abc;

import org.apache.jasper.JasperException;

public class radioques {


public void rq(String q[]) throws JasperException, NullPointerException, Exception {
    int ques[] = new int[44];
    int i = 0;
    for (String s : q) {
        ques[i] = Integer.parseInt(s.trim());
        i++;
    }
    int o, c, e, a, n;
    o = ques[4] + ques[9] + ques[14] + ques[19] + ques[24] + ques[29] + (6 - ques[34]) + ques[39] + (6 - ques[40]) + ques[43];
    c = ques[2] + (6 - ques[7]) + ques[12] + (6 - ques[17]) + (6 - ques[22]) + ques[27] + ques[32] + ques[37] + (6 - ques[42]);
    e = ques[0] + (6 - ques[5]) + ques[10] + ques[15] + (6 - ques[20]) + ques[25] + (6 - ques[30]) + ques[35];
    a = (6 - ques[1]) + ques[6] + (6 - ques[11]) + ques[16] + ques[21] + (6 - ques[26]) + ques[31] + (6 - ques[36]) + ques[41];
    n = ques[3] + (6 - ques[8]) + ques[13] + ques[18] + (6 - ques[23]) + ques[28] + (6 - ques[33]) + ques[38];
    float O = (float) o;
    O = O / 50 * 100;
    float C = (float) c;
    C = C / 50 * 100;
    float E = (float) e;
    E = E / 50 * 100;
    float A = (float) a;
    A = A / 50 * 100;
    float N = (float) n;
    N = N / 50 * 100;
    System.out.println(O);
    System.out.println(C);
    System.out.println(E);
    System.out.println(A);
    System.out.println(N);



}

public static void main(String args[]) {

}
}

Your class count does not have a variable called X . 您的班级count没有名为X的变量。 Your method xyz has a local variable called X , but that is only valid during one call of the method. 您的方法xyz具有一个名为X的局部变量,但这仅在该方法的一次调用期间有效。

Maybe you should read some more about the basics of Java? 也许您应该阅读更多有关Java基础的知识?

Either return X[0] from xyz if xyz is called where you want that value, or make it a instance variable that you can access directly or through a getter method. 如果在需要该值的位置调用了xyz则从xyz返回X[0] ,或者将其设为可以直接访问或通过getter方法访问的实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中同一包中的不同文件上从一个类访问另一个类的变量 - How to access a variable from one class into another class on different files in same package in java 如何从另一个类访问一个变量? - How Do I Access One Variable From Another Class? 如何在 JavaFX 中从一个类控制器文件访问另一个变量? - How to access a variable from one class controller file to another in JavaFX? 如何从一个类访问方法的局部变量到另一个类 - How to Access local variable of the method from one class to another 为什么一类相同的包不能访问另一类? - why one class of same package can't access another? 从一个类访问同一包中的另一个类的方法 - Accessing method from one class to another class in same package 在同一包中使用从一个类到另一个类的Array元素 - Using Array elements from one class into another class in same package 如何从一个 class 中的扫描仪获取用户输入,然后将其应用于同一 package 中的另一个 class? - How do I take an user input from Scanner in one class and then apply it to another class in the same package? 如何从一个类访问String到另一个? - How to access String from one class to another? 如何从一个类访问另一个类的java类变量值(动态)? - How to access a java class variable value (dynamic) from one class to another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM