Programming Articles

Page 21 of 2546

Write a java program to tOGGLE each word in string?

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 1K+ Views

You can change the cases of the letters of a word using toUpperCase() and toLowerCase() methods.Split each word in the string using the split() method, change the first letter of each word to lower case and remaining letters to upper case.Examplepublic class Sample{    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputhELLO hOW aRE yOU

Read More

Check if a string contains a number using Java.

Anjana
Anjana
Updated on 11-Mar-2026 41K+ Views

To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.Examplepublic class ContainsExample {    public static void main(String args[]){       String sample = "krishna64";       char[] chars = sample.toCharArray();       StringBuilder sb = new StringBuilder();       for(char c : chars){          if(Character.isDigit(c)){             sb.append(c);          }       }       System.out.println(sb);    } }Output64

Read More

Java String toCharArray() method example.

Sai Nath
Sai Nath
Updated on 11-Mar-2026 138 Views

The toCharArray() method of a String class converts this string to a character array.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       // converts String value to character array type value       String str = " Java was developed by James Gosling";       char retval[] = str.toCharArray();       // displays the converted value       System.out.println("Converted value to character array = ");       System.out.println(retval);    } }OutputConverted value to character array = Java was developed by James Gosling

Read More

How to test String is null or empty?

V Jyothi
V Jyothi
Updated on 11-Mar-2026 607 Views

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str = "tutorialspoint";       // prints length of string       System.out.println("length of string = " + str.length());       // checks if the string is empty or not       System.out.println("is this string empty? = " + str.isEmpty());    } }Outputlength of string = 14 is this string empty? = false

Read More

How to Split a String with Escaped Delimiters?

Sravani S
Sravani S
Updated on 11-Mar-2026 511 Views

The split(String regex) method of the String class splits this string around matches of the given regular expression.Examplepublic class Sample{    public static void main(String args[]){       String s = "|A|BB||CCC|||";       String[] words = s.split("\|");       for (String string : words) {          System.out.println(string);       }    } }OutputA BB CCC

Read More

Java string case change sample code examples.

Abhinanda Shri
Abhinanda Shri
Updated on 11-Mar-2026 197 Views

You can change the cases using the toUpperCase() and toLowerCase() methods of the String class.Examplepublic class Sample {    public static void main(String args[]){       String str = "Hello how are you";       String strUpper = str.toUpperCase();       System.out.println("Lower to upper : "+strUpper);       String strLower = str.toLowerCase();       System.out.println("Upper to lower : "+strLower);    } }OutputLower to upper : HELLO HOW ARE YOU Upper to lower : hello how are you

Read More

Data Conversion Using valueOf() In Java.

Priya Pallavi
Priya Pallavi
Updated on 11-Mar-2026 398 Views

Java String class provides several variants of valueOf() method. These accept various data types and convert them into String.Examplepublic class Sample {    public static void main(String args[]){       int i = 200;       float f = 12.0f;       char c = 's';       char[] ch = {'h', 'e', 'l', 'l', 'o'};       String data = String.valueOf(i);       System.out.println(String.valueOf(i));       System.out.println(String.valueOf(f));       System.out.println(String.valueOf(c));       System.out.println(String.valueOf(ch));    } }Output200 12.0 s hello

Read More

How to Convert Double to String to Double in Java?

V Jyothi
V Jyothi
Updated on 11-Mar-2026 398 Views

Java lang package provides a Double class which has methods to convert Double to String and vice versa. You can convert a String to a double using the parseDouble() method and double to String using the toString() methodExamplepublic class StringDouble {    public static void main(String args[]){       Double data1 = 2.2;       String str = Double.toString(data1);       System.out.println(str);       Double data2 = Double.parseDouble(str);       System.out.println(data2);    } }Output2.2 2.2

Read More

A closer look at Java "Hello World" program

Daniol Thomas
Daniol Thomas
Updated on 11-Mar-2026 385 Views

Let us look at a simple code that will print the words Hello World.Examplepublic class MyFirstJavaProgram {    /* This is my first java program. *    This will print 'Hello World' as the output */    public static void main(String []args) {       System.out.println("Hello World"); // prints Hello World    } }Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −Open notepad and add the code as above.Save the file as: MyFirstJavaProgram.java.Open a command prompt window and go to the directory where you saved the class. Assume it's ...

Read More

Why can't we define a static method in a Java interface?

Anjana
Anjana
Updated on 11-Mar-2026 818 Views

From Java 8 onwards, static methods are allowed in Java interfaces.An interface can also have static helper methods from Java 8 onwards. public interface vehicle {    default void print() {       System.out.println("I am a vehicle!");    }    static void blowHorn() {       System.out.println("Blowing horn!!!");    } }Default Method ExampleCreate the following Java program using any editor of your choice in, say, C:\> JAVA.Java8Tester.javapublic class Java8Tester {    public static void main(String args[]) {       Vehicle vehicle = new Car(); vehicle.print();    } } interface Vehicle {    default void print() {   ...

Read More
Showing 201–210 of 25,451 articles
« Prev 1 19 20 21 22 23 2546 Next »
Advertisements