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

Could some one help me figure this out? We are using Eclipse so would need to be

ID: 3589920 • Letter: C

Question

Could some one help me figure this out? We are using Eclipse so would need to be done in Eclipse. Thanks !

COSC 1174 – Java II Lab

Chapter 12 – Exception Handling and Text File I/O

Write a Java program that meets the following requirements:

Creates an array of 100 randomly chosen integers between 0 and 999

Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message “Out of Bounds”.

Use a try-catch block to detect when the IndexOutOfBoundsException error occurs.

Write a Java program that prompts the user for a file name, then opens the file and counts the number of characters, words, and lines in the file. Words are separated by whitespace characters. Have your program check to make sure the file exists before opening it and to check for FileIOException errors using a try-catch block. Create your own data file to process.

Write a program named FileMkdir that prompts the user to enter a directory name and creates a directory using the File class mkdirs method. The program displays the message “Directory created successfully” if a directory is created or “Directory already exists” if the directory already exists.

Write a program named CardinalSearch that reads the home page of Lamar University (www.lamar.edu) and counts the number of times the word “cardinal” appears on the page. Use the program in Listing 12.17 on page 483 of your text as a model.

LISTING 12.17 ReadFileFromURL.java 1 import java.util.Scanner 3 public class ReadFileFromURL 4public static void main(String[] args) f System.out.print( Enter a URL:"); String URLString new Scanner (System.in).next 6 7 enter a URL try Java.net . URL ur1 new Java .net.URL(URLString); int count = 0; Scanner input = new Scanner(ur1.openStream()); while Cinput.hasNextO) create a URL object 10 create a Scanner obiect more to read? read a line 12 13 14 15 16 17 18 19 20 21 st ring line = 1nput.nextLine(); count += 11 ne. length(); System.out.println("The file size is "count"characters"); catch (java.net.MalformedURLException ex) MalformedURLException System.out.println("Invalid URL); catch (java.io.IOException ex) 5 IOException 23 24 25 26 System.out.println("I/0 Errors: no such file");

Explanation / Answer

Creates an array of 100 randomly chosen integers between 0 and 999. Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message “Out of Bounds”.Use a try-catch block to detect when the IndexOutOfBoundsException error occurs.

package chegg;

import java.util.Random;
import java.util.Scanner;

public class ArrayRandom {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int a[] = new int[100];
       Random rand = new Random();
       for (int j = 0; j < 100; j++)
       {
            int pick = rand.nextInt(999);
            a[j] = pick;
            //System.out.println(pick);
       }
      
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the index of the array");
       int i = sc.nextInt();
       try{
           System.out.println("Element of array at index "+i+" is "+a[i]);
       }catch(IndexOutOfBoundsException ie){
           System.out.println("Out of Bounds");
       }
   }

}

Write a Java program that prompts the user for a file name, then opens the file and counts the number of characters, words, and lines in the file. Words are separated by whitespace characters. Have your program check to make sure the file exists before opening it and to check for FileIOException errors using a try-catch block. Create your own data file to process.

package chegg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileCount {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       String filename;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a file name");
       filename = sc.nextLine();
       File file = new File(filename);
       try(Scanner sc2 = new Scanner(new FileInputStream(file))){
            int count=0,a=0;
            while(sc2.hasNext()){
                sc2.next();
                count++;
            }
            Scanner sc3 = new Scanner(new FileInputStream(file));
            while(sc3.hasNext()){
               a++;
               sc3.nextLine();
            }
       System.out.println("Number of words: " + count);
       System.out.println("Number of lines: " + a);
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

}

Write a program named FileMkdir that prompts the user to enter a directory name and creates a directory using the File class mkdirs method. The program displays the message “Directory created successfully” if a directory is created or “Directory already exists” if the directory already exists.

package chegg;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class FileMkdir {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       makeDir();
   }

   public static void makeDir()
   {
       String str;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a directory name and creates a directory");
       str = sc.nextLine();
      
       File directory = new File(str);
      
        if (directory.exists() && directory.isFile())
        {
            System.out.println("The dir with name could not be" +
            " created as it is a normal file");
        }
        else
        {
            if (!directory.exists())
           {
                directory.mkdir();
           }
           String username = System.getProperty("user.name");
           String filename = " path/" + username + ".txt"; //extension if you need one
        }
   }
}

Write a program named CardinalSearch that reads the home page of Lamar University (www.lamar.edu) and counts the number of times the word “cardinal” appears on the page.

package chegg;

import java.util.Scanner;

public class ReadFileFromURL {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println("Enter a URL ");
       String URLString = new Scanner(System.in).next();
       try{
           java.net.URL url = new java.net.URL(URLString);
           int count = 0;
           Scanner input = new Scanner(url.openStream());
           while(input.hasNext()){
               String line = input.nextLine();
               count += line.length();
           }
           System.out.println("The file size is "+count+" characters");
       }catch(java.net.MalformedURLException ex){
           System.out.println("Invalid URL");
       }catch (java.io.IOException e) {
           // TODO: handle exception
           System.out.println("I/O errors: no such file");
       }
   }

}