从文本文件获取二维数组。

huangapple go评论75阅读模式
英文:

get 2D array from text file

问题

以下是翻译好的部分:

public static void main(String[] a) throws FileNotFoundException {
    File file = new File("district3.txt");
    Scanner scan = new Scanner(file);
    String b;
    String[] c;
    
    int r = 6;
    
    double[][] arr = new double[r][];
    
    while (scan.hasNextLine()) {
        // 将数字以字符串形式读取
        b = scan.nextLine();
        
        // 拆分字符串
        c = b.split(" ");
        
        // 将拆分的字符串转换为 double 类型并存入二维数组
        double[] row = new double[c.length];
        for (int i = 0; i < c.length; i++) {
            row[i] = Double.parseDouble(c[i]);
        }
        // 将当前行存入二维数组
        arr[r - c.length] = row;
    }
}
英文:

I'm trying to get the 2D array from the text file. So far I accessed the file and got all the numbers in the file, but all these numbers are string so I used split() and then convert it to double. How can convert this to double 2D array?

1.65 4.50 2.36 7.45 3.44 6.23

2.22 -3.24 -1.66 -5.48 3.46

4.23 2.29 5.29

2.76 3.76 4.29 5.48 3.43

3.38 3.65 3.76

2.46 3.34 2.38 8.26 5.34

This is what I have so far:

public static void main(String[] a) throws FileNotFoundException {

	 File file =  new File(&quot;district3.txt&quot;);

	 Scanner scan = new Scanner(file); 
	 String b; 
	 String[] c;
	 
	 int r = 6;
	 
	 double[][]arr = new double[r][];
	 
	 
	 while(scan.hasNextLine()) {
        //get number as String
		b = scan.nextLine();
         
        //split them 
		 c = b.split(&quot; &quot;);
		 
		 for(String i:c)
			System.out.println(Double.parseDouble(i) );
		 
	
		 
		 
	 } 
		 
}

答案1

得分: 2

将你的for循环内部的内容改为print,然后在你打印完所有数字后添加一个println

for(String i:c)
    System.out.print(i + " ");

System.out.println();

要存储数字,你可以按以下方式操作:

double[][] arr = new double[r][];

int i = 0;
while(scan.hasNextLine()) {

    // 获取数字作为字符串
    b = scan.nextLine();

    // 拆分它们
    c = b.split(" ");
    arr[i] = new double[c.length];
    for(int j = 0; j < c.length; ++j) {
        arr[i][j] = Double.parseDouble(c[j]);
        // 根据需要显示它们
        System.out.print(c[j] + " ");
    }
    System.out.println();
    ++i;
}
英文:

Change your for to print inline and then add a println after you've printed all the numbers:

for(String i:c)
            System.out.print(i + &quot; &quot;);

System.out.println()

To store the numbers you can do the following:

double[][]arr = new double[r][];

int i = 0;
while(scan.hasNextLine()) {

    //get number as String
    b = scan.nextLine();

    //split them 
    c = b.split(&quot; &quot;);
    arr[i] = new double[c.length];
    for(int j = 0; j &lt; c.length; ++j) {
        arr[i][j] = Double.parseDouble(c[j]);
        // Display them if needed
        System.out.print(c[j] + &quot; &quot;);
    }
    System.out.println();
    ++i;
 } 

huangapple
  • 本文由 发表于 2020年4月11日 03:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61147069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定