0

import java.util.Scanner; public class csv_demo { // private static final Exception ArrayIndexOutOfBoundsException = null;

public static void main(String[] args) throws ArrayIndexOutOfBoundsException, FileNotFoundException
    {
        //Get scanner instance
      Scanner scanner = new Scanner(new File("C:/Users/Glocation/Desktop/abc.csv"));
        //Set the delimiter used in file
        scanner.useDelimiter(",");

        int sum = 0, avg =0,max = 0,min = 0,a=0,b=0;

        System.out.println("N1|N2|Sum|Avg|Max|Min");

     try
     {
       while(scanner.hasNext())
       {    
           String line=scanner.nextLine();
           String[] arr=line.split(",");

           for(int k=0;k<arr.length-1;k++)
           {
               a=Integer.parseInt(arr[k]);
               b=Integer.parseInt(arr[k+1]);

               sum = a+b;

                avg= a+b/2;
                if(a>b) 
                {
                    max=a;
                    min=b;
                }
                else
                {
                    max=b;
                    min=a;
                }
                System.out.println(a+"  |"+b+"  |"+sum+"  |"+avg+"  |"+max+"  |"+min);
           }
            }
     }catch (Exception e)
     {    }
    }
 }

I am getting error in the line of converting string array to get value in variable "a". My CSV file is 1 2 3 4 6 7 9 2 5 7 5 6 8

shruti
  • 9
  • 2
  • Summing of infinite columns is an interesting problem. I suppose one would have to work out whether the values represented a convergent series, and then use series summing techniques to calculate the answer. – Boris the Spider Jan 25 '17 at 13:57
  • @BoristheSpider Where do you see the AIOOBE occurring? – Tom Jan 25 '17 at 14:04
  • shruti You have a CSV with whitespace delimited lines and yet you split that using ","? Does this make sense to you? – Tom Jan 25 '17 at 14:06
  • My csv file is already with column and row separated. Looking same like Excel file. row col = [1][1] = 8 [1][2]= 9 [2][1] = 10 [2][2]= 8 [3][1] = 5 [3][2]= 7 [3][3] = 8 [4][2]= 4. – shruti Jan 25 '17 at 14:11
  • You're trying to read "," delimited stuff and your CSV doesn't seem to contain such delimiters, so what do you expect is a possible solution? – Tom Jan 25 '17 at 14:14
  • Oh. I got your point. – shruti Jan 25 '17 at 14:19

0 Answers0