使用.txt文件中的标准输入填充多个数组

时间:2017-11-09 02:14:45

标签: java arrays stdin

我一直在努力用文本文件中的不同类型的数据填充我的数组。此文本文件如下所示:

N String String
双双
双双
等N行双打
N String String
  冲洗并重复

例如:

  120 Chicago Illinois  
  34.457832 78.294756  
  34.452948 78.198347
  etc.  

这是我到目前为止的代码,它是原始的,都在主体中。 A = 17且B = 10632.因为总共有10649行,其中17行是整数字符串字符串行,其余的是x和y坐标

//    Read over entire file
    while (!StdIn.isEmpty()){

      String line = StdIn.readLine();
      String[] split = line.split(" ");
//      System.out.print(split.length);
//      if (Character.isDigit(line.charAt(0))){  // Didnt work as to both the integer N and double are a digit 0-9
      if (split.length >= 3){
        for(int i = 0; i < A;i++){
          intArray1[i] = Integer.parseInt(split[0]);
          stringArray1[i] = split[1];
          stringArray2[i] = split[2];
        }
      } else{
        for(int i = 0;i < B; i++){
          xcoords[i] = Double.parseDouble(split[0]);
          ycoords[i] = Double.parseDouble(split[1]);
        }}  
      }

这个想法是,如果由StdI.readLine和split创建的行的长度是3;它必须是.txt文件中的一行,以整数N后跟两个字符串开头   然后我们应该将这3个值存储在3个匹配类型的数组中。

否则该行的长度应为2,仅包含2个双值并分别存储。

3 个答案:

答案 0 :(得分:0)

我想知道你的数组和索引是什么,它只会保存最新的数组和索引,除非你有一个其他的索引并使用它。

//Read over entire file
while (!StdIn.isEmpty()){
    String line = StdIn.readLine();
    String[] split = line.split(" ");
    if (line.contains(".")) {       
        //It is the double double lines
        for(int i = 0; i < 3; i++){
            intArray1[i] = Integer.parseInt(split[0]);
            stringArray1[i] = split[1];
            stringArray2[i] = split[2];
        }
    }else {
        //It is the int String String lines
        for(int i = 0;i < B; i++){
            xcoords[i] = Double.parseDouble(split[0]);
            ycoords[i] = Double.parseDouble(split[1]);
        }
    }
}

我会告诉你我这样做的方法:

//Create List to store all the useful information
List<IntStringObj> intStrList = new ArrayList<IntStringObj>;
List<DoubleObj> doubleList = new ArrayList<DoubleObj>;

//Read over entire file
while (!StdIn.isEmpty()){
    String line = StdIn.readLine();
    String[] split = line.split(" ");
    if (line.contains(".")) {       
        //It is the double double lines
        for(int i = 0; i < 3; i++){
            IntStringObj tempIntStrObj = new IntStringObj();
            tempIntStrObj.setInterger(Integer.parseInt(split[0]));
            tempIntStrObj.setStr1(split[1]);
            tempIntStrObj.setStr2(split[2]);
            intStrList.add(tempIntStrObj);
        }
    }else {
        //It is the int String String lines
        for(int i = 0;i < B; i++){
            DoubleObj tmpDoubleObj = new DoubleObj();
            tmpDoubleObj.setDouble1(Double.parseDouble(split[0]));
            tmpDoubleObj.setDouble2(Double.parseDouble(split[1]));
            doubleList.add(tmpDoubleObj);
        }
    }
}

//The classes can be stored outside/inside depends on your usage
class IntStringObj {
    private int interger;
    private String str1;
    private String str2;

    public int getInterger() {
        return interger;
    }

    public void setInterger(int interger) {
        this.interger = interger;
    }

    public String getStr1() {
        return str1;
    }

    public void setStr1(String str1) {
        this.str1 = str1;
    }

    public String getStr2() {
        return str2;
    }

    public void setStr2(String str2) {
        this.str2 = str2;
    }

}

class DoubleObj {
    private double double1;
    private double double2;

    public double getDouble1() {
        return double1;
    }

    public void setDouble1(double double1) {
        this.double1 = double1;
    }

    public double getDouble2() {
        return double2;
    }

    public void setDouble2(double double2) {
        this.double2 = double2;
    }

}

答案 1 :(得分:0)

由于数据库似乎非常一致,所以你应该能够解决大部分问题。我会处理它有点像这样:

    List<City> cities =  new ArrayList<>();

    while (!StdIn.isEmpty()){

        String line1 = StdIn.readLine();
        String line2 = StdIn.readLine();
        String line3 = StdIn.readLine();

        String[] arr1 = line1.split(" ");
        String[] arr2 = line1.split(" ");
        String[] arr3 = line1.split(" ");

        int cityNum = arr1[0];
        String cityName = arr1[1] + " " + arr1[2];

        double[] xcoords = new double[2];
        double[] ycoords = new double[2];

        xcoords[0] = Double.parseDouble(arr2[0]);
        xcoords[1] = Double.parseDouble(arr3[0]);

        ycoords[0] = Double.parseDouble(arr2[1]);
        ycoords[1] = Double.parseDouble(arr3[1]);

        City city = new City();
        //build city with info

        cities.add(city);
    }

我认为,只要你有理由确定格式保持不变,进行任何类型的测试以查看格式是无用的额外工作。

答案 2 :(得分:0)

看看代码在做什么...... while循环已遍历所有行,那么for循环的目的是什么?你在while循环中读取一行,然后for循环用相同行的副本填充所有数组元素。然后你重复下一行等,直到你到达最后一行,然后数组填充最后一行的副本。

1)如果你想维护结构,那么solution of PSo是合适的,除了我将一个DoubleObj数组作为IntStringObj类的成员,然后只创建一个{主方法中{1}}的{​​1}}。同时删除for循环,例如:

ArrayList

2)如果您只想要一个坐标列表,以及一个单独的整数列表+城市/州:

假设每个城市/州的N都不同,那么最好使用IntStringObj,因为需要更多空间,动态调整自身大小。

// Create List to store all the useful information
List<IntStringObj> intStrList = new ArrayList<>();

IntStringObj tempIntStrObj = null;
int index = 0;
// Read over entire file
while (!StdIn.isEmpty()) {
    String line = StdIn.readLine();
    String[] split = line.split(" ");
    if (!line.contains(".")) {
        tempIntStrObj = new IntStringObj();
        index = 0;
        tempIntStrObj.setInteger(Integer.parseInt(split[0]));
        tempIntStrObj.setStr1(split[1]);
        tempIntStrObj.setStr2(split[2]);
        intStrList.add(tempIntStrObj);
    } else {
        DoubleObj tmpDoubleObj = new DoubleObj();
        tmpDoubleObj.setX(Double.parseDouble(split[0]));
        tmpDoubleObj.setY(Double.parseDouble(split[1]));
        tempIntStrObj.setCoords(index++, tmpDoubleObj);
    }
}
// print to console
/*
for (IntStringObj iso : intStrList) {
    System.out.println(iso.getInteger() + " " + iso.getStr1() + " " + iso.getStr2());
    for (int i = 0; i < iso.getInteger(); i++) {
        System.out.println(iso.getCoords(i).getX() + ", " + iso.getCoords(i).getY());
    }
}
*/

// The classes can be stored outside/inside depends on your usage
class IntStringObj {
    private int integer;
    private String str1;
    private String str2;
    private DoubleObj[] coords;

    public int getInteger() {
        return integer;
    }

    public void setInteger(int integer) {
        this.integer = integer;
        coords = new DoubleObj[integer];
    }

    public String getStr1() {
        return str1;
    }

    public void setStr1(String str1) {
        this.str1 = str1;
    }

    public String getStr2() {
        return str2;
    }

    public void setStr2(String str2) {
        this.str2 = str2;
    }

    public DoubleObj getCoords(int index) {
        return coords[index];
    }

    public void setCoords(int index, DoubleObj coords) {
        this.coords[index] = coords;
    }
}

class DoubleObj {
    private double x;
    private double y;

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

}