Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io . * ;import java.net. * ;import java.util.Scanner;import javafx.application. * ;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.chart.BarChart;import javafx.scene.chart.CategoryAxis;import javafx.scene.chart.NumberAxis;import javafx.scene.chart.XYChart;import javafx.scene.control. * ;import javafx.scene.layout.GridPane;import javafx.scene.paint.Color;import javafx.stage.Stage;import javax.net.ssl

import java.io.*;import java.net.*;import java.util.Scanner;import javafx.application.*;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.chart.BarChart;import javafx.scene.chart.CategoryAxis;import javafx.scene.chart.NumberAxis;import javafx.scene.chart.XYChart;import javafx.scene.control.*;import javafx.scene.layout.GridPane;import javafx.scene.paint.Color;import javafx.stage.Stage;import javax.net.ssl.HttpsURLConnection;public class Project226 extends Application { @Override public void start(Stage primaryStage){// create a Label for the first URL with text ("Enter URL 1"); Label L1= new Label("Enter URL 1: "); // create Label for the second URL with text ("Enter URL 2"); Label L2= new Label("Enter URL 2: "); // create a Text field for entering the first URL TextField text1= new TextField(); // create Text field for entering the second URL TextField text2= new TextField(); // create a button for comparing. Button CompareButton = new Button("Compare"); //create a button for exit the system when the user finishes from getting the chart Button ExitButton = new Button(" Exit System "); // Attache an event handler to the ExitButton. //so When clicked, it exits the system (closes the application). ExitButton.setOnAction(e ->{ System.exit(0); }); //Attaches an event handler to the CompareButton. When clicked, it checks if both URL fields are not empty. //If they are, it shows an error message using Alert CompareButton.setOnAction(e ->{ try { if (text1.getText().trim().length()==0|| text2.getText().trim().length()==0){ Alert alert = new Alert(Alert.AlertType.ERROR, "Please be sure that "+"
the First or the Second URL "+"
are not Empty", ButtonType.OK); alert.showAndWait(); return; } else {// Calls the isValidURL method to check if the entered URLs are valid // If not, it shows an error message stating that enter valid URLs if (!isValidURL(text1.getText())||!isValidURL(text2.getText())){ Alert alert = new Alert(Alert.AlertType.ERROR, "Please enter valid URLs", ButtonType.OK); alert.showAndWait(); return; }// Handles the logic when the CompareButton is clicked. // Create a connection with the first URL. HttpsURLConnection urlConnection1=(HttpsURLConnection)(new URL(text1.getText())).openConnection(); urlConnection1.setRequestMethod("GET"); // Check the HTTP response code if (urlConnection1.getResponseCode()== HttpsURLConnection.HTTP_OK){// Read data from the First URL site try (BufferedReader br = new BufferedReader( new InputStreamReader(urlConnection1.getInputStream())); PrintWriter pw = new PrintWriter(new File("f1.txt"))){ String str; while ((str = br.readLine())!= null){ pw.println(str); }}} else {// Display an error message to the user Alert alert = new Alert(Alert.AlertType.ERROR, "HTTP response code: "+ urlConnection1.getResponseCode()+" for URL: "+ text1.getText(), ButtonType.OK); alert.showAndWait(); return; // Exit the method if an error occurs }// Create a connection with the second URL. HttpsURLConnection urlConnection2=(HttpsURLConnection)(new URL(text2.getText())).openConnection(); urlConnection2.setRequestMethod("GET"); // Check the HTTP response code if (urlConnection2.getResponseCode()== HttpsURLConnection.HTTP_OK){// Read data from the Second URL site try (BufferedReader br = new BufferedReader( new InputStreamReader(urlConnection2.getInputStream())); PrintWriter pw = new PrintWriter(new File("f2.txt"))){ String str; while ((str = br.readLine())!= null){ pw.println(str); }}} else {// Display an error message to the user Alert alert = new Alert(Alert.AlertType.ERROR, "HTTP response code: "+ urlConnection2.getResponseCode()+" for URL: "+ text2.getText(), ButtonType.OK); alert.showAndWait(); return; // Exit the method if an error occurs }}} catch (MalformedURLException ex){// Display an alert to the user Alert alert = new Alert(Alert.AlertType.ERROR, "Invalid URL entered: "+ ex.getMessage(), ButtonType.OK); alert.showAndWait(); return; // Exit the method if an error occurs } catch (IOException ex){// Display an alert to the user Alert alert = new Alert(Alert.AlertType.ERROR, "IOException occurred: "+ ex.getMessage(), ButtonType.OK); alert.showAndWait(); return; // Exit the method if an error occurs
Provide the uml digram for this code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

This symbol denotes the population proportion in group i.

Answered: 1 week ago