1. Member
Join Date
Aug 2008
Posts
4
Rep Power
0

## 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();

}
}

If you want to have more, then you'll have to call the readLine() method again.

3. Member
Join Date
Aug 2008
Posts
4
Rep Power
0

## 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

4. 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.

5. Member
Join Date
Aug 2008
Posts
4
Rep Power
0

## 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.

6. 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.

7. This is the way how to use do-while loop when you don't know the number of iterations you have to done.

Java Code:
```        do {
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();
}while(true);```
As Norm says, it's better to read more about loops. There are lot of ways to do this.

8. Member
Join Date
Aug 2008
Posts
4
Rep Power
0

## 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.

9. In most of the cases we had to have use loops. They are really important actually. And many more too.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•