-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPittyPuttyCsempazo.java
More file actions
34 lines (29 loc) · 1.18 KB
/
PittyPuttyCsempazo.java
File metadata and controls
34 lines (29 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package PittyPuttyCsempazo;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;
public class PittyPuttyCsempazo {
public static void main(String[] args) {
String url = "https://www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String body = response.body().trim();
int number = Integer.parseInt(body);
if (number % 2 == 0) {
System.out.println("Pitty");
} else {
System.out.println("Putty");
}
} catch (IOException | InterruptedException e) {
System.err.println("Network error: " + e.getMessage());
} catch (NumberFormatException e) {
System.err.println("Parsing error: Invalid number received");
}
}
}