Sunday 4 August 2013

Write a program to print the repeated values from an Integer Array in an Ascending order

Write a program to print the repeated values from an Integer Array in an Ascending order

WAP to print the repeated values in an Ascending order From the Integer Array  

The below program is to print the repeated values from an Integer array in Ascending order is asked in many interviews,print the repeated values from an integer array in an ascending order please find the below program 

package com.st.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.*;

public class SortingTest {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        int a[] = { 2, 2, 2, 1, 4, 4, 3, 3, 3, 1, 9, 9, 1, 5, 7, 8 };
        TreeSet<Integer> s = new TreeSet<Integer>();
        int c = 0, i = 0, j = 0;
        for (i = 0; i < a.length; i++) {
            c = 0;
            for (j = 0; j < a.length; j++) {
                if (a[i] == a[j]) {
                    c++;
                }
            }
            if (c > 1) {
                s.add(a[i]);
            }
        }
        Iterator it = s.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }

    }
}

Monday 22 July 2013

Write a program to find number of words in a file

 Write a program to find number of words in a file

Write a program to find number of words in a file

The below program is to find number of words in a file,number of words in a file,find the number of words in a file
Its the program to find a number of words in a file
//Write a program to find number of words in a file (file should be from commandline)
package com.naga.programs;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class CountWordsInFile {

    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
       
            FileInputStream fis = new FileInputStream("D:/Practice/file.txt");
            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strline=null;
            int count=0;
            while((strline = br.readLine()) != null){
                String[] m = strline.split(" ");
                for(int i=0;i<m.length;i++){
                    if(m[i] != null){
                        count++;
                    }
                }
            }
            System.out.println("The number of words in a file: "+count);
    }

}