Home > Software design >  calculate sum and median by importying csv file in java
calculate sum and median by importying csv file in java

Time:02-23

public class data {
    public static void main(String[] args) {       
        String fileName = "Book1.csv";// File name is book1.csv
        File file = new File(fileName); // read file
        try{
            Scanner inputStream = new Scanner(file);
            while (inputStream.hasNext()){  // ignore first line
                String data = inputStream.next();
                System.out.println(data);
            }
            inputStream.close();

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }}}
 

output

enter image description here

I want to calculate the sum of the values of followers. can you please help me

CodePudding user response:

You need to split String and transform to double. Like this:

double sum = 0;
while (inputStream.hasNext()){  // ignore first line
               String data = inputStream.next();
               
               double value = Double.valueOf(data.split(",")[1]); //[1] - number after ','
               sum = value;    
            }
System.out.println(sum);
  • Related