1

我有个问题。我做了几个测试,发现我的程序在第一个 for 循环之后立即停止。我不知道为什么。我之前有一个较小的程序,它正在读取和写入一个文件,它工作得很好。程序编译并且 text.txt 文件位于正确的位置。

我正在使用 Bluej。需要任何其他细节吗?请问。

import java.io.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.lang.reflect.Array;
import java.util.Scanner;

import javax.swing.*;

public class fileStreamTest{

    public static void main(String args[]){
        float averageprice;
        float[] prevDataArray= new float[22];
        float[][] currentDataArray = new float[21][];
        float[] futureData= new float[7];

        Scanner keyboard = new Scanner(System.in);
        System.out.println("start");

        if (true)//prevDataArray[1]!=7
        {
            for (int xx = 0; xx == 20; xx++)
            {
                int n = 0;
                float input = 0;
                double q1, q3;

                //input data 
                System.out.println("Input data, input 0.00407 to finish.");
                while(input!=0.00407)
                {
                    if (input == 0)
                        input = keyboard.nextFloat();
                    currentDataArray[xx][n] = input;
                    input = keyboard.nextFloat();
                    n++;
                }

                //delete outliers
                n = 0; 

                if((int)(currentDataArray.length/2) == currentDataArray.length/2)
                {
                    if((int)((currentDataArray.length-currentDataArray.length/2)/2) == currentDataArray.length/4)
                    {
                        q1 = (currentDataArray[xx][currentDataArray.length/4]+currentDataArray[xx][currentDataArray.length/4+1])/2;
                        q3 = (currentDataArray[xx][3*currentDataArray.length/4]+currentDataArray[xx][3*currentDataArray.length/4+1])/2;
                    }
                    else
                    {
                        q1 = currentDataArray[xx][(int)(currentDataArray.length/4) + 1];
                        q3 = currentDataArray[xx][(int)(3*currentDataArray.length/4) + 1];
                    }
                }
                else
                {
                    if((int)((int)(currentDataArray.length/2)/2) == (int)(currentDataArray.length/2)/2)
                    {
                        q1 = (currentDataArray[xx][(int)(currentDataArray.length/2)/2] +  currentDataArray[xx][(int)(currentDataArray.length/2)/2 + 1])/2;
                        q3 = (currentDataArray[xx][3*(int)(currentDataArray.length/2)/2+1] + currentDataArray[xx][3*(int)(currentDataArray.length/2)/2 + 2])/2;
                    }
                    else
                    { 
                        q1 = currentDataArray[xx][(int)((int)(currentDataArray.length/2)/2) + 1];
                        q3 = currentDataArray[xx][(int)(3*(int)(currentDataArray.length/2)/2)+ 1];
                    }
                }
                System.out.println("q1 = " + q1 + "/n" + ",q3 = " + q3);
                float sum = 0;
                n = 0;
                while(n!=currentDataArray.length)
                {
                    if((currentDataArray[xx][n] >= q1) && (currentDataArray[xx][n] <= q3))
                    {
                        sum += currentDataArray[xx][n];
                        n++;   
                    }
                }
                //finding average price
                averageprice = sum/n;
                System.out.println(" Average price = " + averageprice);
                //write currentDataFile
                try{
                    OutputStream os = new FileOutputStream("C:/test.txt");
                    for(int x=0; x < currentDataArray[xx].length ; x++){
                        os.write( (byte)currentDataArray[xx][x] ); // writes the bytes
                    }
                    os.close();
                }catch(IOException e){
                    System.out.print("Exception");
                } 
            }
            prevDataArray[0]++;
        }
        else
        {
            //drawbg
            //drawList
            //createfuturedata(avgdev)
            //drawPred(futuredata)
            //updateavgdev
            System.out.println("test");
            prevDataArray[0] = 1;
        }

        //InputStream is = new FileInputStream("C:/test.txt");
        //int size = is.available();

        //  for(int i=0; i< size; i++){
        //     System.out.print((double)is.read() + "  ");
        // }
        //  is.close();

    }
}
4

1 回答 1

6

还没有完成你的整个代码,但问题肯定出在你的情况下。

for (int xx = 0; xx == 20; xx++)

这个循环应该是:-

for (int xx = 0; xx <= 20; xx++)

否则,它甚至不会运行一次。

另外,你的数组: -

float[][] currentDataArray = new float[21][];

需要列数的大小,否则NPE当您在行中访问它时会得到一个:

currentDataArray[xx][n] = input;
于 2012-11-18T19:59:25.480 回答