Jas K
Jas K

Reputation: 3

How to restart a java program here

import java.util.Scanner;

public class App {

  public static void main(String[] args) {

    System.out.println("Please enter your official name.");

    Scanner typing = new Scanner(System.in);
    String name = typing.nextLine();

    while (!name.matches("[a-zA-Z]")) {
      System.out.println("Please enter your official name.");
      name = typing.nextLine();
    }
    if (name.length() >= 4)
      System.out.println("Your name is: " + name);

    else
      System.out.println("So your name is less than four characters.");

  }

}

How do I restart the program here! PS: am a beginner in java

Upvotes: 0

Views: 140

Answers (4)

Jas K
Jas K

Reputation: 3

I further improved it:

import java.util.Scanner;

public class App {

public static void main(String[] args) {
    restart();

}

public static void restart() {
    System.out.println("Please enter your official name!");
    Scanner input = new Scanner(System.in);
    String name = input.nextLine();
    String regex = "^[a-zA-Z]+$";

    while (name.length() < 11) {
        System.out.println("Please enter your official name.");
        name = input.nextLine();
        continue;
    }

    if (name.matches(regex)) {
        System.out.println("Your name is: " + name);
        System.exit(0);
    }

    else {
        System.out.println("That's not a name!");
        restart();
    }

}

}

Upvotes: 0

Jas K
Jas K

Reputation: 3

Thanks so much guys, took a cue from both answers & wrote this:

import java.util.Scanner;

public class App {

public static void main(String[] args) {
    restart();

}

public static void restart() {
    System.out.println("Please enter your official name!");
    Scanner input = new Scanner(System.in);
    String name = input.nextLine();
    String regex = "^[a-zA-Z]+$";

    do {
        System.out.println("Please enter your official name.");
        name = input.nextLine();

    } while (name.length() < 11);

    if (name.matches(regex)) {
        System.out.println("Your name is: " + name);
        System.exit(0);
    }

    else {
        System.out.println("That's not a name!");
        restart();
    }

}

}

Upvotes: 0

世界知名伟人
世界知名伟人

Reputation: 155

it's not easy to restart the program entirely and also not neccessary.You can do the same thing in anther way like this.

public static void main(String[] args) {
    foo();
}

public static void foo(){
    System.out.println("Please enter your official name.");

    Scanner typing = new Scanner(System.in);
    String name = typing.nextLine();

    while (!name.matches("[a-zA-Z]")) {
        System.out.println("Please enter your official name.");
        name = typing.nextLine();
    }
    if (name.length() >= 4)
        System.out.println("Your name is: " + name);

    else
        System.out.println("So your name is less than four characters.");

    if(true){// put your condition when you what to restart here
        foo();// rerun this method
    }
}

Update.The previous solution may cause error after a long run. This is better.

public static void main(String[] args) {
    boolean willContinue = false;
    do{
        System.out.println("Please enter your official name.");

        Scanner typing = new Scanner(System.in);
        String name = typing.nextLine();

        while (!name.matches("[a-zA-Z]")) {
            System.out.println("Please enter your official name.");
            name = typing.nextLine();
        }
        if (name.length() >= 4)
            System.out.println("Your name is: " + name);

        else
            System.out.println("So your name is less than four characters.");

        willContinue = true;// put your condition when you what to restart here
    }while(willContinue);
}

Upvotes: 1

Roshana Pitigala
Roshana Pitigala

Reputation: 8806

Once the main method exits the application exits. Put your code in another method and call it inside a while loop.

public static void main(String[] args) {
    while(true){
        process();
    }
}

static void process(){
    System.out.println("Please enter your official name.");

    Scanner typing = new Scanner(System.in);
    String name = typing.nextLine();

    while (!name.matches("[a-zA-Z]")) {
      System.out.println("Please enter your official name.");
      name = typing.nextLine();
    }
    if (name.length() >= 4)
      System.out.println("Your name is: " + name);

    else
      System.out.println("So your name is less than four characters.");
}

This will run the program again and again infinitely. If you want to exit, press Ctrl + C in cmd or you can use,

System.exit(0);

somewhere in your code.

Upvotes: 3

Related Questions