1
resposta

Desafios

//Ex 01

package challenges.part09;

import java.util.Scanner;

public class DivideWithException {
    public static void main(String[] args) {

        int number1, number2;
        double result;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the first number: ");
        number1 = scanner.nextInt();

        System.out.println("Enter the second number: ");
        number2 = scanner.nextInt();

        try {
            if(number1 == 0 || number2 == 0) {
                throw new ZeroInputException("You entered 0, which is not a valid number. Please choose another number!");
            }

            result = (double) number1 / number2;

            System.out.println("The division result of " + number1 + " and " + number2 + " is " + result);

        } catch (ZeroInputException e) {
            System.out.println("An error occurred!");
            System.out.println(e.getMessage());
        }

        System.out.println("Program execution finished correctly!");
    }
}


package challenges.part09;

public class ZeroInputException extends RuntimeException {
    public ZeroInputException(String message) {
        super(message);
    }
}

//Ex 02

package challenges.part09;

import java.util.Scanner;

public class PasswordInputException {
    public static void main(String[] args) {

        String userPassword;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your password: ");
        userPassword = scanner.nextLine();

        try {
            if(userPassword.length() < 8) {
                throw new InvalidPasswordException("Your password is not valid. It must have at least 8 characters.");
            }

            System.out.println("Your password is valid!");

        } catch (InvalidPasswordException e) {
            System.out.println("An error occurred!");
            System.out.println(e.getMessage());
        }

        System.out.println("Program execution finished correctly!");
    }
}
package challenges.part09;

public class InvalidPasswordException extends RuntimeException {
    public InvalidPasswordException(String message) {
        super(message);
    }

}

// Ex 03

package challenges.part09;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;

public class GithubProfileSearch {
    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the Github Username you want to search for: ");
        var username = scanner.nextLine();

        String url = "https://api.github.com/users/" + username;

        try {
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .build();

            HttpResponse<String> response = client
                    .send(request, HttpResponse.BodyHandlers.ofString());

            if (response.statusCode() == 404) {
                throw new GitHubQueryErrorException("Sorry, but the user could not be found. Let's try again with a different username.");
            }
            System.out.println("User details for " + username);
            System.out.println(response.body());

        } catch (IOException | InterruptedException e) {
            System.out.println("An error occurred while processing the request. Please try again later!");
            System.out.println(e.getMessage());
        } catch(IllegalArgumentException e) {
            System.out.println("Invalid input. Enter a valid GitHub username without spaces or special characters, like in the URL.");
        }
        catch (GitHubQueryErrorException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("Programming finished correctly!");
    }
}

package challenges.part09;

public class GitHubQueryErrorException extends RuntimeException {
    public GitHubQueryErrorException(String message) {
        super (message);

    }
}
1 resposta

Oi, Fabrício! Tudo bem?

Parabéns pelo código! A prática do conteúdo é algo que nos ajuda a internalizar o conhecimento adquirido e nesse processo caso tenha restado alguma dúvida em específico, sinta-se à vontade em comunicar, estou à disposição e ficarei super feliz em poder ajudar!

Um forte abraço e bons estudos!