0% found this document useful (0 votes)
16 views

Java Interview Programs

Uploaded by

Nitin Gorde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java Interview Programs

Uploaded by

Nitin Gorde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

//Program to count number of words in a string.

public class CountWords {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter a string");
s=sc.nextLine();
String name[]=s.split("\\s");//to split string at spaces
System.out.println("No. of words :"+name.length);
}

//Program to print initials of a name

public class PrintInitials {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter full name");
s=sc.nextLine();
String name[]=s.split("\\s");
for(String r:name)
{
System.out.print(r.charAt(0)+".");
}
}

//Program to count number of alphabets, numbers and special symbols in a string

public class AlphabetNumberCount {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s;
char c;
int ac=0,nc=0,sct=0;
System.out.println("Enter a string");
s=sc.next();
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(Character.isAlphabetic(c))
ac++;
else if(Character.isDigit(c))
nc++;
else
sct++;

}
System.out.println("Number of alphabets :"+ac);
System.out.println("Number of digits :"+nc);
System.out.println("Number of special characters :"+sct);
}
}

//Program to find second minimum element in array

public class SecondMin {


public static void main(String[] args) {
int ar[]=new int[5];
int i,min2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 ele");
for( i=0;i<5;i++)
ar[i]=sc.nextInt();

int min=ar[0];//Integer.MAX_VALUE;
min2=ar[0];//Integer.MAX_VALUE;
for( i=1;i<5;i++)
{

if(ar[i]<min)
{
min2=min;
min=ar[i];

}
else if((ar[i]< min2)|| (min==min2))
min2=ar[i];

}
System.out.println("min :"+min);
System.out.println("second min ele :"+min2);
}

//Program to count number of lines,words and characters in a file

public class CharCount {


public static void main(String[] args)
{
BufferedReader reader = null;

//Initializing charCount, wordCount and lineCount to 0

int charCount = 0;

int wordCount = 0;

int lineCount = 0;

try
{
//Creating BufferedReader object

reader = new BufferedReader(new FileReader("e:/test.txt"));

//Reading the first line into currentLine

String currentLine = reader.readLine();

while (currentLine != null)


{
//Updating the lineCount

lineCount++;

//Getting number of words in currentLine

String[] words = currentLine.split(" ");

//Updating the wordCount

wordCount = wordCount + words.length;

//Iterating each word

for (String word : words)


{
//Updating the charCount

charCount = charCount + word.length();


}

//Reading next line into currentLine

currentLine = reader.readLine();
}

//Printing charCount, wordCount and lineCount

System.out.println("Number Of Chars In A File : "+charCount);

System.out.println("Number Of Words In A File : "+wordCount);

System.out.println("Number Of Lines In A File : "+lineCount);


}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
reader.close(); //Closing the reader
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

//Program to search a word in a file

public class WordSearch {


public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);

String str,a;
boolean b=false;
//File file = new File("e:/test.txt");
FileReader file = new FileReader("e:/test.txt");
Scanner s=new Scanner(file);
System.out.println("enter string to be searched");
str=sc.next();
while(s.hasNext())
{
if(str.equals(s.next()))
{
System.out.println( str +" is present in file");
b=true;
break;
}
}
if(b==false)
System.out.println( str +" not present in file");

//Program to convert an array to ArrayList

public class ArrayToList {


public static void main(String[] args) {
Integer ar[]={10,29,30,20};//array should be of class type ,not of primitive type
ArrayList<Integer> al=new ArrayList<Integer>();
Collections.addAll(al,ar);
System.out.println(al);
al.add(27);
System.out.println(al);

String[] geeks = {"Rahul", "Utkarsh",


"Shubham", "Neelam"};

List<String> al2 = new ArrayList<String>();

// adding elements of array to arrayList.


Collections.addAll(al2, geeks);

System.out.println(al2);

}
}

//Program to convert ArrayList to an array

public class ListToArray {

public static void main(String[] args) {


ArrayList<Integer> al=new ArrayList<Integer>();
al.add(12);
al.add(23);
al.add(34);

//Method 1
Object ar[]=al.toArray();
for(Object r:ar)
System.out.println(r);

//Method 2
Integer arr[]=new Integer[5];
al.toArray(arr);
for(Integer r:arr)
System.out.println(r);

arr[3]=23;
arr[4]=90;
for(Integer r:arr)
System.out.println(r);

//Program to convert ArrayList to Set to remove duplicates

public class ListToSet {

public static void main(String[] args) {


ArrayList< Integer> al=new ArrayList<>();

al.add(12);
al.add(23);
al.add(22);
al.add(11);
al.add(12);
System.out.println("ArrayList :"+al);
LinkedHashSet<Integer> hs=new LinkedHashSet<Integer>(al);
System.out.println("Set :"+hs);

}
//Program for substring
public class subString {
public static void main(String[] args) {
String s1="welcome";
System.out.println(s1.substring(6));//e
System.out.println(s1.substring(7));//no character returned
System.out.println(s1.substring(7,7));//no character returned
System.out.println(s1.substring(4,4));//no character returned
System.out.println(s1.substring(7,4));//java.lang.StringIndexOutOfBoundsException:
String index out of range: -3
}

//counting occurence of every character in a string


public class CharCountClass {
public static void main(String[] args) {
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
TreeMap<Character,Integer> hm=new TreeMap<Character,Integer>();
char c[]=s.toCharArray();
//char ch;
for(char r:c)
{
if(hm.containsKey(r))
{
hm.put(r,hm.get(r)+1);
}
else
{
hm.put(r,1);
}

}
System.out.println(hm);

You might also like