Good evening programmers, I am new to java and I am stuck on a coding question that is about using first 2 letters of a first name and 4 letters of a last to create a username with a random number for example, Michael Jackson= JackMi42. If anyone can help me out I will really appreciate it. the application that I am using to do this is Eclipse IDE. Here is the code that I have created so far:
import java.util.Scanner;
public class FirstNameLastName {
/**In this step you put in a last name
*/
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
System.out.println("Please enter your last name: ");
in.nextLine();
/**In this step you put in any first name
*/
System.out.println("Please enter your first name: ");
in.nextLine();
/**In this step you type in the four letters of the last name
*/
System.out.println("four letters of last name:");
String FourLetterOfLastName = in.nextLine();
/**In this step you type in the two letters of the first name
*/
System.out.println("two letters of first name:");
String TwoLetterOfFirstName = in.nextLine();
/**In this step you type in any number between 10 and 99
*/
System.out.println("random number between 10 and 99");
String randomnum = in.nextLine();
/**In this step you print out the four letter of last name two letter of first name a
* random number between 10 and 99.
* at then end it will print it out on the output section in the console section
*/
System.out.println(FourLetterOfLastName TwoLetterOfFirstName randomnum);
}
}
}
CodePudding user response:
You can try this way:
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class FirstNameLastName {
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
System.out.println("Please enter your last name: ");
String lastName = in.nextLine();
System.out.println("Please enter your first name: ");
String firstName = in.nextLine();
String fourLetterOfLastName = lastName;
if(lastName.length() > 4) {
fourLetterOfLastName = lastName.substring(0, 4);
}
String twoLetterOfFirstName = firstName;
if(firstName.length() > 2) {
twoLetterOfFirstName = firstName.substring(0, 2);
}
String randomnum = String.valueOf(ThreadLocalRandom.current().nextInt(10, 100));
System.out.println(fourLetterOfLastName twoLetterOfFirstName randomnum);
}
}
}
CodePudding user response:
Try looking into String.substring() method. Example:
String fourLettersLastName = lastName.substring(0, 4);
// if lastName = Jackson, then fourLettersLastName = Jack
For the random number you can look at Math.random(). Example:
int randomNum = (int)(Math.random() * (max - min) min);
// In your case, max = 99 and min = 10
// The (int) cast is needed because Math.random() returns a double
In the end you concatenate everything (using the ' ' sign) and print out for the user. A tip in the future when playing with Strings in Java is to use StringBuilder.
Other thing I noticed is the variable named randomnum which is against Java naming conventions (and almost every programming language). The way is supposed to be named is using camelCase (in this case, randomNum). You should check this page for more info on this.