exception handling and basic file operations in Java #2

Closed
faraz_ardeh wants to merge 1 commits from faraz_ardeh/WS-05-exceptions-and-file-handling:develop into main
2 changed files with 61 additions and 42 deletions
+20 -15
View File
@@ -1,5 +1,11 @@
package org.Exceptions; package org.Exceptions;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Scanner; import java.util.Scanner;
@@ -45,35 +51,32 @@ public class FileService {
public static String writeFile(UserEntity user) { public static String writeFile(UserEntity user) {
//TODO: Uncomment the lines below. try {
//TODO: Use your knowledge of exception handling to make the code work as expected
/* Uncomment this section
//TODO: handle the exception
FileWriter fw = new FileWriter("userInformation.txt", true); FileWriter fw = new FileWriter("userInformation.txt", true);
BufferedWriter bw = new BufferedWriter(fw); BufferedWriter bw = new BufferedWriter(fw);
bw.write(user.toString()); bw.write(user.toString());
bw.close(); bw.close();
*/ } catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
return "failed to write user information";
}
return "user information written to file"; return "user information written to file";
} }
public static void readFile() { public static void readFile() {
//TODO: Uncomment the lines below. try {
//TODO: Use your knowledge of exception handling to make the code work as expected
/* Uncomment this section
FileReader file = new FileReader("userInformation.txt"); FileReader file = new FileReader("userInformation.txt");
BufferedReader fileInput = new BufferedReader(file); BufferedReader fileInput = new BufferedReader(file);
for (int counter = 0; counter < 3; counter++) for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine()); System.out.println(fileInput.readLine());
fileInput.close(); fileInput.close();
*/ } catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
} }
@@ -82,9 +85,11 @@ public class FileService {
Date dob = null; Date dob = null;
//TODO: Uncomment this line. try {
//TODO: Use your knowledge of exception handling to make the code work as expected dob = formatter.parse(date);
//dob = formatter.parse(date); } catch (ParseException e) {
System.out.println("Error: invalid date format, expected yyyy-MM-dd - " + e.getMessage());
}
return dob; return dob;
} }
@@ -19,7 +19,7 @@ public class RuntimeExceptionDemo {
//nullPointerException //nullPointerException
//IllegalArgumentException //IllegalArgumentException
public static void validateEmail(UserEntity user) { public static void validateEmail(UserEntity user) {
try {
String email = user.getEmail(); String email = user.getEmail();
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$"; String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
@@ -29,27 +29,38 @@ public class RuntimeExceptionDemo {
} }
System.out.println("User email is valid"); System.out.println("User email is valid");
} catch (NullPointerException e) {
System.out.println("Error: email is null - " + e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
} }
// ArithmeticException // ArithmeticException
public static void calculatePayment(int totalAmount, int installmentCount) { public static void calculatePayment(int totalAmount, int installmentCount) {
try {
int payment = totalAmount / installmentCount; int payment = totalAmount / installmentCount;
System.out.println("Each installment : " + payment); System.out.println("Each installment : " + payment);
} catch (ArithmeticException e) {
System.out.println("Error: cannot divide by zero - " + e.getMessage());
}
} }
// NumberFormatException // NumberFormatException
public static void convertUserAge(String ageInput) { public static void convertUserAge(String ageInput) {
try {
int age = Integer.parseInt(ageInput); int age = Integer.parseInt(ageInput);
System.out.println("User age: " + age); System.out.println("User age: " + age);
} catch (NumberFormatException e) {
System.out.println("Error: invalid number format - " + e.getMessage());
}
} }
// IllegalStateException // IllegalStateException
public static void completeRegistration(UserEntity user) { public static void completeRegistration(UserEntity user) {
try {
if (user.getDateOfBirth() == null) { if (user.getDateOfBirth() == null) {
throw new IllegalStateException( throw new IllegalStateException(
"Registration cannot be completed. Date of birth is missing." "Registration cannot be completed. Date of birth is missing."
@@ -57,6 +68,9 @@ public class RuntimeExceptionDemo {
} }
System.out.println("Registration completed"); System.out.println("Registration completed");
} catch (IllegalStateException e) {
System.out.println("Error: " + e.getMessage());
}
} }
} }