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 9 of 9

Thread: Help with Looping out of a user saying the word NEW in the console

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Looping out of a user saying the word NEW in the console

    I need to know how to make my code repeat if the user types in NEW at the end of the program. Anybody know how? Please it is for a class

    Code:




    import java.util.Scanner;



    public class Calc {


    static Scanner trap = new Scanner (System.in);

    public static void main( String args []) throws InterruptedException
    {
    //Variables: Base 1, Base 2, Height //



    double B1;
    double B2;
    double height;


    System.out.println("What is the length of the top of your trapezoid?");
    B1 = trap.nextDouble();

    System.out.println("What is the length of the bottom of your trapezoid? ");
    B2 = trap.nextDouble();

    System.out.println("What is the height of your trapezoid?");
    height = trap.nextDouble();

    double b3 = B1 + B2;

    double almArea = b3 / 2;

    double area = almArea * height;

    System.out.println("The area of your trapezoid is:" + area);

    Thread.sleep(2000);

    System.out.println("Would you like to use me again? Type NEW if yes.");



    }








    }


  2. #2
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    you could make a boolean and use a while loop

    and you need to

    System.out.println("Would you like to use me again? Type NEW if yes.");
    //do something here to accept the users input to know what they typed and what do they do if they dont want to use it again? and why not type yes if yes(just curious)?

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    Quote Originally Posted by derekxec View Post
    you could make a boolean and use a while loop

    and you need to

    System.out.println("Would you like to use me again? Type NEW if yes.");
    //do something here to accept the users input to know what they typed and what do they do if they dont want to use it again? and why not type yes if yes(just curious)?


    Could you please explain to me how I would do that?

  4. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    booleans are true or false

    boolean rerun = true;

    then put your code you would like to restart inside a while loop...here is how to use a while loop
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    you could use the do while loop also

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    Quote Originally Posted by derekxec View Post
    booleans are true or false

    boolean rerun = true;

    then put your code you would like to restart inside a while loop...here is how to use a while loop
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    you could use the do while loop also
    Not quite understanding the while loop thing. Any chance you can write it for me in the code, so I can understand its realation to my code?

    Thanks man

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,307
    My Mood
    Mellow
    Thanks
    252
    Thanked 779 Times in 764 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    Try this to improve your understanding. Applying your understanding to your code is your task, not someone else's.

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    Quote Originally Posted by GregBrannon View Post
    Try this to improve your understanding. Applying your understanding to your code is your task, not someone else's.

    Yes, mate. But after reading that I still am not sure how I can get this to work in my program.

  8. #8
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    this is a while loop

    while (insert a boolean here) {
    /*this is where you put all the code you want to loop while the boolean is true
    when all this code is executed it checks to see if the boolean is true still and if it is it executes the code here again until the boolean is false*/
    }

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Looping out of a user saying the word NEW in the console

    Quote Originally Posted by derekxec View Post
    this is a while loop

    while (insert a boolean here) {
    /*this is where you put all the code you want to loop while the boolean is true
    when all this code is executed it checks to see if the boolean is true still and if it is it executes the code here again until the boolean is false*/
    }
    Thanks mate I got it to work!

Similar Threads

  1. Replies: 7
    Last Post: March 29th, 2013, 08:38 AM
  2. Replies: 1
    Last Post: November 2nd, 2012, 03:21 PM
  3. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 04:09 AM
  4. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 02:47 PM
  5. How to Read user input from the console with InputStreamReader in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:48 AM

Tags for this Thread