Question

I'm creating a program that accepts user input and generates a ticket to go to a concert. For this, in one of the text fields, the user enters his full name. The program then gets the contents of the text field and sets it to the string concertgoer. To obtain the user's initials, I am using String Tokenizer to split concertgoer into a firstName, middleName, and lastName string, and then getting the first letter from each of these using substring.

I want the program to handle exceptions where if the user only entered two words separated by one space in the text field (e.x. first name and last name), then the program generates a random letter, and concatenates it to the string concertgoer between the first and last name, where the space is, so that middleName can have a value for the initial. I attempted to do this by processing the string concertgoer and scanning for spaces and trying to replace the space with the letter. However, it is not working, and the program crashes. The code from this part of the program is shown below, particularly in the for loop. The println statements to the console are mainly just for reference, and the message shown after the crash is also below.

Random numCharacter2 = new Random();

String alphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char addedLetter2 = alphabet2.charAt(numCharacter2.nextInt(alphabet2.length()));


for (int counter = 0; counter < concertgoer.length(); counter++) {

 char scanner = ' ';

 if (concertgoer.charAt(counter) == scanner) {

     System.out.println(scanner + "\n Hello, World");

     concertgoer = concertgoer.replace(concertgoer.charAt(scanner), addedLetter2); 

     System.out.println("New: " + concertgoer);
 }

} 


 concertgoer = concertgoer + " " + addedLetter2; 

    //! Add letter in middle of string concertgoer if only two words are entered as substitute
      //for middle name

 StringTokenizer token = new StringTokenizer(concertgoer);
 String firstName = token.nextToken();
 String middleName = token.nextToken();
 String lastName = token.nextToken();

 String firstInitial = firstName.substring(0, 1);
 String middleInitial = middleName.substring(0, 1);
 String lastInitial = lastName.substring(0, 1);

 firstInitial = firstInitial.toUpperCase();
 middleInitial = middleInitial.toUpperCase(); 
 lastInitial = lastInitial.toUpperCase();

 System.out.println(firstInitial);
 System.out.println(middleInitial);
 System.out.println(lastInitial);

 String nameInitials = firstInitial + middleInitial + lastInitial;
 nameInitials = nameInitials.replace(" ", "");
 System.out.println(nameInitials);

 //double num = Math.random();
 //Use random class and nextInt set range for any random number

Random num = new Random(); 
int randomInteger = Math.abs(num.nextInt() % 9999);
System.out.println(randomInteger);
ticketPasscode = nameInitials + randomInteger;
String message = "Your ticket passcode is: " + ticketPasscode;
messageBox(message);
System.out.println(ticketPasscode);

Can somebody please help me for a solution to this to find the right way? I appreciate your help. Thank you.

*Crash

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
    at TicketPrinter.buttonClicked(TicketPrinter.java:138)
    at BreezyGUI.GBFrameButtonListener.actionPerformed(GBFrame.java:241)
    at java.awt.Button.processActionEvent(Button.java:409)
    at java.awt.Button.processEvent(Button.java:377)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Was it helpful?

Solution

Use hasNextToken() before each call to nextToken() to check if there actually is a next token. At this step you have 0, 1, 2, 3,... Strings.

At this step, everything's fine if you have 3 Strings (first name, middle name, last name) and the program continues with the first token as the first name, the second token as the middle name and the third token as the last name, of course.

If on the other hand there are only two Strings at this step, you assign the middle name a random letter as you said, and you assign the first token to the first name and the second token to the last name.

From there, your program continues with

String firstInitial = ...

OTHER TIPS

Are you sure about this line?

concertgoer = concertgoer.replace(concertgoer.charAt(scanner), addedLetter2); 

This will actually check for the 32nd character in the String concertgoer, and then try to replace it. This is because char ' ' actually evaluates to int 32. Are you sure this is the functionality you want? Moreover, if the concertgoer string is less than 32 characters, it will throw an exception.

Does this work? (Didn't test it)

String insert(String input, char tbi, int index) {
    String pre = input.substring(0, index-1);
    String post = input.substring(index, input.length-1);
    return pre + tbi + post;
}

You could actually make a one-liner out of it...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top