java - What is the preferred way to complete this while loop for desired output? -
i have written little program takes name of email sender looking @ email address , returning letters @
character.
i have written while loop in 2 ways, 1 way primes loop , second uses nested if statement. both each other or 1 preferred method on other?
priming while loop:
public void findusername() { scanner keyboard = new scanner(system.in); char symbol = ' '; system.out.println("please enter email!"); symbol = keyboard.findwithinhorizon(".", 0).charat(0); while (symbol != '@') { system.out.print(symbol); symbol = keyboard.findwithinhorizon(".", 0).charat(0); } system.out.println(); }
using nested if statement break if symbol == '@'
public void findusername() { scanner keyboard = new scanner(system.in); char symbol = ' '; system.out.println("please enter email!"); while (symbol != '@') { symbol = keyboard.findwithinhorizon(".", 0).charat(0); if (symbol == '@') { break; } system.out.print(symbol); } system.out.println(); }
you don't need make loop, can use :
string email = "email@abd.com"; string str = email.substring(0, email.indexof("@"));
this give email
Comments
Post a Comment