-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserNameGenerator.java
More file actions
43 lines (37 loc) · 1.26 KB
/
UserNameGenerator.java
File metadata and controls
43 lines (37 loc) · 1.26 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
/*
* Description:This program asks user to enter his first and last name.
* After input, program randomly generates new user name based on both
* inputs.
* Lab# 1
*/
import java.util.Scanner;
import java.util.Random;
public class UserNameGenerator
{
public static void main(String... args)
{
int number; // to hold random number
String firstName; // to hold first name
String lastName; // to hold last name
String userName; // to hold new user name
Scanner input = new Scanner(System.in);
System.out.print("Please, enter your first name: ");
firstName = input.nextLine().replaceAll("\\W","");
System.out.print("Please, enter your last name: ");
lastName = input.nextLine().replaceAll("\\W","");
Random generator = new Random();
number = generator.nextInt(89) + 10;
// possible situation, when last name doesn't have 5 letters.
if (lastName.length() < 5)
{
userName = firstName.toUpperCase().charAt(0) + lastName.substring(0, lastName.length()) + number;
}
else
{
//same statement, but it just prints 5 and more letters
userName = firstName.toUpperCase().charAt(0) + lastName.substring(0, 5) + number;
System.out.print("User name: " + userName);
}
input.close();
}
}