Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How would I change this to have number of columns change with the text file inst

ID: 3884836 • Letter: H

Question

How would I change this to have number of columns change with the text file instead of having it at a definite 1000? I need read from a text file and then keep track of what the longest rows length is.

import java.util.*;

import java.io.*;

public class Fitness{

    public static void main(String[] args)throws Exception{

        try{

            

            Scanner sc=new Scanner(System.in);

            System.out.print("Enter name of the file(with .txt ext)::");

            String fileName=sc.nextLine();

            FileInputStream file = new FileInputStream(fileName);

            BufferedReader br = new BufferedReader(new InputStreamReader(file));

            double values[][]=new double[7][1000];

            for(int i=0;i<7;i++){

                for(int j=0;j<1000;j++){

                    values[i][j]=-1;

                }

            }

        String strLine;


        int currentRow=0;

        while ((strLine = br.readLine()) != null){


            String s[]=strLine.split(" ");


            for(int i=0;i<s.length;i++){

                values[currentRow][i]=Integer.parseInt(s[i]);

            }

            currentRow++;

            System.out.println("Current row : " + currentRow);

        }//while loop ends here

System.out.println("2D Array:");

for(int i=0;i<7;i++){

            for(int j=0;j<1000;j++){

if(values[i][j]==-1){

                    break;

}

else{

                    System.out.print(values[i][j]+" ");

                    System.out.print(values [1][2]);

                }

            }

            System.out.println();

}

        br.close();

        sumByColumns(values);

        }

        catch(FileNotFoundException e){

            System.out.println("No such file");

        }

        

        

    }

Explanation / Answer

The current 2-D array can be converted into an array of size 7 but whose elements are arraylists of doubles. It will facilitate to as many elements at each index of the array as we want. We just need to add an element at the arraylist at corresponding index in the array. You will also need to change the input datatype of your method sumByColumns from 2D double array to array of double arraylist as shown below. The modified code is given below.

Java code:

import java.util.*;
import java.io.*;
public class test{
public static void main(String[] args)throws Exception{
  try{
   Scanner sc=new Scanner(System.in);
   System.out.print("Enter name of the file(with .txt ext)::");
   String fileName=sc.nextLine();
   FileInputStream file = new FileInputStream(fileName);
   BufferedReader br = new BufferedReader(new InputStreamReader(file));

   ArrayList<Double>[] values = (ArrayList<Double>[])new ArrayList[7];

   for(int i=0;i<7;i++){
    for(int j=0;j<values[i].size();j++){
     values[i] = new ArrayList<>();
    }
   }

   String strLine;
   int currentRow=0;
   while ((strLine = br.readLine()) != null){


    String s[]=strLine.split(" ");


    for(int i=0;i<s.length;i++){
     values[currentRow].add(Double.parseDouble(s[i]));
    }
    currentRow++;
    System.out.println("Current row : " + currentRow);
   }//while loop ends here
   System.out.println("2D Array:");
   for(int i=0;i<7;i++){
    for(int j=0;j<values[i].size();j++){
     if(values[i].get(j)==-1){
      break;
     }
     else{
      System.out.print(values[i].get(j)+" ");
      System.out.print(values[1].get(2));
     }
    }
    System.out.println();
   }
   br.close();
   sumByColumns(values);
  }
  catch(FileNotFoundException e){
   System.out.println("No such file");
  }
}

private static void sumByColumns(ArrayList<Double>[] values) {
   // sumByColumns method implementation goes here

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote