kilam_deelaw

Untitled

Apr 5th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. package employee;
  2. import java.util.*;
  3. import java.io.*;
  4. public class Employee <U,V> implements Comparable <Employee> {
  5. private U a;
  6. private V b;
  7.  
  8. public Employee(U a, V b) {
  9. this.a = a;
  10. this.b = b;
  11. }
  12.  
  13. public U getA() {
  14. return a;
  15. }
  16.  
  17. public void setA(U a) {
  18. this.a = a;
  19. }
  20.  
  21. public V getB() {
  22. return b;
  23. }
  24.  
  25. public void setB(V b) {
  26. this.b = b;
  27. }
  28.  
  29.  
  30. public static void main(String[] args) {
  31. ArrayList <Employee> arr = new ArrayList<Employee>();
  32. arr.add (new Employee ("Ali",1));
  33. arr.add (new Employee ("Ahmad",2));
  34. arr.add (new Employee ("Xhmad",5));
  35. arr.add(new Employee (2,"Saim"));
  36. arr.add(new Employee (1,"Xaim"));
  37. arr.add(new Employee (9,"Gaim"));
  38. Collections.sort(arr);
  39. for(Employee x:arr)
  40. System.out.println(x.getA()+" "+x.getB());
  41. }
  42.  
  43. @Override
  44. public int compareTo(Employee o) {
  45. String one="",two="";
  46. int a=0,b=0;
  47. if (o.getA() instanceof String && this.getA() instanceof String){
  48. one = (String) o.getA();
  49. two = (String) this.getA();
  50. return two.compareTo(one);
  51. }
  52. /* else
  53. if (o.getB() instanceof String && this.getB() instanceof String){
  54. one = (String) o.getB();
  55. two = (String) this.getB();
  56. return one.compareTo(two);
  57. }*/
  58. /* else
  59. if (o.getA() instanceof String && this.getB() instanceof String){
  60. one = (String) o.getA();
  61. two = (String) this.getB();
  62. return one.compareTo(two);
  63. }*/
  64. if(o.getA() instanceof Integer && this.getA() instanceof Integer)
  65. {
  66. a=(int) o.getA();
  67. //System.out.println(this.getA());
  68. b=Integer.parseInt((String.valueOf(this.getA())));
  69. return b-a;
  70. }
  71. return 0;
  72. }
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79. Upper to Lower, Lower to Upper
  80. package st2;
  81. public class St2 {
  82.  
  83. public static void main(String[] args) {
  84.  
  85. String s="HelLo woRld @ 2017";
  86. int i;
  87. char[]c=s.toCharArray();
  88. char[] k=new char[100];
  89. int a=c.length;
  90. for(i=0;i<a;i++)
  91. {
  92. if(Character.isUpperCase(c[i]))
  93. {
  94. c[i]=Character.toLowerCase(c[i]);
  95. }
  96. else if(Character.isLowerCase(c[i]))
  97. {
  98. c[i]= Character.toUpperCase(c[i]);
  99. }
  100. }
  101. for(i=0;i<a;i++)
  102. {
  103. System.out.print(c[i]);
  104. }
  105. }
  106. }
  107.  
  108.  
  109.  
  110.  
  111. import java.io.*;
  112. import java.util.ArrayList;
  113. import java.util.Arrays;
  114. import java.util.Collections;
  115. import java.util.StringTokenizer;
  116. /**
  117. * Created by test on 3/14/2017.
  118. */
  119. public class Main{
  120. public static void main(String[] args) throws Exception {
  121. File country = new File("out\\country.txt");
  122. File capital = new File("out\\capital.txt");
  123. File combine = new File("out\\combine.txt");
  124. FileReader f1 = new FileReader(country);
  125. BufferedReader bf1 = new BufferedReader(f1);
  126. String str = new String();
  127. ArrayList<String> list = new ArrayList<String>();
  128. int i = 0;
  129. while((str = bf1.readLine()) != null) {
  130. list.add(str);
  131. i++;
  132. }
  133. Collections.sort(list);
  134. bf1.close();
  135. f1.close();
  136. FileWriter wcountry= new FileWriter(country);
  137. BufferedWriter bwcountry = new BufferedWriter(wcountry);
  138. for (String ss: list) {
  139. bwcountry.write(ss + "\n");
  140. }
  141. bwcountry.close();
  142. wcountry.close();
  143. FileWriter w1 = new FileWriter(capital);
  144. BufferedWriter bw1 = new BufferedWriter(w1);
  145. String delimiter = " ";
  146. String comb = new String();
  147. for (int k = 0; k < i; k++) {
  148. int j =0;
  149. FileReader f2 = new FileReader(combine);
  150. BufferedReader bf2 = new BufferedReader(f2);
  151. while((comb = bf2.readLine()) != null) {
  152. StringTokenizer st = new StringTokenizer(comb, delimiter);
  153. String name = st.nextToken();
  154. String name2 = null;
  155. if(name.equals(list.get(k))) {
  156. name2 = st.nextToken();
  157. bw1.write(name2 + "\n");
  158. }
  159. j++;
  160. }
  161. bf2.close();
  162. f2.close();
  163. }
  164. bw1.close();
  165. w1.close();
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. package sorting.and.deleting.in.file;
  173.  
  174. import java.util.*;
  175. import java.io.*;
  176. public class SortingAndDeletingInFile {
  177.  
  178. public static void main(String[] args)throws IOException {
  179. Scanner sc=new Scanner(System.in);
  180. File fr=new File("File.txt");
  181. File fw=new File("Temp.txt");
  182. BufferedWriter bw=new BufferedWriter(new FileWriter(fw));
  183. BufferedReader br=new BufferedReader(new FileReader(fr));
  184. String str1=new String();
  185. String LinetoDelete="";
  186. String s=new String();
  187. System.out.println("Enter a word to delete:\n");
  188. s=sc.nextLine();
  189. while((str1=br.readLine())!=null){
  190. if (str1.contains(s))
  191. str1 = str1.replace(s, "");
  192. bw.write(str1);
  193. bw.newLine();
  194.  
  195. }
  196. br.close();
  197. bw.close();
  198. fr.delete();
  199. fw.renameTo(fr);
  200. }
  201. }
  202.  
  203.  
  204.  
  205. for deleteing whole line:
  206. LinetoDelete=str1.trim();
  207. if(LinetoDelete.contains(s))continue;
  208. bw.write(str1);
  209.  
  210.  
  211.  
  212.  
  213. package x; 
  214.  
  215. import java.io.BufferedReader; 
  216. import java.io.BufferedWriter; 
  217. import java.io.FileReader; 
  218. import java.io.FileWriter; 
  219. import java.util.ArrayList; 
  220. import java.util.Collections; 
  221. import java.util.Comparator; 
  222. import java.util.Scanner; 
  223.  
  224. public class X { 
  225.     
  226.     public static void main(String[] args) throws Exception { 
  227.     
  228.         ArrayList arr= new ArrayList(); 
  229.         Scanner sc = new Scanner(System.in);         
  230.         String name ="words.txt";                    //**FILE NAME** 
  231.         String line=null; 
  232.         FileReader fr = new FileReader(name); 
  233.         BufferedReader br = new BufferedReader(fr); 
  234.         while((line=br.readLine())!=null)           //**Reading File line by line and adding each line in ARRAYLIST** 
  235.                 { 
  236.                    arr.add(line); 
  237.                 } 
  238.         br.close(); 
  239.          
  240.         Collections.sort(arr,new sortIgnoreCase()); //**Sorting ARrayList elements and created a new class sortIgnorecase which implements comapartor to ignore case** 
  241.         System.out.println("Which element do you want to remove from the list enter index"); 
  242.         int n = sc.nextInt();                      //**To remove any word from the given file** 
  243.         arr.remove(n); 
  244.          
  245.         FileWriter fw = new FileWriter(name);      //**Now Writing the Sorted arrayList in the given file** 
  246.         BufferedWriter bw = new BufferedWriter(fw); 
  247.         for(Object y:arr) 
  248.         { 
  249.             String s=(String)y; 
  250.             bw.write(s); 
  251.             bw.newLine();   //To seperate each word by a new line :for example     
  252.                             // Apple 
  253.                             // Ball 
  254.                             // Cat 
  255.         } 
  256.          
  257.          
  258.          
  259.     } 
  260.  
  261.     private static class sortIgnoreCase implements Comparator<Object> { 
  262.  
  263.         public sortIgnoreCase() { 
  264.         } 
  265.  
  266.         @Override 
  267.         public int compare(Object o1, Object o2) { 
  268. String s1 =(String)o1; 
  269. String s2= (String)o2; 
  270. return s1.compareToIgnoreCase(s2); 
  271.         } 
  272.  
  273.     } 
  274.      
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. package sorting.and.deleting.in.file;
  282.  
  283. import java.util.*;
  284. import java.io.*;
  285. public class SortingAndDeletingInFile {
  286.  
  287. public static void main(String[] args)throws IOException {
  288. Scanner sc=new Scanner(System.in);
  289. File fr=new File("File.txt");
  290. File fw =new File("Temp.txt");
  291. ArrayList arr=new ArrayList();
  292. BufferedWriter bw=new BufferedWriter(new FileWriter(fw));
  293. BufferedReader br=new BufferedReader(new FileReader(fr));
  294. String str2=new String();
  295. String str3=new String();
  296. while((str2=br.readLine())!=null){
  297. arr.add(str2);
  298. }
  299. arr.add("AAAM");
  300. arr.remove("Aani");
  301. Collections.sort(arr);
  302. for(Object y:arr){
  303. String s=(String)y;
  304. bw.write(s);
  305. bw.newLine();
  306. }
  307. br.close();
  308. bw.close();
  309. fr.delete();
  310. fw.renameTo(fr);
  311.  
  312. }
  313. }
Add Comment
Please, Sign In to add comment