From c1aa0e09adfc8965e443dad8539abd3f12febc7a Mon Sep 17 00:00:00 2001 From: Cameron Seamons Date: Wed, 7 Jan 2026 11:57:36 -0700 Subject: [PATCH] explored substring methods --- practice_projects/src/substrings.java | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 practice_projects/src/substrings.java diff --git a/practice_projects/src/substrings.java b/practice_projects/src/substrings.java new file mode 100644 index 0000000..b8a154a --- /dev/null +++ b/practice_projects/src/substrings.java @@ -0,0 +1,42 @@ +import java.util.Scanner; + +public class substrings { + + public static void main(String[] args){ + + Scanner scanner = new Scanner(System.in); + + + // .substring() is a method used to extract a portion of a string. + // You use it by giving a start and end position. + // Example: string.substring(start, end) + + while (true) { + // Have user enter their email + System.out.print("Enter an email: "); + String email = scanner.nextLine(); + + // Check if its a valid email (Contains @ symbol) + if(email.contains("@")){ + // find the index position of the @ symbol + int index = email.indexOf("@"); + + // find the domain address of their email + String domain = email.substring(index + 1); + + String username = email.substring(0, index); + + System.out.println("Your username is " + username); + System.out.println("Your email domain is " + domain); + break; + + } else { + System.out.println("Please enter a valid email address!\n"); + continue; + } + } + + scanner.close(); + + } +}