Friday, March 23, 2018

Write a Java Program to take an String array like this String[] s={"Welcome","to","2018","hi","17"} and filter only numbers.


Write a Java Program to take an String array like this String[] s={"Welcome","to","2018","hi","17"} and filter only numbers.



class FilterNumber
{
public static void main(String[] args)
{
String[] s={"Welcome","to","2018","hi","17"};
for(String n:s)
{
try
{
System.out.println(Integer.parseInt(n));
}
catch (NumberFormatException e)
{}
}
}
}



Write a Java Program to take an String array like this String[] s={"Welcome","to","2018","hi","17"} and filter only numbers.
Write a Java Program to take an String array like this String[] s={"Welcome","to","2018","hi","17"} and filter only numbers. [All Java Interview programs]


Write a Java program to take an string array and find the duplicate and sort in ascending order.


Write a Java program to take an string array and find the duplicate and sort in ascending order.



import java.util.*;
class RemoveDuplicateStringArray
{
public static void main(String[] args)
{
String[] s={"z","a","c","a","d","c","b"};
TreeSet<String> ts=new TreeSet<>(Arrays.asList(s));
System.out.println(ts);
}
}





Write a Java program to take an string array and find the duplicate and sort in ascending order.
Write a Java program to take an string array and find the duplicate and sort in ascending order. [All Java Interview Programs]


Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.


Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.

Given String : google
Input String : gle
Output : gle is the substring of google




import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
String s="google";
Scanner sc=new Scanner(System.in);
System.out.println("Enter an String : ");
String a=sc.next();
boolean flag=false;int len=0;
for(int i=1;i<=s.length()-a.length();i++)
{
for(int j=0;j<a.length();j++)
{
if(a.charAt(j)!=s.charAt(i+j) )
{
len=0;
break;
}
else
len++;
}
if(len==a.length())
{
flag=true;
break;
}
}
if(flag==true)
System.out.println(a+" is the substring of "+s);
else
System.out.println(a+" is not substring of "+s);
}
}





 Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.
Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not. [All Java Interview Programs]






Tuesday, March 20, 2018

Write a program in java to find the longest palindrome from the given String. If the length is same then print in descending order.


Write a program in java to find the longest palindrome from the given String.
If the length is same then print in descending order.

Input : madamLeveLrotornitin
Total Palindromes = madam  ada  LeveL  eve  rotor  oto  nitin  iti
Longest Palindrome : rotor



class Palindrome4
{

public static void main(String[] args) {

// String s="madamLeveLnitin";
// String s="madamnitin";
String s="madamLeveLrotornitin";
String s2="";String s3="";
System.out.print("Total Palindromes =  ");
for(int i=0;i<s.length();i++)
{
for(int j=i+2;j<=s.length();j++)
{
s2=s.substring(i,j);
StringBuffer sb=new StringBuffer(s2).reverse();
if(s2.equals(sb.toString()))
{
System.out.print(s2+"  ");
if(s2.length()>s3.length())
s3=s2;
else if(s2.length()==s3.length())
{
if(s3.compareTo(s2)<0)
s3=s2;
}
}
}
}
System.out.println("\n\nLongest Palindrome = "+s3);
}
}



Write a program in java to find the longest palindrome from the given String.  If the length is same then print in descending order.
Write a program in java to find the longest palindrome from the given String. [All Java Interview Programs]



Write a program in java to print total palindrome strings presented in given string. Input : abcacbbbca Output : [bb, acbbbca, bbb, cac, cbbbc, bcacb]


Write a program in java to print total palindrome strings presented in given string.

Input : abcacbbbca
Output : [bb, acbbbca, bbb, cac, cbbbc, bcacb]


import java.util.*;
class Palindrome3
{
public static void main(String[] args)
{
String s="abcacbbbca";
String s2="";
HashSet<String> hs=new HashSet<>();
for(int i=0;i<s.length();i++)
{
for(int j=i+2;j<=s.length();j++)
{
s2=s.substring(i,j);
StringBuffer sb=new StringBuffer(s2).reverse();
if(s2.equals(sb.toString()))
hs.add(s2);
}
}
System.out.print("Total Palindromes =  "+hs);
}
}



Write a program in java to print total palindrome strings presented in given string.    Input : abcacbbbca  Output : [bb, acbbbca, bbb, cac, cbbbc, bcacb]
Write a program in java to print total palindrome strings presented in given string. [All Java Interview Programs]

Write a program in java to check whether an string is palindrome or not without using reverse method


Write a program in java to check whether an string is palindrome or not
without using reverse method.



public class Palindrome2
{
public static void main(String[] args)
{
String s="malayalam";
String s2="";
for(int i=s.length()-1;i>=0;i--)
s2=s2+s.charAt(i);
if(s.equals(s2))
System.out.println(s+" is palindrome");
else
System.out.println(s+" is not palindrome");
}
}





Write a program in java to check whether an string is palindrome or not without using reverse method
Write a program in java to check whether an string is palindrome or not without using reverse method [All Java Interview Programs]


Write a program in java to check whether an string is palindrome or not

/*
Write a program in java to check whether an string is palindrome or not
*/



public class Palindrome
{
public static void main(String[] args)
{
String s="madam";
String s2=new StringBuffer(s).reverse().toString();
if(s.equals(s2))
System.out.println(s+" is palindrome");
else
System.out.println(s+" is not palindrome");
}
}




Write a program in java to check whether an string is palindrome or not
Write a program in java to check whether an string is palindrome or not [All Java Interview Programs]

Sunday, March 18, 2018

Write a java program to print this numeric pattern 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13


Write a java program to print this numeric pattern

1  2  3  4
8  7  6  5
9 10 11 12
16 15 14 13



class NumericPattern2
{
public static void main(String[] alt){
int n=4;
for(int i=0;i<n;i++){
for(int j=1;j<=n;j++){
if(i%2==0)
System.out.print(i*n+j+"  ");
else
System.out.print((i+1)*n-j+1+"  ");
}
System.out.println();
}
}
}




Write a java program to print this numeric pattern     1  2  3  4   8  7  6  5   9 10 11 12   16 15 14 13
Write a java program to print this numeric pattern [All Java Interview Programs]

Write a java program to print this numeric pattern 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13


Write a java program to print this numeric pattern

1  2  3  4
8  7  6  5
9 10 11 12
16 15 14 13



public class NumericPattern
{
public static void main(String[] args)throws Exception {
int c=0;
for(int i=1;i<=4;i++)
{
if(i%2==1)
{
for(int j=1;j<=4;j++)
System.out.print("   "+ ++c);
c=c+4;
}
else
{
int t=c;
for(int k=1;k<=4;k++)
{
System.out.print("   "+t--);
}
}
System.out.println();
}
}
}




Write a java program to print this numeric pattern     1  2  3  4   8  7  6  5   9 10 11 12   16 15 14 13
Write a java program to print this numeric pattern. [All Java Interview Programs]

Java Program to view System Properties

// Java Program to view System Properties


class SystemProperties
{
public static void main(String[] alt)
{
System.out.println("\nOS Version = "+System.getProperty("os.version"));
System.out.println("\nOS Name = "+System.getProperty("os.name"));
System.out.println("\nOS Architecture = "+System.getProperty("os.arch"));
System.out.println("\nJava Compiler = "+System.getProperty("java.compiler"));
System.out.println("\nExtension Directory = "+System.getProperty("java.ext.dirs"));
System.out.println("\nPath Environment Variable = "+System.getProperty("java.library.path"));
System.out.println("\nFile Separator = "+System.getProperty("file.separator"));
System.out.println("\nPath Separator = "+System.getProperty("path.separator"));
System.out.println("\nCurrent working directory = "+System.getProperty("user.dir"));
System.out.println("\nCurrent working directory = "+System.getProperty("user.dir"));
System.out.println("\nAccount Name = "+System.getProperty("user.name"));
System.out.println("\nJVM implementation version = "+System.getProperty("java.vm.version"));
System.out.println("\nJVM implementation Name = "+System.getProperty("java.vm.name"));
System.out.println("\nJava installation directory = "+System.getProperty("java.home"));
System.out.println("\nJVM version = "+System.getProperty("java.runtime.version"));
System.out.println("\nJVM version = "+System.getProperty("deployment.javaws.jre.0.osname"));
System.out.println("\nJVM version = "+System.getProperty("deployment.javaws.jre.0.path"));
}
}

How to make our classes as immutable class by using Java programming?

/*
How to make our classes as immutable clas by using Java programming?
*/



final class Employee
{
final int id;
final String name;
public Employee(int id,String name)
{
this.id=id;
this.name=name;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
}

class ImmutableClass
{
public static void main(String[] alt)
{
Employee e1=new Employee(222,"Samar");
System.out.println(e1.getId()+" = "+e1.getName());
Employee e2=new Employee(333,"Athar");
System.out.println(e2.getId()+" = "+e2.getName());
}
}

Write a java program in java to reverse each word of string like this input:-"Altaf Java Blog" output:-"Blog Java Altaf"

/* Write a java program in java to reverse each word of string like this

input:-"Altaf Java Blog"
output:-"Blog Java Altaf"

*/

class ReverseString3
{
public static void main(String[] alt)
{
String s="Altaf Java Blog";
String[] s2=s.split(" ");
String s3=new String();
for(int i=s2.length-1;i>=0;i--)
{
s3=s3.concat(s2[i]+" ");
}
System.out.println(s3);
}
}

WAP in java to reverse each word of string like input : "Sriman java group" output:- "namirS avaJ puorG"

/* WAP in java to reverse each word of string like
input : "Sriman java group"
output:- "namirS avaJ puorG"

Note: Here instead of String we are using StringBuffer because we cannot modify the string object beacause of String
immutability. If we use String concat() method then every time new object will be created which is memory causes memory
wastage.
*/

class ReverseString2
{
public static void main(String[] alt)
{
String s="Sriman Java Group";
String[] s2=s.split(" ");
StringBuffer sb=new StringBuffer("");
for(String a:s2)
{
for(int i=a.length()-1;i>=0;i--)
sb.append(a.charAt(i));
System.out.print(sb+" ");
sb=new StringBuffer("");
}

}
}

Write a Java Program to reverse an String without reverse() method



// Write a Java Program to reverse a string without reverse() method


import java.util.*;
class ReverseString
{
public static void main(String[] args)
{
String s="Vishnu";
StringBuffer sb=new StringBuffer("");
for(int i=s.length()-1;i>=0;i--)
sb.append(s.charAt(i));
System.out.println(sb);
}
}




Write a Java Program to reverse an String without reverse() method



Write a program in java to find the 1st non repeated character from the string.



/*
WAP to find the 1st non repeated character from the string.
In this string ("altaf alam") 1st Non repeat character is 't'

*/


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NonRepeatChar {

public static void main(String[] args) {

String s="altaf alam";
for(int i=0;i<s.length();i++)
{
Pattern p=Pattern.compile(String.valueOf(s.charAt(i)));
Matcher m=p.matcher(s);
m.find();
if(!(m.find()))
{
System.out.println("First Non repeatable Character = "+s.charAt(i));
break;
}
}
}
}





Java program in java to find the 1st non repeated character from the string.




Write a Java progarm to input this and print this Input =a2c3z5 Output = aaccczzzzz

/*

Write a Java progarm to input this and print this
Input =a2c3z5
Output = aaccczzzzz

*/

import java.util.*;
class Test
{
public static void main(String[] args)
{
String s="a2c3z5";
String[] s2=s.split("(?<=\\G..)");
for(String s3:s2)
{
char c=s3.charAt(0);
int n=s3.charAt(1)-48;
for(int i=0;i<n;i++)
{
System.out.print(c);
}
}
}
}

WAP to print char occurrence in given string I/P : s=anand s2=prabhakar O/P : 3a 0n a n 0d

/*
WAP to print char occurrence in given string
I/P : s=anand
  s2=prabhakar

O/P : 3a 0n a n 0d
*/

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

public static void main(String[] args) {
String s="anand";
String s2="prabhakar";
String s3="";
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
Pattern p=Pattern.compile(ch);
Matcher m=p.matcher(s2);
int count=0;;
while(m.find())
{
count++;
}
if(s3.contains(ch))
System.out.print(ch+"  ");
else
System.out.print(count+ch+"  ");
s3=s3+ch;
}

}

}

Write a Java Program to print char occurrence in given string I/P : aabbccadc O/P : 3a 2b 3c 1d

/*
Write a Java Program to print char occurrence in given string
I/P : aabbccadc
O/P : 3a 2b 3c 1d
*/

import java.util.regex.*;
public class Test
{
public static void main(String[] args)
{
String s="aabbccadc";
String s4="";
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
if(!(s4.contains(ch)))
{
Pattern p=Pattern.compile(ch);
Matcher m=p.matcher(s);
int count=0;String s3=null;
while(m.find())
{
count++;
s3=m.group();
s4=s4.concat(s3);
}
System.out.print(count+s3+"  ");
}
}
}
}

Wednesday, March 14, 2018

WAP in Java to count occurrence of each character of given string.

/*
   Count occurrence of each character of given string
   Input:-"Sriman java group"
   Output:- S-1, r-2, i-1, m-1, a-3, n-1, J-1, v-1, G-1, o-1, u-1, p-1

 */


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharOccurance
{
public static void main(String[] args)
{
String s = "Sriman Java Group";
String s2=null;
String s3=new String();
Pattern p=null; Matcher m=null;
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
int count=0;
if(!(s3.contains(ch)) && !(ch.equals(" ")))
{
p=Pattern.compile(ch);
m=p.matcher(s);
while(m.find())
{
count++;
s2=m.group();
s3=s3.concat(s2);
}
System.out.println(s2+" - "+count);
}
}
}
}