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

//PLEASE EXPLAIN WHAT THIS CODE IS DOING LINE BY LINE _ INSERT COMMENTS JAVA PRO

ID: 3851391 • Letter: #

Question

//PLEASE EXPLAIN WHAT THIS CODE IS DOING LINE BY LINE _ INSERT COMMENTS JAVA PROGRAMMING

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

public class LincolnAddress {

public static void main(String[] args) {
  
  
System.out.println("President Abraham Lincoln's Gettysburg address");
URL url = null;
try {
url = new URL("http://cs.armstrong.edu/liang/data/Lincoln.txt");
  
} catch (MalformedURLException ex) {
System.out.println("Invalid url: " + ex.getMessage());
ex.printStackTrace();
System.exit(1);
}

int wordCount = 0;
String s = "";
try (Scanner input = new Scanner(url.openStream())) {

while (input.hasNext()) {
s += input.nextLine();
}

} catch (IOException ex) {
ex.printStackTrace();
}
String[] words = s.split("[ ,.]", 0);
words = removeEmptyStrings(words);
System.out.println("Total word count = " + words.length);

}

private static String[] removeEmptyStrings(String[] words) {

ArrayList<String> modifiedWords = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
if (words[i].length() != 0) {
modifiedWords.add(words[i]);
}
}

return modifiedWords.toArray(new String[modifiedWords.size()]);
}


}

Explanation / Answer

// This program is used to counts the number of words in President Abraham Lincoln's Gettysburg address from the given URL file

import java.io.IOException; // imports all classes that are described in java.io package
import java.net.MalformedURLException; // subclass of IOException to checked exception
import java.net.URL; //class represents URL that contain total method to handle URL in java
import java.util.ArrayList; // class gives adjustable-array & implements interface-list
import java.util.Scanner; //class represents a simple text scanner
public class LincolnAddress { //command used to declare public-class "LincolnAddress"
public static void main(String[] args) { //command represents static main method which has a return type value & String is a java's pre-defined class

System.out.println("President Abraham Lincoln's Gettysburg address"); // command used to print this "President Abraham Lincoln's Gettysburg address"
URL url = null;
// "try-catch" command that may used to throw exception
try {
// command used to create a URL object
url = new URL("http://cs.armstrong.edu/liang/data/Lincoln.txt");
} catch (MalformedURLException ex) {
System.out.println("Invalid url: " + ex.getMessage()); // code used to print output
ex.printStackTrace(); // code helps to trace the exception
System.exit(1); // code used to terminate the exception
}

int wordCount = 0; // code used to count words
String s = "";
// Open an input stream and create a Scanner object
try (Scanner input = new Scanner(url.openStream())) {

    //code helps to read words
while (input.hasNext()) {
s += input.nextLine();
//code ingest newline left-over
}
} catch (IOException ex) {
ex.printStackTrace();
// code helps to trace the exception
}
String[] words = s.split("[ ,.]", 0);
// command used to split the strings with condition
words = removeEmptyStrings(words);
   // command used to display total words count
System.out.println("Total word count = " + words.length);

}

// method param is String[] words that can be declare as a private
private static String[] removeEmptyStrings(String[] words) {
ArrayList<String> modifiedWords = new ArrayList<>();
// code used to adding value to arraylist
for (int i = 0; i < words.length; i++) { // loop control code
if (words[i].length() != 0) { //shows condition that word-length not equal to zero
modifiedWords.add(words[i]); // code used for modification of words
}
}
return modifiedWords.toArray(new String[modifiedWords.size()]);
// return the size of modified words
}

}

/////////////////////////////////////////////////////////////////////////////////

If you having any issue then feel free to comment me

Thankyou