0

Hey I have a basic java 'app' showing if people are an adult or teen etc.. I am starting out with java and I can't find how to make it so after a user has input an age, and the string of what they are classified as shows I would like it to re run the whole thing again so someone else can have a go. I have been looking into doing a loop but that hasn't worke dout for me.. Here is the code:

    public static void main(String[] args)
{
    //Declare local variables
    int age;
    Scanner in = new Scanner(System.in);

    //Prompt user for Age
    System.out.print("Enter Age: ");
    age = in.nextInt();

    if(age > 17)
    {
        System.out.println("This persons age indicates that they an adult");
    }
    else if (age > 12)
    {
        System.out.println("This persons age indicates that they are a teenager");
    }
    else if (age >= 0)
    {
        System.out.println("This persons age indicates that they are a child");
    }
    else
    {
        System.out.println("That age is invalid");
    }

    //Close input
    in.close();
}

Any help would be greatly appreciated. I am sure it is super simple and I have missed something.

5
  • 1
    What did you try with the loops? Commented Mar 13, 2015 at 3:47
  • 1
    Put all the content of main into a separate method and call it as many times as you wish. Commented Mar 13, 2015 at 3:47
  • @allieluu This code should be refactored - and you suggest to add more complexity into it instead... Commented Mar 13, 2015 at 3:48
  • @alfasin I only wanted to clarify what specifically he tried. If it was something as simple as not putting all of the necessary code in the loop, I would be able to help better. It wasn't intended to be a suggestion yet. Commented Mar 13, 2015 at 3:51
  • 2
    Also, don't close in here, because that closes System.in. (Sometimes you need to close streams, and sometimes you don't. You should close a lot of things, but not System.in) Commented Mar 13, 2015 at 3:55

3 Answers 3

2

You can put it in a loop such as

 public static void main(String[] args)
{
    //Declare local variables
    int age;
    Scanner in = new Scanner(System.in);
while(true){
    //Prompt user for Age
    System.out.print("Enter Age: ");
        age = in.nextInt();

        if(age > 17)
        {
            System.out.println("This persons age indicates that they an adult");
        }
        else if (age > 12)
        {
            System.out.println("This persons age indicates that they are a teenager");
        }
        else if (age >= 0)
        {
            System.out.println("This persons age indicates that they are a child");
        }
        else
        {
            System.out.println("That age is invalid");
        }
}

        //Close input
        in.close(); //Note that this happens outside the loop
    }

or put the needed code in a method

public static void ageCheck()
{
  //code that is currently in main method
}

it would be called from the main method by

this.ageCheck();
Sign up to request clarification or add additional context in comments.

1 Comment

You might need the code "in.nextLine()" at the end of the while loop.
0

You could do a while(true) loop to run forever

Comments

0

You can call your main() recursively

After this in.close();statement just call your main() like this

 in.close();
  main(null);

But you must put some conditions when to stop the execution of you program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.