I'm a bit new to programming, and for an in class project, I have to take a user-inputted email, find everything before the @ symbol, and return it as a username. The part I'm having trouble on is finding the characters before the @, with it being variable length. As far as I can tell, most methods rely on a preset string length.
Asked
Active
Viewed 165 times
-2
-
I would suggest using two methods of the String class, `indexOf` and `substring`. Use `indexOf` to find the position of the `@` character, and then `substring` to grab just the first portion of the string based on that position. Since you're processing user input you'll want to allow for the case where the user didn't enter a valid email address and so `indexOf('@')` returns `-1`. Your code will throw an exception if you blindy try to use that value. β CryptoFool Jan 23 '21 at 01:34
-
1Please do not post a picture of code. Itβs hard to read and prevents our sight impaired users from participating. Also it cannot be searched or copied into a text editor. Consider editing your question and pasting the code directly into it as text. β VGR Jan 23 '21 at 02:16
1 Answers
1
Read the javadoc of String. Of particular interest are substring
and indexOf
. It's.. a trivial assignment, there's nothing left to say except spoonfeed you the answer, which probably doesn't help you accomplish the goal (which is, surely, to learn java).

rzwitserloot
- 85,357
- 5
- 51
- 72