English 中文(简体)
我如何阅读我的两个阵列?
原标题:How do I get both of my arrays to be read in?

我有两阵列储存从档案中读到的两条线的数值。 在我的全球调查组进行一些处理之后,它应当表明在框架的中间点旁边站。 然而,现在只有一个迹象。 我以各种方式尝试了,我知道如何让对方站起来,但不要ice。 我的守则是:

public class PortraitFileReader 

    public static ArrayList<Drawable> readFile(File a) {
    File myFile;
    ArrayList<Integer> values = new ArrayList<Integer>();
    ArrayList<Drawable> couple = new ArrayList<Drawable>();
    Man aMan;
    Woman aWoman;
    Point aPoint;
    String input;
    String [] array = null;
    String [] array2 = null;
    try {
        myFile = a;
        Scanner inFile = new Scanner(myFile);
        input = inFile.nextLine();
        array = input.split(", ");

        while(inFile.hasNext()) {
            input = inFile.nextLine();
            array2 = input.split(", ");
            System.out.println(Arrays.toString(array2));        
        }

        if(array[0].equals("man")) {
            for(int i=1; i<array.length-1; i++) {
                    int current = Integer.parseInt(array[i]);
                    values.add(current);
                    System.out.println(values);
                }
                aPoint = new Point(values.get(0), values.get(1));
                aMan = new Man(aPoint, values.get(2), values.get(3), array[5]);
                couple.add(aMan);
                values.clear();


        }

        if(array[0].equals("woman")) {
            for(int i=1; i<array.length-1; i++) {
                int current = Integer.parseInt(array[i]);
                values.add(current);
                System.out.println(values);
            }
            aPoint = new Point(values.get(0), values.get(1));
            aWoman = new Woman(aPoint, values.get(2), values.get(3), array[5]);
            couple.add(aWoman);
            values.clear();
        }

        if(array2[0].equals("man")) {
            for(int i=1; i<array2.length-1; i++) {
                int current = Integer.parseInt(array[i]);
                values.add(current);
                System.out.println(values);
            }
            aPoint = new Point(values.get(0), values.get(1));
            aMan = new Man(aPoint, values.get(2), values.get(3), array2[5]);
            couple.add(aMan);
            values.clear();
        }

        if(array2[0].equals("woman")) {
            for(int i=1; i<array2.length-1; i++) {
                    int current = Integer.parseInt(array[i]);
                    values.add(current);
                    System.out.println(values);
                }
            aPoint = new Point(values.get(0), values.get(1));
            aWoman = new Woman(aPoint, values.get(2), values.get(3), array2[5]);
            couple.add(aWoman);
            values.clear();
        }
    }

    catch (FileNotFoundException e) {
        System.out.println("The file was not found.");
    }
    return couple;
}
}

这是我从档案中读到的数据:

man, 260, 100, 40, 80, Tom  
woman, 300, 100, 40, 80, Sally  

任何帮助都将受到高度赞赏。

NOTE: the system.out.println s are only there to test if each range have the right Value in it.

最佳回答

如果是妇女,则通过2阵列检查,你实际上正在穿透不同阵列。

 for(int i=1; i<***array2***.length-1; i++) {
int current = Integer.parseInt(****array***[i]);

当你检查2阵列是否为男子时,情况也是如此。

结果是,你正在处理第一线的内容两次。

问题回答

简言之,我几乎读了它,因此我改写:

public class PortraitFileReader {

public static ArrayList<Drawable> readFile(File file) {
    ArrayList<Drawable> couple = new ArrayList<Drawable>();
    try {
        Scanner scanner = new Scanner(file);
        while(scanner.hasNext()) {
            couple.add(parseLine(scanner.nextLine()));
        }
    }

    catch (FileNotFoundException e) {
        System.out.println("The file was not found.");
    }
    return couple;
}

public static Drawable parseLine(String line) {
    String [] array = line.split(", ");
    String gender = array[0];
    int pointX = Integer.parseInt(array[1]);
    int pointY = Integer.parseInt(array[2]);
    int width  = Integer.parseInt(array[3]);
    int height = Integer.parseInt(array[4]);
    String name = array[5];
    if(gender.equals("man")) {
        return new Man(new Point(pointX, pointY), width, height, name);
    } else {
        return new Woman(new Point(pointX, pointY), width, height, name);
    }
}

}

Anyway, it looks like either you re not drawing your drawable right, or the input in the file isn t exactly formatted as you expect. The parsing itself seems to make sense....
except that you re always parsing array, and not switching it to parseInt(array2[i]) in the 3rd and 4th block. Which just demonstrates why collapsing these cut and pasted blocks into one method is the sensible way to go.

一切照样照搬这些ed子:

....
        Scanner scanner = new Scanner(file);
        while(scanner.hasNext()) {
            String line = scanner.nextLine();
            Drawable person = null;
            // everything from parseLine, change return to assignment to person
            couple.add(person);
        }
....




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签