Results 1 to 3 of 3

Thread: Quick Java Help

  1. #1
    EdOBannon is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Quick Java Help

    For the following code how would I EXIT the program? If the input is equal to 3 I need to exit the program.

    Java Code:
    import java.util.Scanner; // needed to import Scanner class  
    import java.util.ArrayList;
      
    public class cpb09eTestDrive_A5 {  
        // Create Scanner  
        private static Scanner scan = new Scanner(System.in);  
        //Program Execution  
        public static void main(String[] args) {  
        	
        	ArrayList<cpb09eEmployee_A5> emplist = new ArrayList<cpb09eEmployee_A5>();
            
        	    	
        	//Prompt  
            System.out.println("Please indicate the type of employee by typing its number: ");  
            System.out.println("1 - Wage Worker");  
            System.out.println("2 - Executive");  
            System.out.println("3 - Exit");  
            //Read  
            
            String input = scan.nextLine();  
      
            if(input.equals("1")){  
                //create WageWorker object  
            	
            	cpb09eEmployee_A5 obj = new cpb09eEmployee_A5(); 		// create employee object and initialize it	
    			
            	cpb09eWageWorker_A5 wage = new cpb09eWageWorker_A5();
              		
    			System.out.println("Enter the Objects Name ");
              		String Name = scan.next();			// input name from user
              		wage.SetName(Name);					// set the name in employee class
              		
              			
              		System.out.println("Enter the Wage  ");
              		int Wage = scan.nextInt();			// input wage from user
              		wage.SetWage(Wage);   				// set wage of employee	
    
              		System.out.println("Enter the Hours ");
              		int hours = scan.nextInt();        // input hours from the user
              		wage.SetHours(hours);               //set the hours
              		
              		wage.SetSalary(hours * Wage);			// calculate and set the salary
              		
              	    emplist.add(wage);             		// set the object in array list
              	    
              	      	
            }  
            
            else if(input.equals("2")){  
                //create Executive object  
            	
            	cpb09eEmployee_A5 obj = new cpb09eEmployee_A5();	
        		
            	cpb09eExecutive_A5 exe_obj = new cpb09eExecutive_A5();
            	
            	System.out.println("Enter the Objects Name ");
            		String Name = scan.next();				
            		exe_obj.SetName(Name);					
            		
            	System.out.println("Enter the Salary ");
            		int Sal = scan.nextInt();				
            		exe_obj.SetSalary(Sal);					
            		
            	System.out.println("Enter the Title ");
            		String title = scan.next();        	
            		exe_obj.SetTitle(title);				
            	
            	emplist.add(exe_obj); 					
            	
           		
            }  
            
            else if(input.equals("3")){  
                //exit the program  
            }  
        }  
    }
    Last edited by pbrockway2; 11-06-2011 at 09:20 AM. Reason: code tags added

  2. #2
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: Quick Java Help

    maybe its weird but it is as simple as this-

    just write System.exit(0);

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Quick Java Help

    Did you see the answers given in your other thread? And understand them?

    That code doesn't need any change in order to exit if the user enters "3". And, please use "code" tags when you post code.

Similar Threads

  1. Quick how-to java applets
    By rperalta in forum Java Applets
    Replies: 1
    Last Post: 09-24-2011, 06:20 PM
  2. Java Quick Start
    By Duchess in forum New To Java
    Replies: 7
    Last Post: 04-26-2009, 11:49 AM
  3. A Java reference source for a quick look
    By tapies in forum New To Java
    Replies: 6
    Last Post: 10-03-2008, 06:01 PM
  4. Quick Java question/help
    By Zedy in forum New To Java
    Replies: 6
    Last Post: 04-22-2008, 03:40 AM
  5. Quick Job required in Java
    By taxman in forum Jobs Offered
    Replies: 0
    Last Post: 01-02-2008, 11: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
  •