-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsefulStringMethods_SubString.java
More file actions
56 lines (36 loc) · 1.85 KB
/
UsefulStringMethods_SubString.java
File metadata and controls
56 lines (36 loc) · 1.85 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
public class UsefulStringMethods_SubString {
public static void main(String [] args){
// substring()= a method that is used to extract a portion if a string
// .substring(Start, end);the start is the beginning index its inclusive, the end is ending index its exclusive
// NOTE: the index start at 0
// String email="jm1232@gmail.com";
// String userName= email.substring(0,6);
// System.out.println("the username is "+userName);
// String domain=email.substring(7,16);
// if you want all the characters that come after the said index you just enter the index no need of the ending index
// the domain above can be written as :
// domain=email.substring(7);
// System.out.println("the domain is "+domain);
/* EXERCISE EMAIL SLICER PROGRAM
write a program tha asks the user to enter their email.then the system takes in the email and first checks
if it has the "@" character then it displays the username and the domain
otherwise it informs the user it Should contain the '@' character
*/
Scanner input= new Scanner( System.in);
String userEmail;
String userName;
String domain;
System.out.print("Hello there!\n" +
"Enter your email here :");
userEmail=input.nextLine();
if(userEmail.contains("@")){
userName=userEmail.substring(0,userEmail.indexOf("@"));
domain=userEmail.substring(userEmail.indexOf('@')+1);
System.out.println("the username is: " +userName);
System.out.println("the domain is: "+ domain);
}else {
System.out.println(" your email MUST contain the '@' character ");
}
}
}