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 2 of 2

Thread: Need help With program

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Need help With program

    Hi Guys, I am a noob at this java programming thing. I have 3 classes. When My program runs, it works good the first time when i Choose option 1(only option that is working for now). But after a loop, the data is over written, what can i do to keep my data even the counter. I understand why because every loop it does, it reinitialize everything which erases everything. I cannot think of a solution (kinda new in this).


     
     
    package comp2500_1;
    import java.util.Scanner;
    public class EmployeeApplication {
    Scanner input=new Scanner(System.in);
     Company x=new Company();
     
     
     
        public static void main(String[] args) {
            test p=new test();
            EmployeeApplication c=new EmployeeApplication();
            int choice =10,num=100;
     
     
            while (num!=0){
            num=c.choices(choice);
            c.sets(num);   
            }
     
         }
     
     
     
        public int choices(int choice){
            while (choice !=0){
        System.out.println("Please Enter Your Choice\n");
        System.out.println("Add a new employee to the company 1");
        System.out.println("Query for a particular employee 2");
        System.out.println("Raise an employee's Salary 3");
        System.out.println("List all the employees at the company 4");
        System.out.println("To Exit Press 0");
         choice=input.nextInt();
        return choice;
            }
            return 0;
        }
     
        public void sets(int s){
     
            if (s==1)
            {
                System.out.println(" Enter the persons First Name,Last Name and Salary");
                String fname=input.next();
                String lname=input.next();
                double num=input.nextDouble();
                x.addEmployee(fname, lname, num);
            }  
            else if (s==2)
            {
                System.out.println("Please enter the employee ID");
                int num2=input.nextInt();
              //  d.getEmployee(num2);
            }
            else if (s==3)
            {
                System.out.println("Please enter the employees ID");
                int num3=input.nextInt();
                System.out.println("Please enter the percentage increase of salary");
                double sal=input.nextDouble();
                x.raiseSalary(num3, sal);
            }
            else if (s==4)
            {
              //  d.getEmployees();
            }
        }
    }

    Thats 1 class and the other is

     
    public class Employee {
        private String firstName,lastName;
        private double salary;
        private int ID=990;
    private String[] EmpF=new String[100],EmpL=new String[100];
    private double[] EmpSal=new double[100];
    private int counter=0;    
        EmployeeApplication j=new EmployeeApplication();
     
     
     
     
        public Employee(String firstName, String lastName, double salary){
     
            setFName(firstName);
            setLName(lastName);
            setSal(salary);
           counter++;
     
        }
     
     
     
     public Employee() {
     
        }
     
     
        public String toString(){
            return String.format("%s %s %f,",firstName, lastName, salary);
        }
        public void raiseSalary(double percentIncrease){
            salary=salary+(salary*(percentIncrease/100));
        }
     
        public void setSal(double d)
        {
            EmpSal[counter]=d;
            salary=d;
        }
        public void setFName(String fN)
        {
            EmpF[counter]=fN;
            firstName=fN;
        }
        public void setLName(String lN)
        {
            EmpL[counter]=lN;
            lastName=lN;
        }
        public boolean getEmployee(int employeeID){
            employeeID=((employeeID-ID)/10)-1;
            if(EmpF[employeeID]==null)
                return false;
            else
                return true;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help With program

    But after a loop, the data is over written, what can i do to keep my data
    What variable has the data the you want to keep?
    One way to keep data from within a loop is to use an array or arraylist. Each time the code goes around the loop it could change the index to the array so that the new data goes into the next slot in the array and the old data is kept in the previous slot in the array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 03:56 PM
  2. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  3. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  4. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 9th, 2012, 12:47 AM
  5. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM