Results 1 to 9 of 9

Thread: input prompt

  1. #1
    angelbaby21 is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

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

    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    yr = myIn.readLine();
    I only see the one read being done in your program.
    If you want to have more, then you'll have to call the readLine() method again.

  3. #3
    angelbaby21 is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    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. #5
    angelbaby21 is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default 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. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    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. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

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

  8. #8
    angelbaby21 is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default 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. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 0
    Last Post: 07-02-2008, 08:00 PM
  2. Replies: 0
    Last Post: 07-02-2008, 07:59 PM
  3. Problem during executing Command Prompt
    By keshari in forum Advanced Java
    Replies: 4
    Last Post: 06-05-2008, 04:06 AM
  4. How do I show * in a password prompt?
    By tanalyw in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:09 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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