Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将CSV字符串转换为整数并添加它。存储TXT文件_Java_String_Csv_Integer - Fatal编程技术网

Java 如何将CSV字符串转换为整数并添加它。存储TXT文件

Java 如何将CSV字符串转换为整数并添加它。存储TXT文件,java,string,csv,integer,Java,String,Csv,Integer,你能帮我解决这个问题吗,我想添加2个来自CSV的整数并将其存储在txtfile中,但问题是它是字符串,如果我将其转换为整数,我会遇到很多错误。。谢谢你们 import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class CSVReader { public stat

你能帮我解决这个问题吗,我想添加2个来自CSV的整数并将其存储在txtfile中,但问题是它是字符串,如果我将其转换为整数,我会遇到很多错误。。谢谢你们

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {
public static void main (String[]arg)throws Exception {

    String readFile = "C:/Users/user/Desktop/student.csv";
    String writeFile = "C:/Users/user/Desktop/data.txt";

    // Read a comma-separated values (CSV) file.
    BufferedReader CSVFile = new BufferedReader (new FileReader(readFile));

    // Read line.
    String dataRow = CSVFile.readLine();

    // Create an array of student
    List<Student> students = new ArrayList <Student> ();


    // The while checks to see if the data is null. If
    // it is, we’ve hit the end of the file. If not,
    // process the data.
    while (dataRow !=null){
        String [] dataArray = dataRow.split(",");
        System.out.println(dataRow);
        Student student = new Student();
        student.setStudentName(dataArray[0]);
        student.setScore1(dataArray[1]);
        student.setScore2(dataArray[2]);
        students.add(student);
        dataRow = CSVFile.readLine();
        }

    // Close the file once all data has been read.
    CSVFile.close();

    StringBuilder sb = new StringBuilder();

    FileWriter fw = new FileWriter(writeFile);
    for (Student s : students){
        sb.append(s.studentName);
        System.out.println(s.studentName + " - " + (s.score1 + s.score2));

        // Writing to a text file.
        fw.write(sb.toString());
    }

    // Close the file once all data has been written.
    fw.close(); 
}

}
它应该br:

  che,cheche,chet
  100,100,100
  100,100,100
  che - 200
  cheche -200
  chet - 200
你可以试试

int number = Integer.parseInt("your string here");
例如:

   String one = "1";
   String two = "2";
   System.out.println(Integer.parseInt(one) + Integer.parseInt(two));

您在代码中犯了一些错误

  • 学生班级中的分数变量应为整数
  • 要将字符串转换为整数,需要使用Integer.parseInt 方法。理想情况下,你的转换应该在你 设置分数值
  • 为什么要将student对象添加到ArrayList。你不能吗 直接写入文本文件

  • 如果您提供的信息是正确的,那么您面临的主要问题是CSV数据采用的是列格式,而不是典型的行格式。我的意思是,第一行是name,下一行是分数。数据的每个“列”都与同一索引中的“标题”匹配

    您的示例数据:

    che, cheche, chet    -- row[0]
    100, 100,    100     -- row[1]
    100, 100,    100     -- row[2]
    
    因此,行[0]是名称,但您正在解析te数据,就好像行的第一项是名称,第二项和第三项是分数一样——基于此示例数据,情况并非如此

    如果您想要分数,您需要为每一行获取适当的索引,因此che将是第[1][0]行和第[2][0]行


    如果事实上是这样,那么您需要处理第一行以获得名称,然后您需要处理其余行以获得分数。

    如果您告诉我们哪些错误,这将非常有用。如何将字符串转换为整数?请尝试student.setScore1(integer.parseInt(dataArray[1])@Jayamohan我想试试,但不起作用。@OblTobl没有错误。。我关心的是System.out.println(s.studentName+“-”+(s.score1+s.score2))的输出;这是输出:“che,cheche,chet 100100 null-0 null-0”null-0这是用于添加两个整数值的。抱歉,误解了您的帖子;-)
    che, cheche, chet    -- row[0]
    100, 100,    100     -- row[1]
    100, 100,    100     -- row[2]