Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 23rd, 2006, 04:24 PM
RGL RGL is offline
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping back to the beginning

Is there an easy way to go back to the beginning of a program to allow the user to enter another value for calculating?

I have a programm that asks the user for a numeric value and breaks it down. I want to ask them if they want to enter another number and by their sying "Yes" they'll be able to enter another value and calculate again if they want to or to say "No" to exit.

Would someone please let me know what code I need to make this happen?

 
Old May 23rd, 2006, 07:59 PM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can put a while loop around the whole program. e.g.

boolean continue = false;
while (continue == true)
{

//your code here

}

 
Old May 23rd, 2006, 08:00 PM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oops, don't set continue to false at the top or it will never enter the loop!

 
Old June 7th, 2006, 11:11 AM
Authorized User
 
Join Date: Oct 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to yescarthik
Default

Alternatively,
    You can use a "while(true)" loop and use break and continue statement with in your loop ..as in the following example.

import java.io.*;

class ToContinue
{
    public static void main(String ar[])
    {
        while(true)
        {
            try{
            System.out.println("Enter a number");
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            String str= bf.readLine();
            int val=Integer.parseInt(str);
            System.out.println("The value is" + val);
            System.out.println("Want to Continue");
            BufferedReader bf1 = new BufferedReader(new InputStreamReader(System.in));
            String str1= bf1.readLine();
            if(str1.equals("yes"))
                continue;
            else
                break;
            }
            catch(IOException ioe)
            {}
        }
    }
}

 
Old June 14th, 2006, 07:11 PM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yup the best method is a infinte loop . Inside the loop u would have a "break"' stmt to break the loop when the exiting conidtion occurs






Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping ssaranam SQL Server 2005 2 April 17th, 2008 01:40 AM
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
Looping..? dedex C# 2 January 6th, 2005 11:24 PM
history.back or hitting the back button won't work lian_a Classic ASP Basics 4 July 29th, 2004 12:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.