-2

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.

My current code

  • 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
  • 1
    Please 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 Answers1

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