Results 1 to 10 of 10
Thread: Assignment Help :)
- 12-19-2011, 10:27 AM #1
Member
- Join Date
- Dec 2011
- Location
- Amman, Jordan
- Posts
- 5
- Rep Power
- 0
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"); } } }
- 12-19-2011, 10:43 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 12-19-2011, 10:44 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Assignment Help :)
Is the following you found on line 56?
Java Code:int intResult = Integer.parseInt(parts[3]);
- 12-19-2011, 10:53 AM #4
Member
- Join Date
- Dec 2011
- Location
- Amman, Jordan
- Posts
- 5
- Rep Power
- 0
Re: Assignment Help :)
the line is to convert the last element in each row on the string array to integerJava Code:int intResult = Integer.parseInt(parts[3]);
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)
- 12-19-2011, 11:37 AM #5
Member
- Join Date
- Dec 2011
- Location
- Amman, Jordan
- Posts
- 5
- Rep Power
- 0
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
it's running now but not with the result i want,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;
this line should remove the commas between the elements
it removes it and replace it with 0.0Java Code:String[] parts = myWord.split(",");
and i can't seem to get the result (annual raise) when running the code!
what to do now :S
the complete code:
and the result: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"); } } }
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
- 12-19-2011, 11:49 AM #6
Member
- Join Date
- Dec 2011
- Location
- Amman, Jordan
- Posts
- 5
- Rep Power
- 0
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:
but i'm getting the initial value of it only!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; }
the complete code:
the result i'm getting: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"); } } }
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
- 12-19-2011, 12:28 PM #7
Member
- Join Date
- Dec 2011
- Location
- Amman, Jordan
- Posts
- 5
- Rep Power
- 0
Re: Assignment Help :)
I fixed it, i should've user .equals instead of ==
:)
- 12-20-2011, 03:15 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 12-20-2011, 03:21 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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)); } }
- 12-20-2011, 05:21 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
assignment help
By xyknight in forum New To JavaReplies: 6Last Post: 03-13-2011, 07:19 PM -
help with an assignment!
By Tek in forum New To JavaReplies: 6Last Post: 10-24-2010, 11:32 PM -
Need with my assignment ...
By allergy01 in forum New To JavaReplies: 1Last Post: 04-25-2009, 08:33 AM -
Can somebody help me in my assignment
By coolstruxx in forum NetBeansReplies: 0Last Post: 03-24-2009, 01:27 AM -
I am looking for help with an assignment
By nanoo51969 in forum New To JavaReplies: 1Last Post: 03-23-2009, 09:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks