Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Amigoscode Java Exercises

Guided hands-on exercises for the Java learning path on
[Amigoscode Academy](https://skool.com/amigoscode).
[Amigoscode Academy](https://skool.com/amigoscode-academy).

## Tech Stack

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* User Input Exercises
*
* <p>
* Practice reading and validating user input using java.util.Scanner.
* Always validate user input — never trust it blindly!
*/
Expand All @@ -20,7 +20,8 @@ public class UserInputExercises {
public static String readString(Scanner scanner) {
// TODO: 1 - Prompt the user with "Enter your name: " (use System.out.print).
// Read a full line using scanner.nextLine() and return it.
return null;
System.out.print("Enter your name: ");
return scanner.nextLine();
}

/**
Expand All @@ -38,7 +39,17 @@ public static int readIntSafely(Scanner scanner) {
// catch InputMismatchException, print "Invalid input!", and return -1.
// Don't forget to consume the leftover newline with scanner.nextLine()
// after reading the int (both in success and failure cases).
return 0;
System.out.print("Enter a number: ");
try {
int number = scanner.nextInt();
scanner.nextLine();
return number;
} catch (InputMismatchException e) {
System.out.println("Invalid input!");
scanner.nextLine();
return -1;
}

}

/**
Expand All @@ -53,7 +64,17 @@ public static void readUntilQuit(Scanner scanner) {
// Read a line with scanner.nextLine().
// If the line equals "quit" (case-insensitive), break out of the loop.
// Otherwise, print "You entered: " followed by the input.
while (true) {
System.out.print("Enter text (or 'quit' to stop): ");
String input = scanner.nextLine();

if (input.equalsIgnoreCase("quit")) {
break;
} else {
System.out.println("You entered: " + input);
}

}
}

/**
Expand All @@ -64,7 +85,7 @@ public static void readUntilQuit(Scanner scanner) {
*/
public static boolean isValidAge(int age) {
// TODO: 4 - Return true if age is between 0 and 150 (inclusive), false otherwise.
return false;
return age >= 0 && age <= 150;
}

/**
Expand All @@ -75,7 +96,7 @@ public static boolean isValidAge(int age) {
*/
public static boolean isValidEmail(String email) {
// TODO: 5 - Return true if email is not null and contains "@", false otherwise.
return false;
return email != null && email.contains("@");
}

/**
Expand All @@ -92,31 +113,75 @@ public static void registrationForm(Scanner scanner) {
// 3. Ask for email. Keep asking until isValidEmail() returns true.
// 4. Print a summary: "Registration complete!"
// "Name: ...", "Age: ...", "Email: ..."
String inputName;
int inputAge = 0;
String inputEmail;
while (true) {
System.out.print("What is your name?: ");
inputName = scanner.nextLine();

if (inputName.isEmpty()) {
System.out.println("Name cannot be empty.");
} else if (inputName.matches(".*\\d.*")) {
System.out.println("Names cannot contain numbers. Please try again.");
} else {
break;
}
}

while (true) {
System.out.print("What is your age?: ");
try {
inputAge = scanner.nextInt();
scanner.nextLine();
if (isValidAge(inputAge)) {
break;
}
} catch (InputMismatchException e) {
System.out.println("entered a non-number");
scanner.nextLine();
}

}

while (true) {
System.out.print("What is your email?: ");
inputEmail = scanner.nextLine();

if (isValidEmail(inputEmail)) {
break;
}
}

System.out.println("Registration complete!");
System.out.println("Name: " + inputName + ", Age: " + inputAge + ", Email: " + inputEmail);


}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("=== Read String ===");
String name = readString(scanner);
System.out.println("Hello, " + name + "!");

// System.out.println("=== Read String ===");
// String name = readString(scanner);
// System.out.println("Hello, " + name + "!");
//
System.out.println("\n=== Read Int Safely ===");
int number = readIntSafely(scanner);
System.out.println("You entered: " + number);

System.out.println("\n=== Read Until Quit ===");
//
System.out.println();
System.out.println("=== Read Until Quit ===");
readUntilQuit(scanner);

System.out.println("\n=== Validation ===");
System.out.println("Age 25 valid? " + isValidAge(25));
System.out.println("Age -5 valid? " + isValidAge(-5));
System.out.println("Email 'test@mail.com' valid? " + isValidEmail("test@mail.com"));
System.out.println("Email 'invalid' valid? " + isValidEmail("invalid"));

System.out.println("\n=== Registration Form ===");
registrationForm(scanner);
//
// System.out.println("\n=== Validation ===");
// System.out.println("Age 25 valid? " + isValidAge(25));
// System.out.println("Age -5 valid? " + isValidAge(-5));
// System.out.println("Email 'test@mail.com' valid? " + isValidEmail("test@mail.com"));
// System.out.println("Email 'invalid' valid? " + isValidEmail("invalid"));

// System.out.println("\n=== Registration Form ===");
// registrationForm(scanner);

scanner.close();
}
Expand Down