Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Simple Question: How do I input an exit code into my program?

  1. #1
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Simple Question: How do I input an exit code into my program?

    I just want to know how I can get my program to end when the user inputs "0." By using: System.exit(0);

    import java.util.Scanner; 
     
    public class TestCode
    {    
        public static void main (String [ ]args )
        {
            Scanner console =new Scanner(System.in); 
            System.out.println("To exit program input 0");   
            for (int y = 1; y < 11; y++ ){ // Executes output 10 times 
     
     
            System.out.println("Enter the year please:");     
            int year = console.nextInt(); 
     
     
            if (year >= 1900) {
            boolean answer = isLeapYear (year);
                if(answer) {      
     
           System.out.println("The given year is a leap year"); } 
               else { 
           System.out.println("The given year is not a leap year"); }  }
               else {       
           System.out.println("Value is invalid! Number is less than 1900.");     
       }           
      }
     }         
        public static boolean isLeapYear(int y) //y = year
         {
            if ((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0))) // Algorithim used for calculating leap years.
            {
                   return true;
            } else {
                   return false;
           }    
       }
    }

    Any help is appreciated, thank you for your time .


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Simple Question: How do I input an exit code into my program?

    You already check the input, just add another check.

  3. The Following User Says Thank You to Harry Blargle For This Useful Post:

    Knowledge_Feeds_The_Mind (March 12th, 2014)

  4. #3
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Simple Question: How do I input an exit code into my program?

    That solved it, thanks a bunch.

Similar Threads

  1. [SOLVED] Newbie question, simple program
    By feelingroovyslc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 20th, 2013, 09:24 AM
  2. Simple question about a small program
    By azizmaiden in forum Java Theory & Questions
    Replies: 5
    Last Post: March 2nd, 2013, 05:52 PM
  3. Simple question re while loop and input
    By amsh in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 12th, 2012, 03:09 PM
  4. Simple Input Dialog Question
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: March 1st, 2010, 11:10 PM
  5. Simple Input/Output program Acting weird
    By drexasaurus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 02:15 PM