# input prompt

• 08-24-2008, 08:40 PM
angelbaby21
input prompt
Hello...can anyone tell me how to get the input prompt to prompt again and again after the first input prompt is done. Here is what I have so far...

import java.io.*;
import java.util.GregorianCalendar;

public class Calendar
{

/**
* @param args
*/

public static void main(String[] args) throws IOException
{
GregorianCalendar gc = new GregorianCalendar();
System.out.println();
System.out.println("Welcome to my day!");
System.out.println("Day planner for Karen Ascher");
int month = gc.get(GregorianCalendar.MONTH)+1;
int day = gc.get(GregorianCalendar.DAY_OF_MONTH);
int year = gc.get(GregorianCalendar.YEAR);
String yr;
boolean isLeapYear;
System.out.println("" + month + "/" + day + "/"+ year );
System.out.println();

// divisible by 4
isLeapYear = (year % 4 == 0);

// divisible by 4 and not 100
isLeapYear = isLeapYear && (year % 100 != 0);

// divisible by 4 and not 100 unless divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0);

System.out.println();
year = Integer.parseInt(yr);
System.out.println();

if(gc.isLeapYear(year))

System.out.println("The year" + " " + yr +" "+ "is a leap year!");
else
System.out.println("The year" + " " + yr +" "+ "is not a leap year!");
System.out.println();

}
}
• 08-24-2008, 09:52 PM
Norm
Quote:

If you want to have more, then you'll have to call the readLine() method again.
• 08-24-2008, 10:26 PM
angelbaby21
input prompt
so if I want to have the input prompt 10 times I have to call the readline ten times. Isn't there a loop or something to repeat the prompt. I've already tried the latter. Thanks
• 08-24-2008, 11:05 PM
Norm
Yes, you can put the readLine prompt in a loop. The problem will be asking a different question before each read.
Normally you'll have to present a query for input and follow it by a read.
• 08-25-2008, 12:22 AM
angelbaby21
input prompt
yes but I want to ask the same question because the answers will be different. I am prompting the user for a different year each time to see if it is a leap year. I'm not sure how to do a loop.
• 08-25-2008, 02:07 AM
Norm
There are a couple of ways to do a loop.
The for loop is used when you know how many times you want to loop. The while loop is used until a condition is met. The do{} while loop is done once before the condition is tested for.
What textbook are you using? You will need one if you're going to learn to program. What you get here won't be enough.

Use Search on the forum for the keywords I mentioned: for( and while( to get sample programs that use loops.
• 08-25-2008, 03:54 AM
Eranga
This is the way how to use do-while loop when you don't know the number of iterations you have to done.

Code:

```        do {             System.out.print("Please enter a year..." );             System.out.println();             yr = myIn.readLine();             year = Integer.parseInt(yr);             System.out.println();             if(gc.isLeapYear(year))                 System.out.println("The year" + " " + yr +" "+ "is a leap year!");             else                 System.out.println("The year" + " " + yr +" "+ "is not a leap year!");             System.out.println();         }while(true);```
As Norm says, it's better to read more about loops. There are lot of ways to do this.
• 08-25-2008, 04:02 AM
angelbaby21
input prompt
Perfect...thanks much. The book I'm using is Java Programming Third edition - Shelley Cashman series. I actually had Java 2 years ago but just trying to brush up. I think we had pretty much for loops.
• 08-25-2008, 04:22 AM
Eranga
In most of the cases we had to have use loops. They are really important actually. And many more too.