0

编辑:对于偶然发现这篇文章的人,我通过使用 ArrayList 而不是普通数组来解决我的问题 - 这样做减少了我的代码的三分之一,并使其大部分可重用。感谢所有在下面提供帮助的人。这是我为那些正在寻找的人更新的代码的链接:http: //pastebin.com/Yh3LVu2H

该程序旨在读取文件的行并将它们输出到两个数组 xAxis 和 yAxis - iv 将其拆分为两个文件,因为我将使用 ScreenSizes.java 来构建 GUI。

我在 "System.out.println("X: " + xAxis[index]);" 这一行遇到了异常

ScreenSizes.java 中的代码:

package screensizes;
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
public class ScreenSizes{    

    public String filePath = "/Users/jonny/Documents/UNI/ScreenSizes/xy.txt";

    public static void main(String[] args) throws FileNotFoundException
    {
    ScreenSizes obj = new ScreenSizes();
        obj.run();
    }

    public void run() throws FileNotFoundException {
        GetScreens data = new GetScreens(filePath);
        int noLines = data.countLines();
        int[] xAxis = data.getData('x');
        int[] yAxis = data.getData('y');
    int index = 0;
        while(index<noLines){
        System.out.println("X: " + xAxis[index]);
        index++;
        }
    }
}

来自 GetScreens.java 的代码

package screensizes;
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;

public class GetScreens
{

    public int lines;
    public String filePath = "";
    public int[] x = new int[lines];    
    public int[] y = new int[lines];

    public GetScreens(String aFileName) throws FileNotFoundException{
        fFile = new File(aFileName);
        filePath = aFileName;
        try
        {
        processLineByLine();
        countLines();
        }
        catch(FileNotFoundException fnfe)
        {
            System.out.println(fnfe.getMessage());
        }
        catch(IOException ioe)
        {
            System.out.println(ioe.getMessage());
        }
    }

    public final void processLineByLine() throws FileNotFoundException {
        //Note that FileReader is used, not File, since File is not Closeable
        Scanner scanner = new Scanner(new FileReader(fFile));
        try {
            while ( scanner.hasNextLine() ){
                processLine( scanner.nextLine() );
            }
        }
        finally {
            //ensure the underlying stream is always closed
            //this only has any effect if the item passed to the Scanner
            //constructor implements Closeable (which it does in this case).
            scanner.close();
        }
    }

    public int[] getData(char choice){
        if(choice == 'x'){
            return x;
        }
        else{
            return y;
        }
    }

    public void processLine(String aLine){
        //use a second Scanner to parse the content of each line 
        Scanner scanner = new Scanner(aLine);
        scanner.useDelimiter("x");
        if ( scanner.hasNext() ){
           for(int i=0; i<lines; i++){            
                x[i] = scanner.nextInt();
                y[i] = scanner.nextInt();
           }
        }
        else {
            log("Empty or invalid line. Unable to process.");
        }
    }

    public int countLines(){
        try
        {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            while (reader.readLine() != null) lines++;
            reader.close();
        }

        catch(FileNotFoundException fnfe)
        {
            System.out.println(fnfe.getMessage());
        }

        catch(IOException ioe)
        {
            System.out.println(ioe.getMessage());
        }
        return lines;
    }

    // PRIVATE 
    public final File fFile;

    private void log(Object aObject){
        System.out.println(String.valueOf(aObject));
    }

    private String quote(String aText){
        String QUOTE = "'";
        return QUOTE + aText + QUOTE;
    }
} 
4

5 回答 5

0

您需要根据文件中的记录数重新初始化int[] x和。int[] y

于 2012-06-11T05:18:58.837 回答
0

在您的GetScreens类中,实例变量lines被隐式初始化为0. 因此,您的xy数组的长度为零:没有元素。

因此,当您尝试在其他课程中使用它们时,您将获得ArrayIndexOutOfBoundsException.

初始化lines为有用的东西。

当你编译你的程序时,请留意以下警告:The variable _lines_ may not have been initialized.

于 2012-06-11T05:19:25.583 回答
0
public int[] x = new int[lines];    
public int[] y = new int[lines];

和以下一样好:

public int[] x = new int[0];    
public int[] y = new int[0];

在使用它们之前执行此操作:

x = new int[countLines()];    
y = new int[countLines()];

--V

于 2012-06-11T05:20:46.780 回答
0

@乔尼

在您给出的 processLine 方法中

for(int i=0; i< 行; i++)

{

            x[i] = scanner.nextInt();
            y[i] = scanner.nextInt();

}

但不明白你不能scanner.nextInt()到x [i]和y [i]背后的逻辑。分配x [i]和y [i]是主要问题

采用

           x[i] = Integer.parseInt(scanner.next());

           y[i] = Integer.parseInt(scanner.next());
于 2012-06-11T05:57:35.900 回答
0

如果必须使用数组,则必须先用 计算行数countLines(),然后用 初始化数组

 x = new int[lines];
 y = new int[lines];

然后你就可以真正开始处理了……

结果是这样的:

public GetScreens(String aFileName) throws FileNotFoundException{
    fFile = new File(aFileName);
    filePath = aFileName;
    try
    {
        countLines();
        x = new int[lines];
        y = new int[lines];
        processLineByLine();
    } catch(FileNotFoundException fnfe) {
        System.out.println(fnfe.getMessage());
    }
    catch(IOException ioe) {
        System.out.println(ioe.getMessage());
    }
}

但是这种代码/设计在以后使用时很可能会产生更多错误。我宁愿考虑为您的函数使用返回值。(例如,如果你执行countLines()两次,你最终会得到两倍于你在该文件中实际拥有的行......

我认为使用ArrayList会使您的代码更加安全。

于 2012-06-11T06:52:30.977 回答