Shane Williams Shane Williams - 5 months ago 5x
Java Question

Need help utilising the while loop for repeating parts of code

I am trying to use the while loop for re-running parts of code in my simple program. In this program, the user makes a simple account, types in a verification number, then signs in. Once signed in, I wish to allow the user to sign out, edit his profile settings and more. However I have a small problem.

Say the user has just edited their account settings. Instead of the program terminating, I want them to be able to return to the "menu".

The problem lies with how to do that. As there is no

statement in java, from what I have read, I must use the
loop. I have no idea of how to go about that. I just can't wrap my head around it. Loops have always confused me. Also, should I even use the
loop? Would it be better to use the
loops? And what expression should I use? Will I need the

I know it isn't a concrete question, but any help that puts me on the right path is well appreciated.

Below is the full code for reference.

public static void main(String[] args) {
System.out.println("Hi! To begin please choose your username.");
System.out.println("To do this, please enter your username below. ");
System.out.println("This name must be at least three characters.");

Scanner userInput = new Scanner(;

String userName =;

int nameLength = userName.length();

if (nameLength > 2) {

System.out.println("Now please enter your password.");
System.out.println("This password must be at lease five characters");

String passWord =;

int passLength = passWord.length();

if (passLength > 4) {
System.out.println("Signup alost complete.");

Random rand = new Random();

int randm = rand.nextInt(100000) + 1;

System.out.println("To confirm you are not a robot, please enter this code: " + randm);

int code = userInput.nextInt();

if (code == randm) {
System.out.println("Thank you, " + userName);
System.out.println("You may now login. Begin by entering your username");

//Where I would go if the user signed out

String name =;

if (name.equals(userName)) {
System.out.println("Now please enter you password");

String pass =;

if (pass.equals(passWord)) {
System.out.println("Thank you. You have now successfully logged in");

//Where the "main menu" will be
//Rest of code will also go here
else {
System.out.println("Password is incorrect");
else {
System.out.println("Username is incorrect");
else {
System.out.println("The code entered is incorrect.");
else {
System.out.println("Invalid Password");
else {
System.out.println("Invalid Username");


You have placed a comment //where would I go if the user signed out? The answer is, You will show the message to sign in when he is signed out, so that he can sign in again. You can do this by using for loop or loop or whatever loop you want. That means the part of user login will be in a loop, if the user logged in then the menu will be shown up. If the user sign out, the sign in form will be shown up infinitely.