读取文件并以特定格式存储字符串以及数组中的数值

时间:2013-03-14 15:39:53

标签: java

我有一个名为file.txt的输入文件,内容为

Zone
Diameter    5
-321    -560    5811
-209    -580    2935
-27     -409    3580
-1075   -220    2847
-1045   206     2827
-19     410     3570
-205    570     2929
-310    565     5768
2.0     3.0     2.0
Zone
Diameter    50
4379    461     23390
484     -264    13447
1259    -516    11969
182     -669    9530
1366    -931    12720
1553    2350    -18702
-3458    6112   -24596
-3606   -6347   -24995
1491    -2267   -18694
1315    835     12740
193     631     9525
12      1252    490     11942
13      520     211     13311
14      4308    -174    23624

我正在寻找输出:

    zone=1
    x=[1.0 2.0 1.0 2.0 1.0.......2.0 1.0]
    similarly y=[]
    z=[]
    then
    zone=2
    x=[5.0 25.0 15 25 41 25 16 25]
    similarly y=[]
    z=[]

但是,我从zone = 1开始得到区域= 2的x = []值,因为我使用的是x.add(scanner.nextDouble())。

请帮帮我。提前谢谢。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boltcal1103;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Neha
 */
public class NewMain {
private static ArrayList<Double> x;
private static ArrayList<Double> y;
private static ArrayList<Double> z;
private static String next1;

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
    // TODO code application logic here
    //File file1 = new File("file.txt");

    Scanner scanner = new Scanner(new FileReader("file1.txt"));
    String line=null,word="Zone",next = null, next1=null;
    int count=1;
    while(scanner.hasNext()) {
        line = scanner.nextLine();
        if (line.equals(word)) {
            if (count > 1) {
                System.out.println(x);
                System.out.println(y);
                System.out.println(z);
            }
            //    int b;
            //  String next1;
            if(scanner.hasNextLine()){
                String nextLine1 = scanner.next();
                System.out.println("name :"+nextLine1);
                //   scanner.useDelimiter("\t");
                if(scanner.hasNext())
                {

                    //   scanner.useDelimiter(" ");
                    next1 = scanner.next();
                    System.out.println("value of diameter:"+next1);
                    //b = Integer.parseInt(scanner.next());
                    //System.out.println("value of diameter:"+b);
                    //      scanner.useDelimiter("\n");
                    scanner.nextLine();
            }}




            System.out.println("zone=:"+count);
            count++;
            x = new ArrayList<Double>();
            y = new ArrayList<Double>();
            z = new ArrayList<Double>();
        }


        else {
            // String delims="";
            String[] d = line.split("\t");
            //         System.out.println("zone=:"+d);

            x.add(Double.parseDouble(d[0]));
            y.add(Double.parseDouble(d[1]));
            z.add(Double.parseDouble(d[2]));
            //      System.out.println("zone=:"+count);
            //  System.out.println(d[0]);
            //  System.out.println(d[1]);
            //  System.out.println(d[2]);
            int i;
            for(i=0;i<x.size();i++)
            {
                Double get1 = x.get(i);
                // System.out.println("value of x:"+get1);
                Double get2 = y.get(i);

                Double get3 = z.get(i);
                double x1 = get1*get1;
                double y1 = get2*get2;
                double z1 = get3*get3;
                System.out.println("x1:"+x1+"\ty1:"+y1+"\tz1:"+z1);


        } }

    }

    if (count > 1) {
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
    }
}

1 个答案:

答案 0 :(得分:1)

你可以移动

List<Double> x = new ArrayList<Double>();   //Defining double arraylist for Fx
List<Double> y = new ArrayList<Double>();   //Defining double arraylist for Fy
List<Double> z = new ArrayList<Double>();   //Defining double arraylist for Fz
在你的while循环中

,因此列表将在每个区域后重置。

编辑由于您希望支持任意大小的区域,请将while循环设为:

while(scanner.hasNext()) {
    line = scanner.next();
    if (next.equals(word)) {
        if (count > 1) {
            System.out.println(x);
            System.out.println(y);
            System.out.println(z);
        }

        System.out.println("zone=:"+count);
        count++;
        x = new ArrayList<Double>();
        y = new ArrayList<Double>();
        z = new ArrayList<Double>();
    } else {
        String[] d = line.split(' ');
        x.add(Double.parseDouble(d[0]);
        y.add(Double.parseDouble(d[1]);
        z.add(Double.parseDouble(d[2]);
    }
}

if (count > 1) {
    System.out.println(x);
    System.out.println(y);
    System.out.println(z);
}