Skip to main content

Top java programs for fresher



1) Write a program to find a repeated word?


package com.repeat;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

public class RepeatedWords {

public static void main(String[] args) 
{
//Creating wordCountMap which holds words as keys and their occurrences as values
        
        HashMap<String, Integer> wordCountMap = new HashMap<String, Integer>();
     
        BufferedReader reader = null;
         
        try
        {
            //Creating BufferedReader object
             
            reader = new BufferedReader(new FileReader("C:\\sample.txt"));
             
            //Reading the first line into currentLine
             
            String currentLine = reader.readLine();
             
            while (currentLine != null)
            {    
                //splitting the currentLine into words
                 
                String[] words = currentLine.toLowerCase().split(" ");
                 
                //Iterating each word
                 
                for (String word : words)
                {
                    //if word is already present in wordCountMap, updating its count
                     
                    if(wordCountMap.containsKey(word))
                    {    
                        wordCountMap.put(word, wordCountMap.get(word)+1);
                    }
                     
                    //otherwise inserting the word as key and 1 as its value
                    else
                    {
                        wordCountMap.put(word, 1);
                    }
                }
                 
                //Reading next line into currentLine
                 
                currentLine = reader.readLine();
            }
             
            //Getting the most repeated word and its occurrence
             
            String mostRepeatedWord = null;
             
            int count = 0;
             
            //Set<Entry<String, Integer>> entrySet = wordCountMap.entrySet();
             
            for (Entry<String, Integer> entry : entrySet)
            {
                if(entry.getValue() > count)
                {
                    mostRepeatedWord = entry.getKey();  //getting through the key value
                     
                    count = entry.getValue();
                }
            }
             
            System.out.println("The most repeated word in input file is : "+mostRepeatedWord);
             
            System.out.println("Number Of Occurrences : "+count);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();           //Closing the reader
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }    

}//Closing the class


2)Write a program to convert the ArrayToString?


package com.array;

import java.util.List;
import java.util.ArrayList;

public class ArraytoString
{
public static void main(String args[])
{
List al= new ArrayList<String>();
al.add("One");
al.add("Two");
al.add("Three");
al.add("Four");
al.add("Five");
String[] stringArrayObject = new String[al.size()];
al.toArray(stringArrayObject);
for(String temp : stringArrayObject)
System.out.println(temp);
}

}


3)Write a program to create a hotel bill?

  import java.util.Scanner;


public class HotelBill

{
   
    
public static void main(String args[])
{
int t=0,Choice,n1,i=1;
System.out.println(" MENU                   Rate   ");
System.out.println("1.idly                   10      ");
System.out.println("2.poori                  20     ");
System.out.println("3.Dosai                  30      ");
System.out.println("4.Chapthi                40     ");
System.out.println("5.vada                   50      ");
System.out.println("6.Pongal                 60     ");
System.out.println("7.Chicken                70       ");
System.out.println("8.Rice                   80       ");
System.out.println("9.PAN                    90       ");
System.out.println("10.Mutton                100       ");
    Scanner sc = new Scanner(System.in);
    //System.out.println(" Enter the Number of the order: ");
     //Choice=sc.nextInt();
//System.out.println(" Enter the number of plates:");
     //n1 = sc.nextInt();
    while(i==1)
    {
    System.out.println(" Enter the Number of the order: ");
     Choice=sc.nextInt();
System.out.println(" Enter the number of plates:");
     n1 = sc.nextInt();
     switch(Choice)
    {
    
    case 1 :
    t=t+(10*n1);
//     System.out.println("Total:" +t);
    break;
    
    case 2:
    t=t+(20*n1);
       // System.out.println("Total:" +t);
        break;
    
        case 3:
        t=t+(30*n1);
       // System.out.println("Total:" +t);
        break;

        case 4:
        t=t+(40*n1);
    //System.out.println("Total:" +t);
    break;
    case 5:
    t=t+(50*n1);
    //System.out.println("Total:" +t);
    break;
    
    case 6:
    t=t+(60*n1);
       // System.out.println("Total:" +t);
        break;
    
        case 7:
        t=t+(70*n1);
        //System.out.println("Total:" +t);
        break;

        case 8:
        t=t+(80*n1);
    //System.out.println("Total:" +t);
    break;
    case 9:
    t=t+(90*n1);
    //System.out.println("Total:" +t);
    break;
    
       
        case 10:
        t=t+(100*n1);
        //System.out.println("Total:" +t);
        break;

        default:
        //System.out.println();
            break;
}
    System.out.println("Do you want to continue");
    i=sc.nextInt();
    }  
    System.out.println("the total bill:"+t);
}
}

Comments

Popular posts from this blog

Decode of the image file from hibernate and struts

Create a java file for the decoding the image Image.java package com.img.action; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.img.dao.ConnectionDAO; import com.img.pojo.Film_TablePojo; import com.img.pojo.imagePojo; import com.opensymphony.xwork2.ActionSupport; public class Show extends ActionSupport { List<Object> values = new ArrayList<Object>();  public List<Object> getValues() { return values; } public void setValues(List<Object> values) { this.values = values; } /*HttpServletRequest request;     public void setServletRequest(HttpS...

Example for Registration using hibernate integration with struts2 and spring

First create a Pojo class for the Registeration form:     1)User.java                     package com.gklife.pojo; import com.gklife.RegisterDao.RegisterDao; public class User { private int id; private String name; private String Email; private String mobilenumber; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } public String getMobilenumber() { return mobilenumber; } public void setMobilenumber(String mobilenumber) { this.mobilenumber = mobilenumber; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute(){ ...