Results 1 to 9 of 9
Thread: input prompt
- 08-24-2008, 08:40 PM #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();
BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
// 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.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();
}
}
- 08-24-2008, 09:52 PM #2
I only see the one read being done in your program.yr = myIn.readLine();
If you want to have more, then you'll have to call the readLine() method again.
- 08-24-2008, 10:26 PM #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
- 08-24-2008, 11:05 PM #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.
- 08-25-2008, 12:22 AM #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.
- 08-25-2008, 02:07 AM #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.
- 08-25-2008, 03:54 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
This is the way how to use do-while loop when you don't know the number of iterations you have to done.
As Norm says, it's better to read more about loops. There are lot of ways to do this.Java 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);
- 08-25-2008, 04:02 AM #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.
- 08-25-2008, 04:22 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In most of the cases we had to have use loops. They are really important actually. And many more too.
Similar Threads
-
How to use DirectoryDialog to prompt for a directory
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:00 PM -
How to use FileDialog to prompt for a file name (to save)
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 07:59 PM -
Problem during executing Command Prompt
By keshari in forum Advanced JavaReplies: 4Last Post: 06-05-2008, 04:06 AM -
How do I show * in a password prompt?
By tanalyw in forum New To JavaReplies: 3Last Post: 04-16-2008, 02:09 PM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks