Monday, November 3, 2014

Java easy pro



Write a program to get a line with max word count from the given file

package com.java2novice.algos;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
public class MaxWordCountInLine 
{
   private int currentMaxCount = 0;
   private List lines = new ArrayList();
 public void readMaxLineCount(String fileName)
 {
                        FileInputStream fis = null;
               DataInputStream dis = null;
               BufferedReader br = null;
   try 
    {
                           fis = new FileInputStream(fileName);
                           dis = new DataInputStream(fis);
                           br = new BufferedReader(new InputStreamReader(dis));
                           String line = null;
                           while((line = br.readLine()) != null)
                             {
                           int count = (line.split("\\s+")).length;
                           if(count > currentMaxCount)
                              {
                           lines.clear();
                           lines.add(line);
                           currentMaxCount = count;
                           } 
        else if(count == currentMaxCount)
        {
                        lines.add(line);
              } 
                 }
          } 
         catch (FileNotFoundException e) 
         {
                           e.printStackTrace();
          } 
          catch (IOException e) 
          {
               e.printStackTrace();
           } 
           finally
          {
           try
           {
               if(br != null) br.close();
                           }
                catch(Exception ex){}
               }
   }
            public int getCurrentMaxCount() 
             {
               return currentMaxCount;
   }
            public void setCurrentMaxCount(int currentMaxCount) 
             {
               this.currentMaxCount = currentMaxCount;
   }
            public List getLines() 
             {
               return lines;
   }
            public void setLines(List lines) 
             {
               this.lines = lines;
   }
   public static void main(String a[])
  {
               MaxWordCountInLine mdc = new MaxWordCountInLine();
               mdc.readMaxLineCount("/Users/ngootooru/MyTestFile.txt");
               System.out.println("Max number of words in a line is: "+mdc.getCurrentMaxCount());
               System.out.println("Line with max word count:");
               List lines = mdc.getLines();
               for(String l:lines)
               {
                           System.out.println(l);
               }
   }
}

Write a program to convert string to number without using Integer.parseInt() method

package com.java2novice.algos;
           public class MyStringToNumber 
           {
           public static int convert_String_To_Number(String numStr)
           {
               char ch[] = numStr.toCharArray();
               int sum = 0;
               int zeroAscii = (int)'0';
               for(char c:ch)
               {
                           int tmpAscii = (int)c;
                           sum = (sum*10)+(tmpAscii-zeroAscii);
               }
               return sum;
   }
   public static void main(String a[])
   {
               
   System.out.println("\"3256\" == "+convert_String_To_Number("3256"));
   System.out.println("\"76289\" == "+convert_String_To_Number("76289"));
   System.out.println("\"90087\" == "+convert_String_To_Number("90087"));
   }
}

Write a program to find two lines with max characters in descending order

package com.longest.lines;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
           public class Main 
          {
           public static void main(String[] args)
           {
               BufferedReader br = null;
               String filePath = args[0];
               int topList = 0;
               Set liSet = new TreeSet(new MyComp());
               try 
                {
                 br = new BufferedReader(new FileReader(new File(filePath)));
                String line = br.readLine();
                topList = Integer.parseInt(line.trim());
                while((line = br.readLine()) != null)
                   {
                           line = line.trim();
                if(!"".equals(line))
                   {
                      liSet.add(new Entries(line.length(), line));
                  }
                }
               int count = 0;
               for(Entries ent:liSet)
                  {
                           System.out.println(ent.line);
                           if(++count == topList)
                              {
                              break;
                            }
                           }
               } 
              catch (FileNotFoundException e) 
               {
                                     e.printStackTrace();
               } 
              catch (IOException e) 
               {
                  e.printStackTrace();
               }
   }
            public static class Entries
             {
               Integer length;
               String line;
   public Entries(Integer l,String line)
   {
               length = l;
               this.line = line;
               }
   }
   public static class MyComp implements Comparator
    {
             public int compare(Entries e1, Entries e2) 
             {
                           if(e2.length > e1.length)
                            {
                                       return 1;
                           } 
                             else 
                             {
                                       return -1;
               }
         }
   }
}

No comments:

Post a Comment