import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Employee { String employeeNo; String employeeName; String groupNo; String hourlyRate; String hoursWorked; String bonusStatus; PrintWriter pw; BufferedReader br; public void addEmployee() { Scanner scanner = new Scanner(System.in);// reading from console System.out.println("Enter name"); employeeName = scanner.next(); System.out.println("Enter no"); employeeNo = scanner.next(); System.out.println("Enter group no"); groupNo = scanner.next(); System.out.println("Enter horly rate"); hourlyRate = scanner.next(); System.out.println("Enter hours worked"); hoursWorked = scanner.next(); int ihw = Integer.parseInt(hoursWorked);// converting a string to a // number String stri = Integer.toString(ihw);// converting an integer to a string System.out.println("Enter Bonus status"); bonusStatus = scanner.next(); char c = bonusStatus.charAt(0); try { pw = new PrintWriter(new FileWriter("newly.txt", true));// writing // to a file // without // overwriting } catch (Exception e) { System.out.println(e.getMessage()); } String s = employeeName + "@" + employeeNo + "@" + groupNo + "@" + hourlyRate + "@" + hoursWorked + "@" + bonusStatus + "@";// String // concatenation pw.write(s); pw.flush(); } public void sortHourlyRate() { String l = null; String x = null; try { br = new BufferedReader(new FileReader("newly.txt"));// reading from // a file while ((l = br.readLine()) != null) { x = l; } String[] array = x.split("@");// using split method String[] arr = new String[array.length]; int i = 0; int j = 3; int k = 0; while (j < array.length - 2) { arr[i] = array[j]; j = j + 6; i++; } String a = null; while ((arr[k]) != null) { if (arr[k + 1] != null) { if (a == null) { a = arr[k] + "@" + arr[k + 1] + "@"; } else if (a != null) { a = a + arr[k + 1] + "@"; } } System.out.println(a); k++; } String[] array2 = a.split("@"); System.out.println(array2[0]); Arrays.sort(array2); System.out.println(Arrays.toString(array2)); } catch (Exception e) { System.out.println(e.getMessage()); } } public void modifyData() { String l = null; String x = null; try { br = new BufferedReader(new FileReader("newly.txt")); while ((l = br.readLine()) != null) { x = l; } System.out.println(x); String[] array = x.split("@"); Scanner scanner = new Scanner(System.in); System.out.println("please enter the employee name:"); String s = scanner.next(); int hs = s.hashCode(); for (int i = 0; i < array.length; i++) { if (array[i].hashCode() == hs) { System.out.println("please enter the new employee name:"); s = scanner.next(); array[i] = s; System.out.println("please enter the new employee no:"); s = scanner.next(); array[i + 1] = s; System.out.println("please enter the new group no:"); s = scanner.next(); array[i + 2] = s; System.out.println("please enter the new hourly rate:"); s = scanner.next(); array[i + 3] = s; System.out.println("please enter the new hours rate:"); s = scanner.next(); array[i + 4] = s; System.out.println("please enter the new bonus rate:"); s = scanner.next(); array[i + 5] = s; } } String g = null; int m = 0; for (m = 0; m < array.length; m++) { if (g == null) { g = array[m] + "@"; } else if (g != null) { g = g + array[m] + "@"; } } PrintWriter pwd = new PrintWriter(new FileWriter("newly.txt")); pwd.write(g); pwd.flush(); System.out.println(g); } catch (Exception e) { System.out.println(e.getMessage()); } } public static void main(String args[]) { new Employee().addEmployee(); new Employee().addEmployee(); new Employee().addEmployee(); new Employee().sortHourlyRate(); new Employee().modifyData(); } }
Sunday, December 19, 2010
IO and String manipulation example java code
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment