Showing posts with label JavaPrograms. Show all posts
Showing posts with label JavaPrograms. Show all posts

Thursday, 17 September 2015

How to use Comparator in java?


import java.util.*;

class ComparatorTest {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add(new Employee("B", 3));
        list.add(new Employee("A", 1));
        list.add(new Employee("C", 2));

        Collections.sort(list, new EmployeeComparator());
        for (Employee emp : list) {
            System.out.println(emp.name + "\t" + emp.age);
        }
    }
}

class EmployeeComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee o1, Employee o2) {
        if (o1.age > o2.age) {
            return -1;
        } else {
            return 1;
        }
    }
}

class Employee {
    String name;
    int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
}



Output:

B 3
C 2
A 1

Monday, 3 August 2015

Getting Started with Java GUI Programming



Download the Netbeans IDE from filehippo.com/download_netbeans

Also download Java JDK(32-bit or 64-bit) according to your operating system. For 32-bit operating system download the 32-bit version of Java JDK and for 64-bit operating system download the 64-bit version of Java JDK

After downloading firstly install Java JDK and then Netbeans

Monday, 22 June 2015

Program to calculate how many times a word occurs in a given string in just one line




//Program to calculate how many times a word occurs in a given string in just one line

import java.io.*;  //for IOException, BufferedReader, InputStreamReader
class CountWord
{
  public static void main(String args[]) throws IOException
  {
     System.out.println("Enter the string: ");
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     String s=br.readLine();  //Gets a string from user
     System.out.println("Enter the word to search for: ");
     String word=br.readLine();  //Gets a string from user

     int count=(s.length()-s.replace(word+"", "").length())/word.length();     

   System.out.println(word+" occurs in given string for: "+count+" times");
  }
}

Wednesday, 17 June 2015

Program to reverse each word of a given string




//Program to reverse each word of a given string

import java.io.*;  //for IOException, BufferedReader, InputStreamReader
import java.util.*;  //for StringBuffer
public class WordReverse {
    public static void main(String[] args)throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the string:");
    String str=br.readLine();
    StringTokenizer st=new StringTokenizer(str);
    String temp="";
    while(st.hasMoreTokens())
    {
        temp=temp+new StringBuffer(st.nextToken()).reverse().toString()+" ";
    }
        System.out.println("Reversed String is:");
        System.out.println(temp);
    }   
}




Output:



Enter the string:

Hello this is GsbProgramming

Reversed String is:

olleH siht si gnimmargorPbsG