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

1. Write a method called getNameArray that takes an integer parameter (numNames)

ID: 3824637 • Letter: 1

Question

1. Write a method called getNameArray that takes an integer parameter (numNames), returns an array of Strings,

and is declared to throw an Exception. If the parameter passed is a negative value, throw

an Exception. Otherwise, loop numNames times prompting the user to enter another name

each pass through the loop. Return this array of names.

2. Write a method called writeNames that takes two parameters: an array of Strings (nameArray) and

a String (fileName), and returns nothing. Write the array of Strings (names) to a text file

named using the fileName parameter. Make sure to catch any Exception that

might be thrown.

3. Write a method called readNames that takes a String parameter (fileName) and

returns an ArrayList of Strings (nameArrayList). The method reads the text file (fileName) and

populates the ArrayList with an element for each line in the file. The method returns the

populated ArrayList. Make sure to catch any Exception that

might be thrown.

4. Write a method called printNames that takes an ArrayList of Strings and prints

the contents of the ArrayList to the command line, each on a separate line.

4. Call each of the methods above from the main method.

The pseudocode for the main method is as given below.

Make sure to catch any Exception that

might be thrown.

    Call getNameArray passing the size of the array (any integer) that you want returned

    Pass the array and a file name to the writeNames method

    Pass the file name to the readNames method and return the ArrayList

    Pass the ArrayList to the printNames method.

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


public class cClass {
   /*getNameArray*/
public static String[] getNameArray(int n)
{
  
   if(n<0)
   {
       throw new ArithmeticException("not valid");
   }
   String[] s = new String[n];
   for(int i=0;i<n;i++)
   {
       System.out.println("Enter another name");
       Scanner sc=new Scanner(System.in);
       s[i]=sc.next();
   }
   return s;
}
/*writeNames*/
public static void writeNames(String[] s, String fileName)

{
   try{
       PrintWriter writer = new PrintWriter(fileName, "UTF-8");
       Scanner sc=new Scanner(System.in);
       for (String string : s) {
               writer.println(s);
           }
       writer.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
}
/* Read Names*/
public static ArrayList readNames(String fileName)
{
   BufferedReader br = null;
       FileReader fr = null;
       ArrayList arr = new ArrayList();

       try {

           fr = new FileReader(fileName);
           br = new BufferedReader(fr);

           String sCurrentLine;

           br = new BufferedReader(new FileReader(fileName));

           while ((sCurrentLine = br.readLine()) != null) {
              
               arr.add(sCurrentLine);
           }

       } catch (IOException e) {

           e.printStackTrace();

       } finally {

           try {

               if (br != null)
                   br.close();

               if (fr != null)
                   fr.close();

           } catch (IOException ex) {

               ex.printStackTrace();

           }

       }
       return arr;
}
/* Print Names*/

public static void printNames(ArrayList arr)
{
   for(int i=0;i<arr.size();i++)
   {
       System.out.println(arr.get(i));
   }
}/* Main Method*/
public static void main(String[] args) {
   getNameArray(5);
   String names[] ={"RAM","ROM","GOD"};
   writeNames(names,"hello.txt");
   ArrayList arr = new ArrayList();
   arr= readNames("HeyBaby.txt");
   printNames(arr);
  
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote