sreoch
sreoch

Reputation: 21

Code is crashing, it is meant to take in a full name e.g. John Cena and output it as JCena)

private void getName()  
   {
       System.out.println("Please enter your full name");
       String fullName = iscan.next();
       getUserName(fullName);

   }

   private void getUserName(String fullName)
   {
       String firstChar = fullName.substring(0, 1);
       int firstSpace = fullName.indexOf(" ");
       int lastSpace = fullName.lastIndexOf(" ");
       String lastName = fullName.substring(firstSpace, lastSpace);

       String userName = firstChar+lastName;
       System.out.println("Your username is: " +userName);

   }

whenever I try to enter a name the code crashes. The error that appears is Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 thanks in advance!

Upvotes: 2

Views: 91

Answers (3)

Olivier Grégoire
Olivier Grégoire

Reputation: 35437

I guess this is above your level so far, but regexes can help you a bit.

static String getUserName(String fullName) {
  return fullName.replaceAll("(?<=[A-Z]).*?(?=[A-Z])", "");
}

Try it online!

Test cases

"John Cena" becomes "JCena"
"Donald J. Trump" becomes "DJTrump"

Explanation

  • String::replaceAll accepts a regex as first parameter and replaces what matches it with the second parameter.
  • (?<=[A-Z]) makes sure that the match has an uppercase letter before the actual match (it's a positive lookbehind)
  • .*? in replaceAll matches and captures the shortest string between the above delimiters
  • (?=[A-Z]) makes sure that the match has an uppercase letter after the actual match (it's a positive lookahead).

Basically, the regex means "match anything between two uppercase letters". And with the second parameter of replaceAll, that becomes: "replace anything between two uppercase letters with nothing".

Upvotes: 0

manbearpiig
manbearpiig

Reputation: 65

First, I would make those methods static if they do not belong to an object class.

Currently your code is crashing because your scanner is only reading in "John" and indexOf() returns -1 if the character you're looking for isn't in the string. Therefore you're trying to take a substring from index -1 to -1 which is out of the array bounds, thus the out of bounds error.

scan.next() is terminated at the space, you will want to use scan.nextLine() which will read the entire string until it reaches the end of the line.

There is only one space within the string so the first and last space will be at the same index.

Use the following code:

String lastName = fullName.substring(firstSpace + 1, fullName.length());

and now you no longer need the code for the second space. You could also use split(" ") to split the string in half at the space (this would be my preferred method for solving this problem, or regex but that seems overkill).

An easy way to figure this out on your own would be going through putting print statements to make sure you are getting what you want from your method calls or using the debugger.

Learning the debugger would also be invaulable, as this question isn't a difficult one to solve if you can see the underlying process happening along each step of the program.

Upvotes: 3

Amey Kulkarni
Amey Kulkarni

Reputation: 87

String[] names = fullName.split(" ");
   // this simple logic will only work for 2 word usernames
   if(names.length == 2){
       System.out.println(names[0].charAt(0)+ names[1]);
   }

I am not sure what you're trying to do with lastSpace, in case of John Cena, both firstSpace and lastSpace should be 5; and that makes your lastName string empty.

Upvotes: 1

Related Questions