使用Java计算p值

时间:2015-06-01 06:58:08

标签: rjava rcaller

我想制作一个Java程序来计算我要上传的数据的P值,但是这样做太麻烦了。 Java程序已经读取了数据,但下一步是解决获取p值的数据。

输入文件是:



The input Should be:- 

Name	A	B	C
a	1.7085586	0.73179674	3.3962722
b	0.092749596	-0.10030079	-0.47453594
c	1.1727467	0.15784931	0.0572958
d	-0.91714764	-0.62808895	-0.6190882
e	0.34570503	0.10605621	0.30304766
f	2.333506	-0.2063818	0.4022169
g	0.7893815	1.449388	1.5907407

And the Output should be like this:-
  
Name	pValue
a	0.129618298
b	0.4363544
c	0.323631285
d	0.017916658
e	0.076331828
f	0.385619995
g	0.035449488




我在R程序中运行了这些数据,但我想编写一些Java来解决这个问题。

我的Java代码到现在为止:



import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
public class Abc {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		BufferedReader br = null;
		 
		try {
 
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader("C:/Documents and Settings/Admin/Desktop/test.txt"));
 
			while ((sCurrentLine = br.readLine()) != null) {
				System.out.println(sCurrentLine);
				//output  = BufferedReader.getParser().getAsDoubleArray("br");
				
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}




这只是读取我的数据并在Java的控制台中显示它。怎么办?

1 个答案:

答案 0 :(得分:1)

我认为您在从文件中检索和打印值时遇到问题。以下程序以所需格式提供输出:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Abc {

    public static void main(String[] args) {

        BufferedReader br = null;
        String sCurrentLine;

        try {

            br = new BufferedReader(new FileReader("C:/Documents and Settings/Admin/Desktop/test.txt"));

            // Override Default Header Row
            br.readLine();
            System.out.println("Name" + "\t" + "pValue");

            while ((sCurrentLine = br.readLine()) != null) {
                int i = 0;
                String str[] = sCurrentLine.split("\t");

                System.out.print(str[i] + "\t");

                Double dValue1 = Double.parseDouble(str[++i]);
                Double dValue2 = Double.parseDouble(str[++i]);
                Double dValue3 = Double.parseDouble(str[++i]);

                // Do pValue Calc here
                Double pValue = 1.2334;

                System.out.println(pValue);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}