Results 1 to 3 of 3
  1. #1
    soradogoof is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default My program terminates before letting me enter an answer

    I am writing a program that reads a year from the keyboard and says if it is a leap year or not. After it tells you, it asks if you want to check another year, however the program terminates before accepting an answer.
    This is the program:
    //-----------------------------------
    //Programmer: Zach Powers
    //Date: 9/21/2011
    //This program does alot, read PP5.2 on page 286 of Java Software Solutions for more info.
    //----------------------------------------------------------------/
    import java.util.Scanner;

    public class PP5_2
    {
    public static void main(String[] args)
    {
    int year;
    String answer;
    Scanner scan = new Scanner(System.in);
    System.out.println("Would you like to check a year (y/n)?");
    answer = scan.nextLine();
    while (answer.equalsIgnoreCase("y"))
    {
    System.out.println("Enter a year: ");
    year = scan.nextInt();
    if (year >= 1582)
    {
    if (year%4 == 0)
    {
    if (year%100 == 0)
    {
    if (year%400 == 0)
    {
    System.out.println("That year is a leap year.");
    }
    else
    {
    System.out.println("That year is not a leap year.");
    }
    }
    else
    {
    System.out.println("That year is a leap year");
    }
    }
    else
    {
    System.out.println("That year is not a leap year");
    }
    }
    else
    {
    System.out.println("That year was before leap years existed!");
    }
    System.out.println();
    System.out.print ("Would you like to check another year? (y/n)");
    answer = scan.nextLine();
    }
    }
    }



    And this is the output I receive:
    ----jGRASP exec: java PP5_2

    Would you like to check a year (y/n)?
    y
    Enter a year:
    2000
    That year is a leap year.

    Would you like to check another year? (y/n)
    ----jGRASP: operation complete.

  2. #2
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default Re: My program terminates before letting me enter an answer

    When you type something into the console and hit enter it consists of what you type and the enter key even though you don't see it. So if you type "1996" and hit enter what really got input was "1996/n". When you read an integer using nextInt() it leaves behind that newline sequence (/n) because it only reads in integers. Now when you call nextLine() it reads in that newline sequence that was left behind and since it's not equal to "y" the program exits. To solve this you have to call nextLine() after the call to nextInt() to get rid of the newline sequence. Something like this:
    Java Code:
    year = scan.nextInt();
    scan.nextLine();
    Just so you know, the Advanced Java section is meant for more advanced problems. This thread should probably be in the New to Java section. Also when posting code it's best to use the [code][/code] tags so that your code stays formatted and is easier to read. Just paste your code in between those tags.
    Last edited by Zman3359; 09-22-2011 at 01:06 AM. Reason: Using [noparse][code][/code][/noparse] instead of [ code ][ / code ]

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: My program terminates before letting me enter an answer

    Moved from "Advanced Java"

    Please post beginner questions in this section.

    db

Similar Threads

  1. Replies: 3
    Last Post: 03-13-2012, 09:28 AM
  2. Letting my program command the command prompt
    By Smokey. in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-29-2011, 11:34 PM
  3. Replies: 3
    Last Post: 08-19-2009, 03:54 AM
  4. Replies: 2
    Last Post: 07-13-2009, 04:45 AM
  5. how do you bypass the enter(/n) key in a program?
    By mtwisd in forum New To Java
    Replies: 1
    Last Post: 04-24-2009, 08:50 AM

Tags for this Thread

Posting Permissions

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