从 java 中的文件中获取两个连续的值

getting two consicutive value from file in java

我正在处理一个 csv 文件,我想从每一行的特定位置提取两个值。

csv 输入文件如下所示:

a, b, c, d
12,32,45,76
23,45,77,56
32,34,49,28
73,92,26,68
73,36,77,26

例如我想要同时从每一行的第 3 个位置(c 列)开始的两个连续值,所以 (45, 77), (49, 26), (77, ???)...

获得这 2 个值后,我想对它们进行一些计算并存储它们 back.I 我正在研究 []2X1 大小矩阵乘法。因此,我一次需要两个连续的值。

package rotation.pkg45;import java.io.File;

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class Rotation45 {
    public static void main(String[] args) throws IOException {
        String filename = "bank-full2.csv";

        File file = new File(filename);
        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new FileWriter("bank2test1.csv"));     
            double a1 = 0.866025;
            double a2 = 0.5;
            double a3 = -0.5;
            double a4 = 0.866025;
            double b1;
            double b2;
            double c1;
            double c2;        

            Scanner inputStream = new Scanner(file);
            inputStream.next(); 
            Scanner inputStreamm = new Scanner(file);
            inputStreamm.next();         

            while (inputStreamm.hasNext()) {  
                String data = inputStreamm.next(); //read each line and store in data
                String[] values = data.split(","); //every line splited with " ; " and store each attribute in string list

                double first = Double.parseDouble(values[2]);


/*NoSuchElementException*/String data1 = inputStreamm.next(); //read comming nextline for second value and store in data1
                String[] values1 = data1.split(","); 
                //inputStream.next();         
                double second = Double.parseDouble(values1[2]);

                c1 = ((a2 * second) + (a1 * first));
                c2 = ((a3 * first) + (a4 * second));
                values1[2] = String.valueOf(c2);
                values[2] = String.valueOf(c1);
                StringBuilder sb = new StringBuilder();
                //String newData = sb.toString();
                for (int i = 0; i < values.length; i++) {
                    sb.append(values[i]);
                    if (i < values.length - 1) {
                        sb.append(",");
                    }
                }
                sb.append("\n");
                for (int i = 0; i < values1.length; i++) {
                    sb.append(values1[i]);
                    if (i < values.length - 1) {
                        sb.append(",");
                    }
                }
                //get the new string
                //System.out.println(sb.toString());

                writer.write(sb.toString()+"\n");
            }
            writer.close();
            inputStreamm.close();
        } catch(FileNotFoundException ex) {
            Logger.getLogger(Rotation45.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我在代码中提到时遇到类似 nosuchelement 异常的错误...

我通过使用 BufferedReader 而不是扫描仪来读取数据解决了这个问题。由于您的代码试图通过扫描仪读取整行然后将其拆分,因此您可以使用 BufferedReader 并调用其 readLine() 方法以更简单的方式完成此操作。

因此,我对您的代码所做的更改只是通过使用 BufferedReader 来替换扫描仪的使用。 我没有检查计算,但在我看来它工作正常。

这是我得到的结果的图像:

此外,由于您的 while 循环基于扫描仪,因此我更改了 too.The 我正在使用的 while 循环检查两行是否为空(在评估 while 条件的同时读取它们)

关于您的代码的最后一点是,在 BufferedWriter 上,您有一个 newLine() 方法,该方法插入了一个可移植的等价行保持(代码中的 \n

这是代码。执行它并检查它是否工作正常。

public class Rotation45 {
    public static void main(String[] args) throws IOException {
        String filename =     "bank-full2.csv";

        File file = new File(filename);
        BufferedWriter writer = null;
        BufferedReader reader = null;

        try {

            reader = new BufferedReader(new FileReader(file));
            writer = new BufferedWriter(new      FileWriter("bank2test1.csv"));     
            double a1 = 0.866025;
            double a2 = 0.5;
            double a3 = -0.5;
            double a4 = 0.866025;
            double b1;
            double b2;
            double c1;
            double c2;        

            String line1= null;
            String line2 = null;
            // Skeeps the head line
            reader.readLine();

            String currentLine = null;

            while (  ((line1 = reader.readLine())!= null) && ((line2 =     reader.readLine()) != null)       ) {

                String[] values = line1.split(","); 

                double first = Double.parseDouble(values[2]);

                String[] values1 = line2.split(","); 

                double second = Double.parseDouble(values1[2]);

                c1 = ((a2 * second) + (a1 * first));
                c2 = ((a3 * first) + (a4 * second));
                values1[2] = String.valueOf(c2);
                values[2] = String.valueOf(c1);
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < values.length; i++) {
                    sb.append(values[i]);
                    if (i < values.length - 1) {
                        sb.append(",");
                    }
                }
                sb.append("\n");
                for (int i = 0; i < values1.length; i++) {
                    sb.append(values1[i]);
                    if (i < values.length - 1) {
                        sb.append(",");
                    }
                }

                writer.write(sb.toString());
                writer.newLine();
            }
            writer.flush();
            writer.close();
            reader.close();

        } catch(FileNotFoundException ex) {
            Logger.getLogger(Rotation45.class.getName()).log(Level.SEVERE,      null, ex);
        }
    }
}

您的代码执行以下操作:

inputStream.next();
while (inputStreamm.hasNext()) {               
   inputStreamm.next();
   ...
   inputStreamm.next();
   ...
}

这意味着它将执行奇数次读取 (2*n + 1)。但是您的输入显然包含偶数的数据。这就是你得到例外的原因。

不清楚你想如何处理 (77, ???) 的情况,但要检测它你应该在循环中再次调用 hasNext()

inputStream.next();
while (inputStreamm.hasNext()) {               
   inputStreamm.next();
   ...
   if(inputStreamm.hasNext())
       inputStreamm.next();
       ...
   else
       //take care of special case
   ...
}