Results 1 to 10 of 10
  1. #1
    Shahed_R is offline Member
    Join Date
    Dec 2011
    Location
    Amman, Jordan
    Posts
    5
    Rep Power
    0

    Red face Assignment Help :)

    Hi, I'm very new to java and I have this assignment below:
    " Write a program that reads data from employee.txt file, then calculate annual raise for each employee, then print
    the name of each employee, his title, his current salary and his annual raise.

    Annual raise rules:
    programmer raise = current salary * 0.2
    manager raise = current salary * 0.3
    tester raise = current salary * 0.1
    president raise = current salary * 0.4

    and the employee.txt file includes the following data:
    #id, name, title, salary

    1, Ali, programmer, 500
    2, Ahmad, programmer, 600
    3, ameer, manager, 1000
    4, sameer, manager, 1300
    5, kareem, president, 3000
    6, john, tester, 400

    when I try running the code it gives me the following error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at org.tasks.task2.EmployeeFile.main(EmployeeFile.jav a:56)

    I'm lost here, can any one help please :)
    and am I writing the code in the correct way?




    Java Code:
     package org.tasks.task2;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class EmployeeFile
    {
        public static void main(String args[])
        {
            List<String> wordList = new ArrayList<String>();
            BufferedReader br = null;
    
            // surround with try & catch
            try
            {
                br = new BufferedReader(new FileReader("employee.txt"));
    
                String word;
    
                while ((word = br.readLine()) != null)
    
                    wordList.add(word);
    
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    br.close();
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
    
            // /
            String[] words = new String[wordList.size()];
            wordList.toArray(words);
    
            for (int i = 0; i < words.length; i++)
            {
                String myWord = words[i];
    
                // split comma from each line
                String[] parts = myWord.split(",");
    
                // converting the last element in the array to int
                int intResult = Integer.parseInt(parts[3]);
    
                // result is to calculate the annual raise for each row
                double result = 0;
    
                // parts[2] refers to the title in each row
                // result refers to the annual raise in each row
                if (parts[2] == "tester")
                {
                    result = intResult * 0.1;
                }
    
                if (parts[2] == "programmer")
                {
                    result = intResult * 0.2;
                }
    
                if (parts[2] == "manager")
                {
                    result = intResult * 0.3;
                }
    
                if (parts[2] == "president")
                {
                    result = intResult * 0.4;
                }
    
                for (int x = 0; x < parts.length; x++)
                    System.out.print(parts[x] + " " + result);
    
                System.out.print("\n");
    
            }
    
        }
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Assignment Help :)

    Do you know the meaning of the exception you ends up with?

    BTW, what you have on line number 56 of EmployeeFile class?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Assignment Help :)

    Is the following you found on line 56?

    Java Code:
    int intResult = Integer.parseInt(parts[3]);

  4. #4
    Shahed_R is offline Member
    Join Date
    Dec 2011
    Location
    Amman, Jordan
    Posts
    5
    Rep Power
    0

    Default Re: Assignment Help :)

    Java Code:
    int intResult = Integer.parseInt(parts[3]);
    the line is to convert the last element in each row on the string array to integer
    so I can perform the manipulation process on it
    parts[3] would be the last element ( the salary element in the employee.txt file)
    the salary must be manipulated to a specific value dependeing on the parts[2] value
    ( .2 if title is programmer ... etc)

  5. #5
    Shahed_R is offline Member
    Join Date
    Dec 2011
    Location
    Amman, Jordan
    Posts
    5
    Rep Power
    0

    Default Re: Assignment Help :)

    Ok, now the error was because it couldn't read the file
    cus it had some "space" & "#" marks on the employee.txt
    i added

    Java Code:
    //to skip space in employee.txt
    if (word.length() == 0)
     continue;
    
    //to skip lines starting with # in employee.txt
    if (word.charAt(0) == '#')
     continue;
    it's running now but not with the result i want,
    this line should remove the commas between the elements

    Java Code:
    String[] parts = myWord.split(",");
    it removes it and replace it with 0.0
    and i can't seem to get the result (annual raise) when running the code!
    what to do now :S
    the complete code:

    Java Code:
     package org.tasks.task2;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class EmployeeFile
    {
        public static void main(String args[])
        {
            List<String> wordList = new ArrayList<String>();
            BufferedReader br = null;
    
            try
            {
                br = new BufferedReader(new FileReader("employee.txt"));
    
                String word;
    
                while ((word = br.readLine()) != null)
                {
                    // to skip space in employee.txt
                    if (word.length() == 0)
                        continue;
    
                    // to skip lines starting with # in employee.txt
                    if (word.charAt(0) == '#')
                        continue;
    
                    wordList.add(word);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    br.close();
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
    
            String[] words = new String[wordList.size()];
            wordList.toArray(words);
    
            for (int i = 0; i < words.length; i++)
            {
                String myWord = words[i];
    
                // remove commas between elements
                String[] parts = myWord.split(",");
    
                String salary = parts[3].trim();
    
                int intResult = Integer.parseInt(salary);
    
                double result = 0;
    
                if (parts[2] == "tester")
                {
                    result = intResult * 0.1;
                }
    
                if (parts[2] == "programmer")
                {
                    result = intResult * 0.2;
                }
    
                if (parts[2] == "manager")
                {
                    result = intResult * 0.3;
                }
    
                if (parts[2] == "president")
                {
                    result = intResult * 0.4;
                }
    
                for (int x = 0; x < parts.length; x++)
                    System.out.print(parts[x] + " " + result);
    
                System.out.print("\n");
    
            }
    
        }
    
    }
    and the result:

    1 0.0 Ali 0.0 programmer 0.0 500 0.0
    2 0.0 Ahmad 0.0 programmer 0.0 600 0.0
    3 0.0 ameer 0.0 manager 0.0 1000 0.0
    4 0.0 sameer 0.0 manager 0.0 1300 0.0
    5 0.0 kareem 0.0 president 0.0 3000 0.0
    6 0.0 john 0.0 tester 0.0 400 0.0

  6. #6
    Shahed_R is offline Member
    Join Date
    Dec 2011
    Location
    Amman, Jordan
    Posts
    5
    Rep Power
    0

    Default Re: Assignment Help :)

    Ok, now i managed to print the result in the end of each row, without replacing the
    result (0.0) in the place of the deleted commas, but i'm not getting the result i want
    the code suppose to display the result according to the following code:
    Java Code:
        if (parts[2] == "tester")
    {
        result = intResult * 0.1;
    }
    
    if (parts[2] == "programmer")
    {
        result = intResult * 0.2;
    }
    
    if (parts[2] == "manager")
    {
        result = intResult * 0.3;
    }
    
    if (parts[2] == "president")
    {
        result = intResult * 0.4;
    }
    but i'm getting the initial value of it only!

    the complete code:

    Java Code:
     package org.tasks.task2;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class EmployeeFile
    {
        public static void main(String args[])
        {
            List<String> wordList = new ArrayList<String>();
            BufferedReader br = null;
    
            try
            {
                br = new BufferedReader(new FileReader("employee.txt"));
    
                String word;
    
                while ((word = br.readLine()) != null)
                {
                    // to skip space in employee.txt
                    if (word.length() == 0)
                        continue;
    
                    // to skip lines starting with # in employee.txt
                    if (word.charAt(0) == '#')
                        continue;
    
                    wordList.add(word);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    br.close();
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
    
            String[] words = new String[wordList.size()];
            wordList.toArray(words);
    
            for (int i = 0; i < words.length; i++)
            {
                String myWord = words[i];
    
                // remove commas between elements
                String[] parts = myWord.split(",");
    
                String salary = parts[3].trim();
    
                int intResult = Integer.parseInt(salary);
    
                double result = 0;
    
                if (parts[2] == "tester")
                {
                    result = intResult * 0.1;
                }
    
                if (parts[2] == "programmer")
                {
                    result = intResult * 0.2;
                }
    
                if (parts[2] == "manager")
                {
                    result = intResult * 0.3;
                }
    
                if (parts[2] == "president")
                {
                    result = intResult * 0.4;
                }
    
                for (int x = 0; x < parts.length; x++)
                    System.out.print(parts[x] + " ");
                System.out.print(result);
                System.out.print("\n");
    
            }
    
        }
    
    }
    the result i'm getting:

    1 Ali programmer 500 0.0
    2 Ahmad programmer 600 0.0
    3 ameer manager 1000 0.0
    4 sameer manager 1300 0.0
    5 kareem president 3000 0.0
    6 john tester 400 0.0

    i

  7. #7
    Shahed_R is offline Member
    Join Date
    Dec 2011
    Location
    Amman, Jordan
    Posts
    5
    Rep Power
    0

    Default Re: Assignment Help :)

    I fixed it, i should've user .equals instead of ==
    :)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Assignment Help :)

    Yes, there is a difference between == and equal(). If you have a time read bit about it. Really worth to read mate.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Assignment Help :)

    I wrote a simple code segment. Have a look at,

    Java Code:
    public class EqualDemo {
        public static void main(String args[]) {
            String s1 = "Hello";
            String s2 = new String(s1);
            String s3 = "Hello";
    
            System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
    
            System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
            
            System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
    
            System.out.println(s1 + " == " + s3 + " -> " + (s1 == s3));
        } 
    }

  10. #10
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Assignment Help :)

    Why do you have a try block inside a finally block to close the bufferreader?
    I think you should just do finallly and close the bufferreader because there is no point of trying it if you want it to be executed..

Similar Threads

  1. assignment help
    By xyknight in forum New To Java
    Replies: 6
    Last Post: 03-13-2011, 07:19 PM
  2. help with an assignment!
    By Tek in forum New To Java
    Replies: 6
    Last Post: 10-24-2010, 11:32 PM
  3. Need with my assignment ...
    By allergy01 in forum New To Java
    Replies: 1
    Last Post: 04-25-2009, 08:33 AM
  4. Can somebody help me in my assignment
    By coolstruxx in forum NetBeans
    Replies: 0
    Last Post: 03-24-2009, 01:27 AM
  5. I am looking for help with an assignment
    By nanoo51969 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:41 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
  •