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

I need Help on this coding. Get the input file \"inputNumbers.txt\" (which has b

ID: 3633976 • Letter: I

Question

I need Help on this coding.

Get the input file "inputNumbers.txt" (which has been provided). Write a program to read in the first 50 numbers into one array, then read the next 50 numbers into a second array. Create a new third array that will contain the sum of the corresponding values of the first 2 arrays. Print: first array number, second array number and then the sum that is stored in the third array, print the second number stored in the first array, the second number in the second array, and the sum in the third array.

How to I read in the file and use it with my array? This is for a Java class using starting out with java book.

Explanation / Answer

Dear,

Here is the code

import java.util.Scanner;
import java.io.*;

public class SumArray
{
public static void main(String[] args)throws IOException
{
   String filename;      // File name
  
   int arr1[]=new int[50];
   int arr2[]=new int[50];
   int arr3[]=new int[50];
   // Create a Scanner object for keyboard input.
       Scanner keyboard = new Scanner(System.in);

       // Get the filename.
       System.out.print("Enter the filename: ");
       filename = keyboard.nextLine();

       // Open the file.
       FileReader freader = new FileReader(filename);
       BufferedReader inputFile = new BufferedReader(freader);
       // Read the first line from the file.
      int num = Integer.parseInt(inputFile.readLine());
          int i=0;
     
          System.out.print("Array1 ");
       while (i<50)
       {
          // Convert the String referenced by str
          // to a double and add it to sum.
          arr1[i]= num;       
          System.out.print(arr1[i]+" ");  
          num = Integer.parseInt(inputFile.readLine());
          i++;
       }
       num = Integer.parseInt(inputFile.readLine());
       i=0;
       while (i<50)
       {
          // Convert the String referenced by str
          // to a double and add it to sum.
          arr2[i]= num = Integer.parseInt(inputFile.readLine());;       
                
          i++;
       }
       for(int j=0;j<50;j++)
        arr3[j]=arr1[j]+arr2[j];
       //displaying array3
       System.out.print("Sum:");
       for(int j=0;j<50;j++)
       System.out.print(arr3[j]+" ");
      

       // Close the file.
       inputFile.close();
   }
}



Hope this will help you..

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