Monday, 14 March 2016

String Program like convert string to integer

Q-

How to convert string to int in Java

ans-
1st:- Using Integer.parseInt()
2nd:-Using Integer.valueOf()
example:-
package com.string.to.integer;

public class StringtoInteger2 {

public static void main(String[] args) {
String str="-123";
int num1=100;
int num2=Integer.valueOf(str);
int sum=num1+num2;
System.out.println(""+sum);

}

}

Q2:-

Convert int to String conversion?

ans:-There are two ways to convert an integer value to a String.
1) Method 1: Using String.valueOf(int i):
2) Method 2: Using Integer.toString(int i)


1) Method 1: Using String.valueOf(int i): This method takes integer value as an argument and returns a string representing the int augment.
Method declaration:
public static String valueOf(int i)
parameters:
i – integer that needs to be converted to a string
returns:
A string representing the integer argument

2) Method 2: Using Integer.toString(int i): This method works same as String.valueOf(int i) method. It belongs to the Integer class and converts the specified integer value to String. for e.g. if passed value is 101 then the returned string value would be “101”.

example:-int ivar2 = 200;
String str2 = Integer.toString(ivar2);

example:-
package com.convertnt.to.convertstring;

public class ConvertInttoString {

public static void main(String[] args) {
/* Method 1: using valueOf() method
         * of String class.
         */
int var=111;
String str2=String.valueOf(var);
System.out.println(""+str2);
/* Method 2: using toString() method 
         * of Integer class
         */

int var2=222;
String str3=Integer.toString(var2);
System.out.println(""+str3);
}

}

How to convert String to Double in Java?

ans:-Method 1: Using Double.parseDouble
       Method 2: Using Double.valueOf
     Method 3: Double class constructor
============ Example of string to double=======

package com.convert.string.to.doublle;

public class StringToDouble {

public static void main(String[] args) {
//Using parseDouble
String str1="12.33";
Double var=Double.parseDouble(str1);
System.out.println(var);
//Using valueOf method
String str2="123.234";
Double var2=Double.valueOf(str2);
System.out.println(var2);
//using Double class constructor
String str4="1234.2345";
Double var3=new Double(str4);
System.out.println(var3);
}

}

Q-double to string conversion?

ans:- 

Method 1:Using String.valueOf() method 

Method 2: Using Double.toString() method.

Example:-
package com.convert.doublle.to.string;

public class DoubleToString {

public static void main(String[] args) {
/* Method 1: using valueOf() method
       * of String class
       */
double var=121.23;
String str=String.valueOf(var);
System.out.println("String1 is:"+str);
/* Method 2: using toString() method 
       * of Double class
       */
double var2=123.342;
String str2=Double.toString(var2);
System.out.println("String2 is:"+str2);
}

}

Q-How to convert String to long in Java?
ans:-1) Long.parseLong:
      2) Long.valueOf:
Example:-
package com.convert.string.convertlong;

public class StringtoLong {

public static void main(String[] args) {
String str="203456";
      //Conversion using parseInt method
      long num = Long.parseLong(str);

      //Conversion using valueOf method
      long num2 = Long.valueOf(str);

      //Conversion: Long(String s) constructor
      long num3 = new Long(str);

      //Displaying variables values
      System.out.println(num);
      System.out.println(num2);
      System.out.println(num3);
}

}
Q-long to String conversion?
ans:-1) Method 1: Using String.valueOf(long l):
2) Method 2: Using Long.toString(long l)
Example:-
package com.convert.string.convertlong;

public class LongToString {

public static void main(String[] args) {
/* Method 1: using valueOf() method
         * of String class.
         */
        long lvar = 123;
        String str = String.valueOf(lvar);
        System.out.println("String is: "+str);
        
        /* Method 2: using toString() method 
         * of Long class
         */
        long lvar2 = 200;
        String str2 = Long.toString(lvar2);
        System.out.println("String2 is: "+str2);
}

}
Q-

Convert String to boolean primitive in Java: parseBoolean() method

ans:-using Boolean.parseBoolean(str);
Example:-

package com.convert.string.convertlong;

public class StringToBoolean {

public static void main(String[] args) {
// String Objects
     String str = "false";
     // Case does not matter for conversion
     String str2 = "TrUe";
  
     // String to boolean conversion
     boolean bvar = Boolean.parseBoolean(str);
     boolean bvar2 = Boolean.parseBoolean(str2);
   
     // Displaying boolean values
     System.out.println("boolean value of String str is: "+bvar);
     System.out.println("boolean value of String str2 is: "+bvar2);

}

}

Java – boolean to String conversion

ans:-1) Method 1: Using String.valueOf(boolean b)
2) Method 2: Using Boolean.toString(boolean b):
Example:-
package com.convert.booleans.convertstring;

public class BooleanToString {

public static void main(String[] args) {
/* Method 1: using valueOf() method
         * of String class.
         */
        boolean boovar = true;
        String str = String.valueOf(boovar);
        System.out.println("String is: "+str);
        
        /* Method 2: using toString() method 
         * of Boolean class
         */
        
        boolean boovar2 = false;
        String str2 = Boolean.toString(boovar2);
        System.out.println("String2 is: "+str2);
        
}

}


String to ArrayList conversion

ans:-1) First we are splitting the string using String split() method and storing the substrings into a String array.
2) Creating an ArrayList while passing the substring reference to it using Arrays.asList() method.

     import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StringToArrayList {

public static void main(String[] args) {

String str1="11,32,45,67,88";
String arr[]=str1.split(",");

List<String> al=new ArrayList<String>();
al=Arrays.asList(arr);
for (String s : al) {
System.out.println(s);
}
//System.out.println(al);

}

}

How to convert a char array to a string in Java?

ans:-There are two ways to convert a char array (char[]) to String in Java:
1) Creating String object by passing array name to the constructor
2) Using valueOf() method of String class.
Example:-

public class CharArrayToString {

public static void main(String[] args) {
// Method 1: Using String object
     char ch[] = {'g', 'o', 'o', 'd', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g'};
     
     // Method 1: Using String object
     String str1=new String(ch);
     System.out.println(str1);
     
     // Method 1: Using ValueOf Method
     String str2=String.valueOf(ch);
     System.out.println(str2);
}

}
Q-Reverse a String Without using String API?
ans-input my name is xyz
      output:-xyz is name my
example:-
public class ReverseStringWithoutUsingMethod {

public static void main(String[] args) {

System.out.println("Enter String");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String arr[] = input.split(" ");
for (int i = arr.length-1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}


}
Q-how to replace string?
input: how are you modi
output:-are how  modi you
Example:
public class StringREplace {

public static void main(String[] args) {

System.out.println("Enter string:- ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

String s[] = input.split(" ");
//for even 
if (s.length % 2 == 0) {
for (int i = 0; i <= s.length - 1; i++) {
if (i % 2 != 0)
System.out.print(s[i - 1] + " ");
else
System.out.print(s[i + 1] + " ");
}
//for odd
} else {
for (int i = 0; i <= s.length - 2; i++) {
if (i % 2 != 0)
System.out.print(s[i - 1] + " ");
else
System.out.print(s[i + 1] + " ");
}
if ((s.length - (s.length - 1)) <= 1) {
System.out.print(s[s.length - 1] + " ");
}
}
}

}

Q-How to reverse a String  same possition (alternate)  in java
answer should be come like this
input:- i love my india
output: i evol my aidni

Ezample:-import java.util.Scanner;

public class ReplacesomeString {

public static String rev(String x) {

StringBuilder sb = new StringBuilder(x);
String y = sb.reverse().toString();
return y;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String:- ");
String input = sc.nextLine();
String arr[] = input.split(" ");
for (int i = 0; i < arr.length; i++) {
if (i % 2 != 0) {
String x = arr[i];
String output = rev(x);
arr[i] = output;
}
}
System.out.println("After Operation Output Are:- ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}

}

Q- How to find Prime number ?
ans-

public class PrimeNumber {

public static void main(String[] args) {
int n = 3;
int temp = 1;
for (int i = 2; i < 200; i++) {
for (int j = 2; j < n; j++) {
if (n % j == 0) {
temp = 0;
break;
}
}
if (temp != 0)
System.out.println("Prime " + n);
n++;
temp=1;
}
}
}
Q- how to print pattern ?
I
I N
I N D
I N D I
I N D I A
ANS:-
import java.util.Scanner;


public class PatternINDIA {

public static void main(String[] args) {
System.out.println("Enter string:- ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
for (int i = 0; i < input.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(input.charAt(j)+" ");
}
System.out.println();
}

}

}

Q-HOW TO DELETE COMMON VALUE FROM ARRAY LIST.. we have have three arrayList?
note:-i want delete 4 and 5 from frst arrayList
arrayList 1[1, 2, 3, 4, 5, 6, 7, 8, 9]
arrayList 1[1, 2, 3, 4, 5]
arrayList 1[1, 2, 3]

output:-should be come
4, 5]
[1, 2, 3, 6, 7, 8, 9]

Example:-package com.remove.intoarraylist;

import java.util.ArrayList;

public class Array {

public static void main(String[] args) {
ArrayList<Integer> al1=new ArrayList<Integer>();
al1.add(1);
al1.add(2);
al1.add(3);
al1.add(4);
al1.add(5);
al1.add(6);
al1.add(7);
al1.add(8);
al1.add(9);
ArrayList<Integer> al2=new ArrayList<Integer>();
al2.add(1);
al2.add(2);
al2.add(3);
al2.add(4);
al2.add(5);
ArrayList<Integer> al3=new ArrayList<Integer>();
al3.add(1);
al3.add(2);
al3.add(3);
System.out.println("arrayList 1"+al1);
System.out.println("arrayList 1"+al2);
System.out.println("arrayList 1"+al3);
al2.removeAll(al3);
System.out.println(al2);
al1.removeAll(al2);
System.out.println(al1);
}

}


2 comments:

  1. Nice explanation.
    Thanks,
    https://www.flowerbrackets.com/arraylist-in-java/

    ReplyDelete