Results 1 to 8 of 8
Thread: program not working
- 08-09-2011, 07:19 PM #1
Member
- Join Date
- Aug 2011
- Location
- Durban
- Posts
- 5
- Rep Power
- 0
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]); } } }
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(); } }
Java Code:WELCOME TO AFRICOM EMPLOYEE REGISTER Enter number of Employees 4 Name Surname Age Process completed.
- 08-09-2011, 07:38 PM #2
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 07:40 PM.
- 08-09-2011, 08:17 PM #3
Member
- Join Date
- Aug 2011
- Location
- Durban
- Posts
- 5
- Rep Power
- 0
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>
- 08-09-2011, 08:39 PM #4
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
- 08-09-2011, 08:44 PM #5
Member
- Join Date
- Aug 2011
- Location
- Durban
- Posts
- 5
- Rep Power
- 0
ok let me edit my code and get back to you if i have further questions.
Thanks in Advance bru.
- 08-09-2011, 09:07 PM #6
Member
- Join Date
- Aug 2011
- Location
- Durban
- Posts
- 5
- Rep Power
- 0
Norm its not working. can you code it for me and run it and show me the output.
- 08-09-2011, 09:09 PM #7
Member
- Join Date
- Aug 2011
- Location
- Durban
- Posts
- 5
- Rep Power
- 0
oh I think I got it. I will work on it too, through out the night. Thanks man.
- 08-09-2011, 09:10 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Similar Threads
-
Program working fine until I add methods....
By LadyMonsterFace in forum New To JavaReplies: 3Last Post: 12-04-2010, 04:22 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 07:53 PM -
Driving me insane - Program not working as it should ><
By mainy in forum New To JavaReplies: 1Last Post: 08-27-2009, 02:20 PM -
Working on a menu program...using exceptions
By Nightwarrior in forum New To JavaReplies: 0Last Post: 04-16-2009, 05:40 AM -
My program is not working
By MICHAELABICK in forum New To JavaReplies: 6Last Post: 12-23-2008, 12:05 AM
Bookmarks