1
package stringvars;

import java.util.Scanner;

public class ConcertID {
    public static void main(String[] args) {
        try (Scanner userInput = new Scanner (System.in)) {
            String yourName;
            System.out.print ("enter the last letter of your second name: ");
            yourName = userInput.next();

            String yourDOB;
            System.out.print ("enter your Year Of Birth: ");
            yourDOB = userInput.next();

            String ConcertID;
            ConcertID = yourName + " " + yourDOB;
                        
            System.out.println("your concert ID is " + ConcertID);
        }
    }
}

I'm trying to get the code to take the user input, add a number between 1 and 10 at the end and print it as Y18867. Currently it prints as Y 1886.

(And I've yet to figure out the math.random part.)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    Take care of java naming conventions. All namess shoud be camelCase not snake_case – Jens Aug 06 '22 at 10:35
  • @Jens how is this question a duplicate from linked one? I can't see any `nextLine()` in posted code... (but the indentation is not helping) || @Guilty_Mulburry What name is being input? Two words? (second word will be read as DOB) – user16320675 Aug 06 '22 at 11:40
  • @user16320675 sorry about my wonky indents... I'm new to programming I've no idea what you mean... sorry... –  Aug 06 '22 at 12:39
  • @user16320675 did what I could to make it prettier –  Aug 06 '22 at 12:48
  • 1
    @user16320675 *I can't see any nextLine() in posted code* and that is the problem. `nex()` has tobe `nextLine()` – Jens Aug 06 '22 at 13:36
  • And do you think so? – Jens Aug 06 '22 at 14:11
  • @user16320675 I've switched out the second next( ) for nextline, its printing in the way I want it to now, but there's still the problem of the maths.random (Which I've been plugging away at and got nowhere) and it printing with spaces whereas I'd want it to print as one word, however I can live with that not working –  Aug 06 '22 at 14:35

1 Answers1

1

Let me recommend you start using the StringBuilder class to create concatenated strings. It has a better performance regarding time consuming to concatenate strings.

The following code generates the random number as well as the concertId string that you are trying to get.

public class ConcertID
{
    public static void main(String[] args)
    {
        try (Scanner userInput = new Scanner(System.in))
        {
            String yourName;
            System.out.print("Enter the last letter of your second name: ");
            yourName = userInput.nextLine();
        
            String yearOfBirth;
            System.out.print("Enter your Year of Birth: ");
            yearOfBirth = userInput.nextLine();
        
            StringBuilder concertId = new StringBuilder();
         
            concertId.append(yourName);
            concertId.append(yearOfBirth);
            concertId.append(generateNumber());
        
            System.out.println(concertId.toString());
        }
    }

    public static int generateNumber()
    {
        int number = 0;
        Random random = new Random();
        number = random.nextInt(1, 10);

        return number;
    }
}
  • Thank you so much, man. the number generator you wrote will help me in the future as well. –  Aug 06 '22 at 15:55
  • 1
    BTW your proposed code using the `StringBuilder` is basically the same that (older) Java would do when using `+` for concatenation, no advantage - the compiler would create almost the same code - up to Java 8, it would be indicated to create a `StringBuilder` outside a loop if the concatenation is inside that loop - and with not so old versions of Java (9 and newer), concatenation is done with help of `StringConcatFactory`, better than `StringBuilder` – user16320675 Aug 06 '22 at 16:01