Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package employee;
- import java.util.*;
- import java.io.*;
- public class Employee <U,V> implements Comparable <Employee> {
- private U a;
- private V b;
- public Employee(U a, V b) {
- this.a = a;
- this.b = b;
- }
- public U getA() {
- return a;
- }
- public void setA(U a) {
- this.a = a;
- }
- public V getB() {
- return b;
- }
- public void setB(V b) {
- this.b = b;
- }
- public static void main(String[] args) {
- ArrayList <Employee> arr = new ArrayList<Employee>();
- arr.add (new Employee ("Ali",1));
- arr.add (new Employee ("Ahmad",2));
- arr.add (new Employee ("Xhmad",5));
- arr.add(new Employee (2,"Saim"));
- arr.add(new Employee (1,"Xaim"));
- arr.add(new Employee (9,"Gaim"));
- Collections.sort(arr);
- for(Employee x:arr)
- System.out.println(x.getA()+" "+x.getB());
- }
- @Override
- public int compareTo(Employee o) {
- String one="",two="";
- int a=0,b=0;
- if (o.getA() instanceof String && this.getA() instanceof String){
- one = (String) o.getA();
- two = (String) this.getA();
- return two.compareTo(one);
- }
- /* else
- if (o.getB() instanceof String && this.getB() instanceof String){
- one = (String) o.getB();
- two = (String) this.getB();
- return one.compareTo(two);
- }*/
- /* else
- if (o.getA() instanceof String && this.getB() instanceof String){
- one = (String) o.getA();
- two = (String) this.getB();
- return one.compareTo(two);
- }*/
- if(o.getA() instanceof Integer && this.getA() instanceof Integer)
- {
- a=(int) o.getA();
- //System.out.println(this.getA());
- b=Integer.parseInt((String.valueOf(this.getA())));
- return b-a;
- }
- return 0;
- }
- }
- Upper to Lower, Lower to Upper
- package st2;
- public class St2 {
- public static void main(String[] args) {
- String s="HelLo woRld @ 2017";
- int i;
- char[]c=s.toCharArray();
- char[] k=new char[100];
- int a=c.length;
- for(i=0;i<a;i++)
- {
- if(Character.isUpperCase(c[i]))
- {
- c[i]=Character.toLowerCase(c[i]);
- }
- else if(Character.isLowerCase(c[i]))
- {
- c[i]= Character.toUpperCase(c[i]);
- }
- }
- for(i=0;i<a;i++)
- {
- System.out.print(c[i]);
- }
- }
- }
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.StringTokenizer;
- /**
- * Created by test on 3/14/2017.
- */
- public class Main{
- public static void main(String[] args) throws Exception {
- File country = new File("out\\country.txt");
- File capital = new File("out\\capital.txt");
- File combine = new File("out\\combine.txt");
- FileReader f1 = new FileReader(country);
- BufferedReader bf1 = new BufferedReader(f1);
- String str = new String();
- ArrayList<String> list = new ArrayList<String>();
- int i = 0;
- while((str = bf1.readLine()) != null) {
- list.add(str);
- i++;
- }
- Collections.sort(list);
- bf1.close();
- f1.close();
- FileWriter wcountry= new FileWriter(country);
- BufferedWriter bwcountry = new BufferedWriter(wcountry);
- for (String ss: list) {
- bwcountry.write(ss + "\n");
- }
- bwcountry.close();
- wcountry.close();
- FileWriter w1 = new FileWriter(capital);
- BufferedWriter bw1 = new BufferedWriter(w1);
- String delimiter = " ";
- String comb = new String();
- for (int k = 0; k < i; k++) {
- int j =0;
- FileReader f2 = new FileReader(combine);
- BufferedReader bf2 = new BufferedReader(f2);
- while((comb = bf2.readLine()) != null) {
- StringTokenizer st = new StringTokenizer(comb, delimiter);
- String name = st.nextToken();
- String name2 = null;
- if(name.equals(list.get(k))) {
- name2 = st.nextToken();
- bw1.write(name2 + "\n");
- }
- j++;
- }
- bf2.close();
- f2.close();
- }
- bw1.close();
- w1.close();
- }
- package sorting.and.deleting.in.file;
- import java.util.*;
- import java.io.*;
- public class SortingAndDeletingInFile {
- public static void main(String[] args)throws IOException {
- Scanner sc=new Scanner(System.in);
- File fr=new File("File.txt");
- File fw=new File("Temp.txt");
- BufferedWriter bw=new BufferedWriter(new FileWriter(fw));
- BufferedReader br=new BufferedReader(new FileReader(fr));
- String str1=new String();
- String LinetoDelete="";
- String s=new String();
- System.out.println("Enter a word to delete:\n");
- s=sc.nextLine();
- while((str1=br.readLine())!=null){
- if (str1.contains(s))
- str1 = str1.replace(s, "");
- bw.write(str1);
- bw.newLine();
- }
- br.close();
- bw.close();
- fr.delete();
- fw.renameTo(fr);
- }
- }
- for deleteing whole line:
- LinetoDelete=str1.trim();
- if(LinetoDelete.contains(s))continue;
- bw.write(str1);
- package x;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
- public class X {
- public static void main(String[] args) throws Exception {
- ArrayList arr= new ArrayList();
- Scanner sc = new Scanner(System.in);
- String name ="words.txt"; //**FILE NAME**
- String line=null;
- FileReader fr = new FileReader(name);
- BufferedReader br = new BufferedReader(fr);
- while((line=br.readLine())!=null) //**Reading File line by line and adding each line in ARRAYLIST**
- {
- arr.add(line);
- }
- br.close();
- Collections.sort(arr,new sortIgnoreCase()); //**Sorting ARrayList elements and created a new class sortIgnorecase which implements comapartor to ignore case**
- System.out.println("Which element do you want to remove from the list enter index");
- int n = sc.nextInt(); //**To remove any word from the given file**
- arr.remove(n);
- FileWriter fw = new FileWriter(name); //**Now Writing the Sorted arrayList in the given file**
- BufferedWriter bw = new BufferedWriter(fw);
- for(Object y:arr)
- {
- String s=(String)y;
- bw.write(s);
- bw.newLine(); //To seperate each word by a new line :for example
- // Apple
- // Ball
- // Cat
- }
- }
- private static class sortIgnoreCase implements Comparator<Object> {
- public sortIgnoreCase() {
- }
- @Override
- public int compare(Object o1, Object o2) {
- String s1 =(String)o1;
- String s2= (String)o2;
- return s1.compareToIgnoreCase(s2);
- }
- }
- }
- package sorting.and.deleting.in.file;
- import java.util.*;
- import java.io.*;
- public class SortingAndDeletingInFile {
- public static void main(String[] args)throws IOException {
- Scanner sc=new Scanner(System.in);
- File fr=new File("File.txt");
- File fw =new File("Temp.txt");
- ArrayList arr=new ArrayList();
- BufferedWriter bw=new BufferedWriter(new FileWriter(fw));
- BufferedReader br=new BufferedReader(new FileReader(fr));
- String str2=new String();
- String str3=new String();
- while((str2=br.readLine())!=null){
- arr.add(str2);
- }
- arr.add("AAAM");
- arr.remove("Aani");
- Collections.sort(arr);
- for(Object y:arr){
- String s=(String)y;
- bw.write(s);
- bw.newLine();
- }
- br.close();
- bw.close();
- fr.delete();
- fw.renameTo(fr);
- }
- }
Add Comment
Please, Sign In to add comment