Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: program not working

  1. #1
    Join Date
    Aug 2011
    Location
    Durban
    Posts
    5
    Rep Power
    0

    Default program not working

    I am learning java and I decided to challenge myself with a business program for listing employees and their ages using arrays. I named my files test and testone(the class with main).

    test.java
    Java Code:
    // africom business employee list
    
    import java.util.*;
    
    public class test
    {
    	 Scanner input = new Scanner(System.in);
    	 int number_of_empl;
    	 String emplName[] = new String[number_of_empl];
    	 int l = emplName.length;
    	 
    	 int age[] = new int[number_of_empl];
    	
    	public void number_of_emplyz(int num)
    	{
    		number_of_empl = num;
    	}
    	
    	public void setEmpl() // set employee name and age
    	{
    		
    		for(int count = 0; count < l; count++)
    		{
    			System.out.println("Enter name: ");
    			System.out.println();
    			emplName[count] = input.nextLine();
    			
    	
    		}
    		
    		for(int i=0; i<l; i++)
    		{
    			System.out.println("Enter " + age[i]);
    			System.out.println("'s age");
    		}
    	}
    	
    	public void greet() // print greet messege
    	{
    		System.out.println("WELCOME TO AFRICOM EMPLOYEE REGISTER");
    	}
    	
    	public void header() // print header
    	{
    		char tab = '\t';
    		
    		System.out.print("Name");
    		System.out.print(tab);
    		System.out.print("Surname");
    		System.out.print(tab);
    		System.out.print("Age");
    		System.out.println();
    	}
    	
    	public void getEmpl() // dispay table of employees
    	{
    		char tab = '\t';
    		for(int count = 0; count < emplName.length; count++)
    		{
    			System.out.println(emplName[count] + tab + age[count]);
    			
    			
    		}
    	}
    }
    testone.java
    Java Code:
    // testing the app
    import java.util.*;
    
    public class testone
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		test africom = new test();
    		africom.greet();
    		System.out.println("Enter number of Employees");
    		int x;
    		x = input.nextInt();
    		africom.number_of_emplyz(x);
    		africom.setEmpl();
    		africom.header();
    		africom.getEmpl();
    	}
    }
    output is
    Java Code:
    WELCOME TO AFRICOM EMPLOYEE REGISTER
    Enter number of Employees
    4
    Name    Surname Age
    
    Process completed.
    Please Help

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Do you have any questions or problems?
    What do you expect the output to look like?

    You need to show the full console/command prompt with the data that was entered into the program.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Last edited by Norm; 08-09-2011 at 06:40 PM.

  3. #3
    Join Date
    Aug 2011
    Location
    Durban
    Posts
    5
    Rep Power
    0

    Default

    Hey Norm. I am expecting the program to ask me to enter a name of the employees, using the for loop to ask me the names up to the number I gave it when it asks me "How may Employees". then it should ask me the age of each employee.
    Here is the console output
    Java Code:
    Microsoft Windows [Version 6.1.7600]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\africom computer 1>cd C:\Users\africom computer 1\Documents\JCreator Pr
    o\MyProjects\test\classes
    
    C:\Users\africom computer 1\Documents\JCreator Pro\MyProjects\test\classes>java
    testone
    WELCOME TO AFRICOM EMPLOYEE REGISTER
    Enter number of Employees
    5
    Name    Surname Age
    
    C:\Users\africom computer 1\Documents\JCreator Pro\MyProjects\test\classes>

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    The program uses the length of the array to determine how many times to ask for a name.
    Because of the way the program is coded, the length is 0. So it does not ask for any names.
    You have created the arrays BEFORE there is a size for them. When the class is created, the arrays are given the size as set by a variable with the value of 0.

    You need to change the logic so you don't create the arrays until you have a size to give them.
    Define the variables in the class:
    String emplName[]; // define the array variable

    and then when you have the size:

    emplName = new String[theSize]; // give the variable a value

  5. #5
    Join Date
    Aug 2011
    Location
    Durban
    Posts
    5
    Rep Power
    0

    Default

    ok let me edit my code and get back to you if i have further questions.
    Thanks in Advance bru.

  6. #6
    Join Date
    Aug 2011
    Location
    Durban
    Posts
    5
    Rep Power
    0

    Default

    Norm its not working. can you code it for me and run it and show me the output.

  7. #7
    Join Date
    Aug 2011
    Location
    Durban
    Posts
    5
    Rep Power
    0

    Default

    oh I think I got it. I will work on it too, through out the night. Thanks man.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Dax Le Foundry View Post
    Norm its not working. can you code it for me and run it and show me the output.
    Are you going to pay Norm for that? This is the way we all play the game: you do the coding; you get stuck; you ask a clear question and we try to help you out.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Program working fine until I add methods....
    By LadyMonsterFace in forum New To Java
    Replies: 3
    Last Post: 12-04-2010, 03:22 AM
  2. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  3. Replies: 1
    Last Post: 08-27-2009, 01:20 PM
  4. Working on a menu program...using exceptions
    By Nightwarrior in forum New To Java
    Replies: 0
    Last Post: 04-16-2009, 04:40 AM
  5. My program is not working
    By MICHAELABICK in forum New To Java
    Replies: 6
    Last Post: 12-22-2008, 11:05 PM

Posting Permissions

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