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

Thread: Need some help with creating a new object

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Need some help with creating a new object

    Basically, my code is a payroll program that keeps track of an employee's first and last names, salary, and address. I was doing fine up until I made my first object in the main class. My "Employee1" object seems to be the trouble, and I thought it might have been because I didn't have the .txt file in the right folder. I copied the .txt file to every folder of the project because this is my first time using NetBeans and it made so many folders I had no idea which one to put it in. Long story short, if I don't comment out everything involving "Employee1" then the program doesn't run. It gave me something along the lines of "no such element exception" if I remember correctly. I wish I could get the exact phrase but NetBeans won't even let me compile the program right now, so I can't. If anyone can help point me in the right direction it would be greatly appreciated.

    import java.util.*;
    import java.io.*;
    public class PayrollTest {
     
        public static void main(String[] args)throws IOException
        {
            Payroll Employee1 = new Payroll("payrollSample.txt");
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Please choose a function below."
                    + "\n\na - add new "
                    + "employee\nr - remove employee from the address book\nf - "
                    + "find an employee's information\np - print out payroll\nq "
                    + "- quit program");
            String input = keyboard.nextLine();
     
            if (input.equals("a"))
                addEmployee();
        }
     
        public static void addEmployee(){
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("What is the employee's first name?");
            String firstName = keyboard.nextLine();
     
            System.out.println("What is the employee's last name?");
            String lastName = keyboard.nextLine();
     
            System.out.println("What is the employee's salary?");
            double salary = keyboard.nextDouble();
     
            System.out.println("What is the employee's address?");
            String address = keyboard.nextLine();
     
            boolean success = Employee1.add(firstName, lastName, salary, address);
     
            if (success == true)
                System.out.println("The employee has been added.");
            else
                System.out.println("The employee is already in the system.");
     
        }
     
    }

    import java.util.*;
    import java.io.*;
    public class Payroll {
     
        private ArrayList<Employee> payList;
     
        public Payroll(){
            payList = new ArrayList<Employee>();
        }
     
        public Payroll(String filename)throws IOException{
            payList = new ArrayList<Employee>();
            Scanner fileInput = new Scanner(new File(filename));
            while (fileInput.hasNextLine()){
                String line = fileInput.nextLine();
                StringTokenizer st = new StringTokenizer(line, "~");
                String firstName = st.nextToken();
                String lastName = st.nextToken();
                double salary = Double.parseDouble(st.nextToken());
                String address = st.nextToken();
     
                payList.add(new Employee(firstName, lastName, salary, address));
            }
        }
     
        private int search(String firstName, String lastName){
            int i = 0;
            Employee tempEmployee = new Employee(firstName, lastName, 0.0, null);
            for (i = 0; i < payList.size(); i++){
                if (payList.get(i).equals(tempEmployee))
                    return i;
            }
            return -1;
        }
     
        public boolean add(String firstName, String lastName, double salary, 
                String address){
            int index = search(firstName, lastName);
            if (index == -1){
                payList.add(new Employee(firstName, lastName, salary, address));
                return true;
            }
            else
                return false;
        }
     
        public boolean remove(String firstName, String lastName){
            int index = search(firstName, lastName);
            if (index == -1)
                return false;
            return true;
        }
     
        public String findAllInfo(String firstName, String lastName){
            int index = search(firstName, lastName);
            if (index == -1)
                System.out.println("The Employee you requested does not work here");
            return payList.get(index).toString();
        }
     
        public void printPayRoll(){
            int i = 0;
            for (i = 0; i < payList.size(); i++){
            System.out.println(payList.get(i));
        }
        }
     
     
    }

    public class Employee {
     
        static int empcount = 0;
        private int empID = 0;
        private String firstName = null;
        private String lastName = null;
        private String address = null;
        private double salary = 0.0;
     
        public Employee(){
     
        }
     
        public Employee(String fname, String lname, double sal, String addr){
            firstName = fname;
            lastName = lname;
            address = addr;
            salary = sal;
        }
     
        public int getID(){
            return empID;
        }
     
        public String getFirstName(){
            return firstName;
        }
     
        public String getLastName(){
            return lastName;
        }
     
        public String getAddress(){
            return address;
        }
     
        public double getSalary(){
            return salary;
        }
     
        public void setFirstName(String fname){
            firstName = fname;
        }
     
        public void setLastName(String lname){
            lastName = lname;
        }
     
        public void setAddress(String addr){
            address = addr;
        }
     
        public String toString(){
            return firstName + "~" + lastName + "~" + salary + "~" + address;
        }
     
        public boolean equals (Object obj){
            return false;
        }
     
     
     
    }
    Last edited by bankston13; August 28th, 2012 at 03:25 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need some help with creating a new object

    We can't help much without having the exception. But if I had to guess, your problem is with the StringTokenizer. Can you provide us with a sample input?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Need some help with creating a new object

    After some time with the program, this is the only error it gives me.

    Error: Could not find or load main class PayrollTest
    Java Result: 1

    I changed the object variable in the PayrollTest class from "Employee1" to "storePayroll" for easier reading. I also added some methods to it, but it can't grab storePayroll in the methods like it can in the main. Sorry, but Netbeans won't let me compile the program for some reason, so I can't post a sample input/output without typing it out.

    import java.util.*;
    import java.io.*;
    public class PayrollTest {
     
        public static void main(String[] args)throws IOException
        {
            Payroll storePayroll = new Payroll("payrollSample.txt");
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Please choose a function below."
                    + "\n\na - add new "
                    + "employee\nr - remove employee from the address book\nf - "
                    + "find an employee's information\np - print out payroll\nq "
                    + "- quit program");
            String input = keyboard.nextLine();
     
            if (input.equals("a"))
                addEmployee();
            else if (input.equals("r"))
                removeEmployee();
            else if (input.equals("f"))
                findEmployee();
            else if (input.equals("p"))
                storePayroll.printPayRoll();
     
        }
     
        public static void addEmployee(){
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("What is the employee's first name?");
            String firstName = keyboard.nextLine();
     
            System.out.println("What is the employee's last name?");
            String lastName = keyboard.nextLine();
     
            System.out.println("What is the employee's salary?");
            double salary = keyboard.nextDouble();
     
            System.out.println("What is the employee's address?");
            String address = keyboard.nextLine();
     
            boolean success = storePayroll.add(firstName, lastName, salary, address);
     
            if (success == true)
                System.out.println("The employee has been added.");
            else
                System.out.println("The employee is already in the system.");
     
        }
     
        public static void removeEmployee(){
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("What is the employee's first name?");
            String firstName = keyboard.nextLine();
     
            System.out.println("What is the employee's last name?");
            String lastName = keyboard.nextLine();
     
            boolean success = storePayroll.remove(firstName, lastName);
     
            if (success == true)
                System.out.println("The employee has been removed.");
            else
                System.out.println("The employee is not in the system.");
        }
     
        public static void findEmployee(){
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("What is the employee's first name?");
            String firstName = keyboard.nextLine();
     
            System.out.println("What is the employee's last name?");
            String lastName = keyboard.nextLine();
     
            System.out.println(storePayroll.findAllInfo(firstName, lastName));
        }
     
     
    }

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need some help with creating a new object

    You must have a compiler error or something for it not to be able to find its own main. What happens when you attempt to compile it in netbeans?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Need some help with creating a new object

    It won't even let me compile the program. The option is grayed out, so I've been googleing around trying to see if I can solve the problem, but I haven't been able to. I'm still trying to.

    And now, it gives me this error instead:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:349)
    at Payroll.<init>(Payroll.java:26)
    at PayrollTest.main(PayrollTest.java:16)
    Java Result: 1


    I haven't changed anything else in my code, so I have no idea why the errors just changed like that.
    Last edited by bankston13; August 28th, 2012 at 04:49 PM.

Similar Threads

  1. Creating new Object Help!!!
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 27th, 2012, 01:59 PM
  2. Problem creating a new object
    By smithmar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2012, 05:19 AM
  3. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Creating an object...
    By RodePope4546 in forum Object Oriented Programming
    Replies: 6
    Last Post: June 27th, 2011, 06:44 AM