Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Return to the start of a program

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Return to the start of a program

    I have had to write a java program that takes in some user details and then performs some calculations. I have included a check in the program to ensure that the user has entered details correctly. I have no problem getting the program to perform the calculations if they are correct but I can't figure out how to get the program to restart so that the user re-enters values if they are not correct. I have copied the code below:
    System.out.println("Is this correct? (Y/N)");//Validation and check of data entered
    correctValues=sc.next() .charAt(0);
    if (correctValues=='Y'|| correctValues=='y')
    System.out.println("Thank you. You have achieved the following grade:" +finalGrade());
    if (correctValues=='N'||correctValues=='n')
    {System.out.print("Please re-run program");}

    So where I have added "Please re-run program" I would really like this no answer to send the user back to the first method in the program. I suspect this is fairly easy to do but I don't know how. Any help would be very gratefully received.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Return to the start of a program

    how to get the program to restart
    That is usually done by wrapping the code to be repeated in something like a while loop.
       boolean repeat = true;    // control looping
       while(repeat)   {
          // do the code to be repeated
          // if want to end loop set repeat to false
       }
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How should I start creating this program? Help
    By illegit in forum Java Theory & Questions
    Replies: 8
    Last Post: May 16th, 2013, 10:34 AM
  2. Other possible issues with if statements - book return program
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2012, 07:29 PM
  3. Where does the program start?
    By CrestaMan in forum Java Theory & Questions
    Replies: 12
    Last Post: May 10th, 2012, 12:16 PM
  4. Start application, and return process ID (PID)
    By prepared91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 03:06 AM
  5. tax return program..help finish please!!
    By dscrudato21xo in forum Loops & Control Statements
    Replies: 2
    Last Post: November 5th, 2009, 03:23 PM