Thursday, 5 March 2015

Programs

1.Word count in Java?
public class WordCount{

  public static void main(String[] args) {
    String s = "Count the No of words in the given String. String given is ";
    String array[] = s.split(" ");
    List<String> arrayList = Arrays.asList(array);
    Map<String, Integer> map = new HashMap<String, Integer>();
    for (String string : arrayList) {
      if (map.get(string) == null)
        map.put(string, 1);
      else
        map.put(string, map.get(string) + 1);
    }
    System.out.println("Word Count = " + map);
  }
}
O/P:
Word Count = {of=1, is=1, Count=1, given=2, words=1, String=1, String.=1, the=2, No=1, in=1}

2.How to find the duplicate number in the given integer array?
Conditions : the given string should be in sequence from 1 to n(only one number should be duplicated) .
 public class DuplicateNumber{

  public static void main(String[] args) {
    int[] i = { 1, 2, 3, 4, 5, 6, 6, 7, 8 };
    Arrays.sort(i);
    int sum = 0;
    for (int j = 0; j < i.length; j++) {
      sum = sum + i[j];
    }
    int max = i[i.length - 1];
    int total = max * (max + 1) / 2;
    int miss = total - sum;
    System.out.println(Math.abs(miss));
  }
}
O/P:
The duplicated number is : 6

3. Find the count of letters in the file?
public class CountofLettersString {
  public static void main(String[] args) {
    String str = "CHINNA";
    Map<Character, Integer> wordSet = new HashMap<Character, Integer>();
    Map<Character, Integer> letterCount = new HashMap<Character, Integer>();
    System.out.println("Through String");
    printletterCount(str, letterCount);
    System.out.println("Character" + "-->Count");
    for (Character ch : letterCount.keySet()) {
      System.out.println(ch + "\t--> " + letterCount.get(ch));
    }
    letterCount.clear();
    readthefile(wordSet, letterCount);
    System.out.println("Through File");
    System.out.println("Character" + "-->Count");
    for (Character ch : letterCount.keySet()) {
      System.out.println(ch + "\t--> " + letterCount.get(ch));
    }
  }

  public static void readthefile(Map<Character, Integer> wordSet, Map<Character, Integer> letterCount) {
    Scanner wordFile = null;
    String word;
    try {
      wordFile = new Scanner(new FileReader("words.txt"));
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    while (wordFile.hasNext()) {
      word = wordFile.next();
      printletterCount(word, letterCount);
    }
  }

  public static void printletterCount(String str, Map<Character, Integer> letterCount) {
    if (str != null) {
      for (Character c : str.toCharArray()) {
        if (c.isSpaceChar(c)) {
          continue;
        }
        Integer count1 = letterCount.get(c);
        int newCount = (count1 == null ? 1 : count1 + 1);
        letterCount.put(c, newCount);
      }
    }
  }
}
O/P:
Through String
Character-->Count

A    --> 1
C    --> 1
N    --> 2
H    --> 1
I    --> 1
Through File
Character-->Count

A    --> 1
C    --> 1
N    --> 2
H    --> 1
I    --> 1

4.convert string to number without using Parse method?
public class MyStringToNumber {
  public static int convert_String_To_Number(String numStr) {
    char ch[] = numStr.toCharArray();
    int sum = 0;
    int zeroAscii = '0';// Converts the char to int
    for (char c : ch) {
      int tmpAscii = (int) c;// converts the every character of an arry to int
      sum = (sum * 10) + (tmpAscii - zeroAscii);
    }
    return sum;
  }
  public static void main(String[] args) {
    System.out.println("\"3256\" == " + convert_String_To_Number(",jzxcb"));
  }
}


Print Friendly Version of this pagePrint Get a PDF version of this webpagePDF

No comments:

Simplest way to display Server Time in JSP using js

THE BELOW CODE WORKS ONLY IN JSP : <html> <body > <table width="95%" border="1" cellpadding="1...