簡體   English   中英

Java將某些輸出寫入文件

[英]java writing certain output to file

我對如何將程序輸出的特定部分寫入文件有疑問。 就我而言,我的程序顯示所有行星的半徑,質量,直徑和重力,而我只需要在文件中顯示重力。 當我運行程序時,將創建文件,但其中顯示的全部是這樣的:

[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7

我怎樣才能解決這個問題? 任何幫助將不勝感激。 以下是我的程序的摘要:

public static double[] calcGravity(double[] radius, double[] mass)
{
    // fill in code here
        double[] grav = new double[radius.length];
        for(int i = 0; i < radius.length; i++){
            grav[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
        }
        return grav;
    // The formula to calculate gravity is:
    // 6.67E-17 times the massOfPlanet divided by the radius of the planet squared
}
//print the gravity values to text file
public static void printToFile(double[] gravity)throws IOException
{
    // fill in code here
        PrintWriter outFile = new PrintWriter(new File("/Users/timothylee/gravity1.txt"));
        for(int i = 0; i < gravity.length; i++){
            outFile.println(gravity);
        }
        outFile.close();
    }

public static void main(String[] args)throws IOException
{
    // Initialize variables
    String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
    double[] radii = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
    double[] masses = {3.3022 * Math.pow(10,23), 4.8685 * Math.pow(10,24), 5.9736 * Math.pow(10,24), 6.4185 * Math.pow(10,23),
                1.8986 * Math.pow(10,27), 5.6846 * Math.pow(10,26), 8.6810 * Math.pow(10,25), 1.0243 * Math.pow(10,26), 1.312 *
                    Math.pow(10,22)};
    // or using big E notation:
    // double [] mass = {3.30E23, 4.87E24, 5.97E24, 6.42E23, 1.90E27, 5.68E26, 8.68E25, 1.02E26, 1.27E22}; // See IMACS double lesson for big E notation

    // Processing
    double[] gravities = calcGravity(radii, masses);

    // Output
    printResults(names, radii, masses, gravities);
    printToFile(gravities);

當您打印出不是原始數據類型的內容(對象,數組等)時,將打印數據結構的內存地址,而不是您要查找的內容。 對於數組,您要遍歷其所有元素,然后逐一打印。

對於對象,您需要為要打印的實例變量調用getter函數。 例如:

public class ExampleObject {
    private int x;
    public void setX(int x) { this.x = x; }
    public int getX() { return x; }

    public static void main(String[] args) {
        ExampleObject ex = new ExampleObject();
        ex.setX(5);
        System.out.println(ex); // prints the memory address of this ExampleObject
        System.out.println(ex.getX()); // call the getter for what you want to print.
    }
}

但是,如果要在運行命令System.out.println(ex)而不是內存地址時默認打印x,則可以覆蓋Object類的toString()方法,該方法被稱為獲取字符串的方法。要求打印對象時類的表示形式。 默認情況下,它返回對象的內存地址,這就是您看到的原因。

public class ExampleObject {
    private int x;
    public void setX(int x) { this.x = x; }
    public int getX() { return x; }
    public String toString() { return ""+x; }

    public static void main(String[] args) {
        ExampleObject ex = new ExampleObject();
        ex.setX(5);
        System.out.println(ex); // prints the string representation of ex, which is the value of x
    }
}

您正在打印重力數組,而不是在文件中打印重力元素。

當您將grav成員寫入文件時,將grav更改為grav[i]

代替

 outFile.println(gravity); // <-- prints the "value" of the gravity reference.

你可以試試

outFile.println(Arrays.toString(gravity)); // <-- prints the values contained in the gravity array

[D @ 642b6fc7代表對象在內存中的位置。 如果不更深入地查看代碼,則似乎需要嘗試打印對象的特定屬性,而不是對象本身。 您也可以嘗試覆蓋要打印的對象的toString()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM