Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday, 2 December 2015

How to convert Byte array to String in Java

In our application sometime there is need of byte array. We can use byte array for encryption purpose . We can convert a string to byte array using string getBytes() method. In byte array string become in decryted format . We can make string from byte array using this syntax.

String str = new String(ByteArray);


package com.javaproficiency;

public class StringToBinary {
public static void main(String[] args) {
String IPCode = "java string is good";
//convert string to byte  array
byte [] bytes = IPCode.getBytes();
System.out.println(" string ="+ IPCode);
System.out.println(" byte array ="+ bytes);
//Convert byte array to string 
String str = new String( bytes );
System.out.println(" str ="+  str);
}

}

Output:

 string =java string is good

 byte array =[B@6c267f18

 str =java string is good

Monday, 29 June 2015

How to find all occurrences of string in a string Java

 Find all occurrences of string in a string. for this we will use one loop and indexof() method

public class StringCheck {
   
    public static void main(String[] args) {
       
        String str = "delhi is good city in india and india also good. there are many city good and people also good" +
                "so india is good country ";
        String mystr = "good";
        int index =0;
   while(index != -1){
       index = str.indexOf(mystr, index+1);
        System.out.println("index is = "+index);
   };
       
    }
}

Output:

index is = 9

index is = 43

index is = 69

index is = 90

index is = 106

index is = -1

Tuesday, 31 March 2015

Convert StringBuilder To String

 In this example firstly create a string builder and then convert it to string using toString() method.


package com.javapro.string;

public class StringBuilderDemo {
   
  public static void main(String[] args) {
      StringBuilder data=new StringBuilder("Hi");
      data.append(" How Are");
      data.append(" You");
      System.out.println("data="+data);
    
      String str=data.toString();
    
      System.out.println("str="+str);
  }
 
 
}

Sunday, 29 March 2015

Convert String To Integer

 Convert string to a number in java

public class ConvertStringToInteger {
  
           public static void main(String[] args) {
                      String number="123";
                      int num = Integer.parseInt(number);
                     System.out.println("Number="+num);

                     }
}

Thursday, 19 March 2015

Expand String


 Write A Function

String expnadString(String x);

string x="1,2,3,6";

output : 1,2,3,4,5,6

x=1,....,4....,10

output:

1,2,3,4,5,6,7,8,9,10


public class ExpandMyString {

    public static void main(String[] args) {
        TestData obj = new TestData();
        String x = "1,2,3,15,......,20,....,30";
        System.out.println("out put=" + obj.expnadString(x));
    }

    public String expnadString(String x) {
        String[] namesOfArray = x.split(",");
        char[] arr = namesOfArray[namesOfArray.length - 1].toCharArray();
        int i = 0;
        int num = 0;
        int zeroAscii = (int) '0';
        while (i < arr.length) {
            int charAscii = (int) arr[i];
            num = num * 10 + (charAscii - zeroAscii);
            i++;
        }

        String y = "";
        for (int j = 1; j < num; j++) {
            y += j + ",";
        }
        y += num;

        return y;
    }

}






 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).

Saturday, 31 January 2015

How to Print Double Quotes in JAVA


how to print double quotes in java

public class Display {
public static void main(String[] args) {
       String str="GANGA:"+"\"RIVER\"";
       System.out.print(str);

   }
}


Output:

GANGA:"RIVER"

Saturday, 24 January 2015

Check a string value is Integer or not in java


Check a given string value is Integer or not in java


private boolean isInteger(String str){

for (int i = 0; i < str.length(); i++) {
if (str.charAt(i)-'0'<0||str.charAt(i)-'0'>9) {
return false;
}
}
return true;
}



 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).

 

 

Check ia string is hexadecimal number or not


Check if a String is Hexadecimal number or not in java


  public boolean isHexNumber(String){
       boolean flag;
       try {
           int t = Integer.parseInt(value, 16);
            flag = true;
       } catch (NumberFormatException e) {
            flag = false;
       }
       return flag;
}
   



 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).

Friday, 23 January 2015

Read File And Count Occurrence Of Word In Java

Read a file and count the occurrence of word in java

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.TreeMap;
import java.util.StringTokenizer;

public class fileRead {

    public static void main(String [] args){

        try{
           
            String textFile ="D:\\workspace\\datastructure\\src\\javaprogram\\test.txt";
            BufferedReader input = new BufferedReader(new FileReader(textFile));
           
            //Creating the Map to store the words and their occurrences
            TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
            String currentLine = null;
           
            //Reading line by line from the text file
            while((currentLine = input.readLine()) != null){
               
                //Parsing the words from each line
                StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'\"");
                while(parser.hasMoreTokens()){
                    String currentWord = parser.nextToken();
                   
                    Integer frequency = frequencyMap.get(currentWord);
                    if(frequency == null){
                        frequency = 0;                       
                    }
                    //Putting each word and its occurrence into Map
                    frequencyMap.put(currentWord, frequency + 1);
                }
               
            }
           
            //Displaying the Result
            System.out.println(frequencyMap);
                  
        }catch(IOException ie){
            ie.printStackTrace();
            System.err.println("Your entered path is wrong");
        }       
       
    }
   
}

Wednesday, 21 January 2015

Top String Programs In Java

Top String Program In Java


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).



Other Useful Posts:


  Top 50 core java coding interview question and answer

  Guava Tutorial








Generate Combination in java

public class Combination {
     void doCombination(char []str,int start,int end){   
     char []data=new char[3];
     int i=0;
     int r=3;// how many element combine in one selection
     doCombinationUntil(str,data,start,end,i,r);
     }
    
   void   doCombinationUntil(char []str,char []data,int start,int end,int index,int r){
        if (index==r) {
          for (int j = 0; j < r; j++) {
              System.out.print(data[j]);
          }
          System.out.println("");
          return;
     }
        for (int i=start; i<=end && end-i+1 >= r-index; i++)
         {
             data[index] = str[i];
             doCombinationUntil(str, data, i+1, end, index+1, r);
         }
     }
    
     public static void main(String[] args) {  
          String str1="abcd";
          char []str=str1.toCharArray();
          Combination obj=new Combination();
          obj.doCombination(str,0,(str.length-1));
     }
    

}



 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).



Generate permutation of string

Permutation:
each of several possible ways in which a set or number of things can be ordered or arranged. 

public class permutation {
  void    doPermutation(char [] str,int i,int n){
       if (i==n) {
          for (int m = 0; m < str.length; m++) {
       System.out.print(str[m]);
          }
          System.out.println("");
    
     }
       else{
            for (int j = i; j <=n; j++) {
                //swap str[i] and str[j]
                 char ch=str[i];
                 str[i]=str[j];
                 str[j]=ch;
                 doPermutation(str, i+1, n);
                 //swap str[i] and str[j]
                 ch=str[i];
                 str[i]=str[j];
                 str[j]=ch;
          }
       }
         
     }   
     public static void main(String[] args) {
    
          String str1="abc";
          permutation obj=new permutation();
          char []str=str1.toCharArray();
          obj.doPermutation(str,0,(str.length-1));       
     }
    
}



 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).


Saturday, 17 January 2015

Permutation of a number

Find all permutation of a number
public class permutation {

  void    doPermutation(char [] str,int i,int n){
      if (i==n) {
        for (int m = 0; m < str.length; m++) {
      System.out.print(str[m]);
        }
        System.out.println("");
   
    }
      else{
          for (int j = i; j <=n; j++) {
                     // swap str[i] and swap[j]
               char ch=str[i];
               str[i]=str[j];
               str[j]=ch;
               doPermutation(str, i+1, n);
               // swap str[i] and swap[j]
               ch=str[i];
               str[i]=str[j];
               str[j]=ch;
        }
      }
       
    }
   
    public static void main(String[] args) {
           String str1="abc";
        permutation obj=new permutation();
        char []str=str1.toCharArray();
        obj.doPermutation(str,0,(str.length-1));       
    }
   
}
==========================
Output:
abc
acb
bac
bca
cba
cab






 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).




Swap two strings without using any variable

 Swap two strings without using any variable 
public class SwapTwoStringWithoutUsingVaraible {
    public static void main(String[] args) {
    String a="delhi";
    String b="dehradun";
    a= a+b;
    b = a.substring(0,(a.length()-b.length()));
    a = a.substring(b.length(),(a.length()));
    System.out.println("a = "+a);
    System.out.println("b = "+b);
    }
}




 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).