如何在Java中读取文件并将特定的浮点数值设置到特定的数组中?

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

How do you read a file and set specific float values to a specific array in java?

问题

以下是翻译好的部分:

假设我有一个文件设置如下:

20 88.6

16 71.6

19.8 93.3

18.4 84.3

17.1 80.6

15.5 75.2

14.7 69.7

17.1 82

两个浮点数由空格分隔,假设左边的是 x 值,右边的是 y 值,那么如何读取文件,并将 x 值存储在一个数组中,将 y 值存储在另一个数组中?

public static Float[] getData() throws FileNotFoundException {
    float data;
    List<Float> xValues = new ArrayList<Float>();
    List<Float> yValues = new ArrayList<Float>();
    
    Scanner inFile = new Scanner(new File("cricket"));
    while (inFile.hasNext()) {
        data = inFile.nextFloat();
        xValues.add(data);
        
        data = inFile.nextFloat();
        yValues.add(data);
    }
    inFile.close();
    
    // 转换为数组
    Float[] xArray = xValues.toArray(new Float[0]);
    Float[] yArray = yValues.toArray(new Float[0]);
    
    // 返回数组
    return xArray;
}

这是我目前的代码,我已经将所有的值存储在一个单独的数组中,我不确定如何将它们分割开。

英文:

Say I had a file set up like this:

20 88.6

16 71.6

19.8 93.3

18.4 84.3

17.1 80.6

15.5 75.2

14.7 69.7

17.1 82

The two float values are separated by a space, say that the ones on the left are x values and the ones on the right are y values how would I read the file and store the x values in one array and the y values in another?

public static Float[] getData() throws FileNotFoundException {
	float data;
	List&lt;Float&gt; temps = new ArrayList&lt;Float&gt;();

	Scanner inFile = new Scanner(new File(&quot;cricket&quot;));
	while(inFile.hasNext()) {
		
		data = inFile.nextFloat();
		temps.add(data);
		
	}
	inFile.close();
	
	for(int i = 0; i&lt;=temps.size(); i++) {
	Float[] tempsArray = temps.toArray(new Float[0]);
	
	}
	return tempsArray;
	
	
}

This is my code so far, I have all of the values in a single array, I'm not sure how to split them up.

答案1

得分: 1

你可以将每行的内容封装为类型为Data的对象,并将它们添加到一个ArrayList中。然后,你可以逐个读取Data类型的ArrayList中的每个元素,并将其添加到两个数组arrXarrY中,以供后续使用。请参考下面的示例代码。

public class Main {
   public static void main(String[] args) throws FileNotFoundException {    
      List<Data> dataList = getData("cricket");
      final int size = dataList.size();
      float[] arrX = new float[size];
      float[] arrY = new float[size];
      int i = 0;
      for (Data data : dataList) {
         arrX[i] = data.x;
         arrY[i] = data.y;
         i++;
      }
      // 现在你可以使用 arrX 和 arrY 数组
   }

   public static List<Data> getData(String file) throws FileNotFoundException {
      List<Data> dataList = new ArrayList<>();
      try (Scanner sc = new Scanner(new File(file))) {
         while (sc.hasNext()) {
            dataList.add(new Data(sc.nextFloat(), sc.nextFloat()));
         }
      }
      return dataList;
   }

   static class Data {
      float x;
      float y;

      public Data(final float x, final float y) {
         this.x = x;
         this.y = y;
      }
   }
}

注意:为了保持代码的可读性和格式,一些特殊字符(如&lt;&quot;)可能在这里没有被逐字翻译,但在实际使用时应该正确地使用尖括号和引号。

英文:

You can encapsulate the content of each line as an object of type Data and add them to an ArrayList. Then, you can read each element of the ArrayList of Data and add it to two arrays: arrX and arrY, which you can use. See the example below.

public class Main {
   public static void main(String[] args) throws FileNotFoundException {    
      List&lt;Data&gt; dataList = getData(&quot;cricket&quot;);
      final int size = dataList.size();
      float[] arrX = new float[size];
      float[] arrY = new float[size];
      int i = 0;
      for (Data data : dataList) {
         arrX[i] = data.x;
         arrY[i] = data.y;
         i++;
      }
      // now you can use the arrX and arrY arrays
   }

   public static List&lt;Data&gt; getData(String file) throws FileNotFoundException {
      List&lt;Data&gt; dataList = new ArrayList&lt;&gt;();
      try (Scanner sc = new Scanner(new File(file))) {
         while (sc.hasNext()) {
            dataList.add(new Data(sc.nextFloat(), sc.nextFloat()));
         }
      }
      return dataList;
   }

   static class Data {
      float x;
      float y;

      public Data(final float x, final float y) {
         this.x = x;
         this.y = y;
      }
   }
}

答案2

得分: 0

如何使用分隔符字符设置文件?然后您可以使用 split() 函数将其分割,并将分割的每一侧添加到相应的数组中。

英文:

How about setting up the file with a delimiter character? Then you can do a split() and add each side of the split to the corresponding array.

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

发表评论

匿名网友

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

确定