Question: Edit this code so that instead of one big main method it is separated into different methods. _ _ _ _ _ _ _ _

Edit this code so that instead of one big main method it is separated into different methods.
_____________________
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class NamePopularityV1{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the year: ");
String year = scanner.nextLine();
System.out.print("Enter the name: ");
String name = scanner.nextLine();
System.out.print("Enter the gender (M/F): ");
String gender = scanner.nextLine().toUpperCase();
String urlString = "websiteurlgoeshere" + year;
try {
URL url = new URL(urlString);
HttpURLConnection connection =(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode()==200){
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
boolean found = false;
while ((line = reader.readLine())!= null){
String[] parts = line.split(",");
if (parts.length ==4){
String recordName = parts[0].trim();
String recordGender = parts[1].trim();
String popularity = parts[3].trim();
if (recordName.equalsIgnoreCase(name) && recordGender.equalsIgnoreCase(gender)){
System.out.println("The popularity of the name "+ name +" in year "+ year +" is: "+ popularity);
found = true;
break;
}
}
}
reader.close();
if (!found){
System.out.println("The name "+ name +" with gender "+ gender +" was not found for the year "+ year +".");
}
} else {
System.out.println("Failed to fetch data. HTTP Response Code: "+ connection.getResponseCode());
}
} catch (Exception e){
System.out.println("An error occurred: "+ e.getMessage());
}
scanner.close();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!