Professional Documents
Culture Documents
Scanner;
class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");
import java.util.Scanner;
public class JavaExample {
number = num;
while (number != 0)
{
temp = number % 10;
total = total + temp*temp*temp;
number /= 10;
}
if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
Output:
import java.util.Scanner;
public class JavaExample {
if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
Output:
class SumOfArray{
public static void main(String args[]){
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;
//Advanced for loop
for( int num : array) {
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
Output:
import java.util.Scanner;
class SumDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int[] array = new int[10];
int sum = 0;
System.out.println("Enter the elements:");
for (int i=0; i<10; i++)
{
array[i] = scanner.nextInt();
}
for( int num : array) {
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
Output:
import java.util.Scanner;
public class JavaExample {
Java String length(): The Java String length() method tells the length of the
string. It returns count of total number of characters present in the String.
For example:
1
public class Example{
2 public static void main(String args[]{
3 String s1="hello";
4 String s2="whatsup";
5 System.out.println("string length is: "+s1.length());
System.out.println("string length is: "+s2.length());
6 }}
7
Here, String length() function will return the length 5 for s1 and 7 for s2 respectively.
Java String compareTo(): The Java String compareTo() method compares the
given string with current string. It is a method of ‘Comparable’ interface which is
implemented by String class. Don’t worry, we will be learning about String
interfaces later. It either returns positive number, negative number or 0. For
example:
1
2 public class CompareToExample{
public static void main(String args[]){
3 String s1="hello";
4 String s2="hello";
5 String s3="hemlo";
6 String s4="flag";
7 System.out.println(s1.compareTo(s2)); // 0 because both are equal
System.out.println(s1.compareTo(s3)); //-1 because "l" is only one time lower than "m"
8 System.out.println(s1.compareTo(s4)); // 2 because "h" is 2 times greater than "f"
9 }}
10
This program shows the comparison between the various string. It is noticed that
if s1 > s2, it returns a positive number
if s1 < s2, it returns a negative number
if s1 == s2, it returns 0
Java String concat() : The Java String concat() method combines a specific
string at the end of another string and ultimately returns a combined string. It is
like appending another string. For example:
Java String Trim() : The java string trim() method removes the leading and
trailing spaces. It checks the unicode value of space character (‘\u0020’) before
and after the string. If it exists, then removes the spaces and return the omitted
string. For example:
Java String ValueOf(): This method converts different types of values into
string.Using this method, you can convert int to string, long to string, Boolean to
string, character to string, float to string, double to string, object to string and
char array to string. The signature or syntax of string valueOf() method is given
below:
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(char[] c)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)
public static String valueOf(Object o)
1
2 public class EqualsExample{
public static void main(String args[]){
3 String s1="hello";
4 String s2="hello";
5 String s3="hi";
6 System.out.println(s1.equalsIgnoreCase(s2)); // returns true
System.out.println(s1.equalsIgnoreCase(s3)); // returns false
7 }
8 }
9
Learn Java From Experts
1
public class EqualsIgnoreCaseExample{
2 public static void main(String args[]){
3 String s1="hello";
4 String s2="HELLO";
5 String s3="hi";
6 System.out.println(s1.equalsIgnoreCase(s2)); // returns true
System.out.println(s1.equalsIgnoreCase(s3)); // returns false
7 }}
8
In the above code, the first statement will return true because the content is same
irrespective of the case. Then, in the second print statement will return false as
the content doesn’t match in the respective strings.
Java String toCharArray(): This method converts the string into a character
array i.e first it will calculate the length of the given Java String including spaces
and then create an array of char type with the same content. For example:
1 StringToCharArrayExample{
2 public static void main(String args[]){
3 String s1="Welcome to Edureka";
4 char[] ch=s1.toCharArray();
5 for(int i=0;i<ch.length;i++){
System.out.print(ch[i]);
6 }}}
7
The above code will return “Welcome to Edureka”.
Java StringGetBytes() : The Java string getBytes() method returns the
sequence of bytes or you can say the byte array of the string. For example:
1
public class StringGetBytesExample {
2 public static void main(String args[]){
3 String s1="ABC";
4 byte[] b=s1.getBytes();
5 for(int i=0;i<b.length;i++){
6 System.out.println(b[i]);
}
7 }}
8
In the above code, it will return the value 65,66,67.
Java String IsEmpty() : This method checks whether the String is empty or not.
If the length of the String is 0, it returns true else false. For example:
1
public class IsEmptyExample{
2 public static void main(String args[]) {
3 String s1="";
4 String s2="hello";
5 System.out.prinltn(s1.isEmpty()); // returns true
System.out.prinltn(s2.isEmpty()); // returns false
6 }}
7
In the above code, the first print statement will return true as it does not contain anything
while the second print statement will return false.
Java String endsWith() : The Java String endsWith() method checks if this
string ends with the given suffix. If it returns with the given suffix, it will return
true else returns false. For example:
1
public class EndsWithExample{
2 public static void main(String args[]) {
3 String s1="hello how are you”;
4 System.out.println(s1.endsWith("u")); // returns true
5 System.out.println(s1.endsWith("you")); // returns true
System.out.println(s1.endsWith("how")); // returns false
6 }}
7
Write a program to implement all string operations.
import java.util.*;
class StringOperation
{
public static void main(String[] args)
{
String first="",second="";
Scanner sc=new Scanner(System.in);
System.out.println("String Operation");
System.out.println();
System.out.print("Enter the first Sting: ");
first=sc.nextLine();
System.out.print("Enter the second Sting: ");
second=sc.nextLine();
System.out.println("The strings are: "+first+" , "+second);
System.out.println("The length of the first string is :"+first.length());
System.out.println("The length of the second string is :"+second.length());
System.out.println("The concatenation of first and second string is :"+first.concat("
"+second));
System.out.println("The first character of " +first+" is: "+first.charAt(0));
System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());
System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());
System.out.print("Enter the occurance of a character in "+first+" : ");
String str=sc.next();
char c=str.charAt(0);
System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first);
System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: "
+ first.substring(3,7));
System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));
boolean check=first.equals(second);
if(!check)
System.out.println(first + " and " + second + " are not same.");
else
System.out.println(first + " and " + second + " are same.");
}
}
Output
C:\Users\Jaisha\Desktop\Java>javac StringOperation.java
C:\Users\Jaisha\Desktop\Java>java StringOperation
String Operation