explored substring methods
This commit is contained in:
parent
a88cb9a6ed
commit
c1aa0e09ad
1 changed files with 42 additions and 0 deletions
42
practice_projects/src/substrings.java
Normal file
42
practice_projects/src/substrings.java
Normal file
|
|
@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue