Thought of the Day!!!!!!!

Don't make EXCUSES, make IMPROVEMENTS. - Tyra Banks

Search This Blog

Showing posts with label split(). Show all posts
Showing posts with label split(). Show all posts

Friday, August 11, 2017

How to print Hello World as Olleh Dlrow in JAVA?

Question: Print Hello World as 'Olleh Dlrow'.

Problem: Write a program which reverses the each word in a sentence and also while printing first letter should be in upper case. Input can contain any number of words as mentioned below:


Input String:- 
             Hello World
             ask for help
            You Live Only Once

Output would be as:-
            Olleh Dlrow
            Ksa Rof Pleh
            Uoy Evil Ylno Ecno

Code:
public class ReverseWord
{

public static void main(String[] args)
{
// Step #1: Taking multiple Strings in the array.
String[] strArray={"Hello World","ask for help","You Live Only Once"};

for (String string : strArray)
{
reverseString(string);
}

}

public static void reverseString(String string)
{
// Step #2: Extracting words from a sentence.
String[] getWords=string.split(" ");

for (String word : getWords)
{
// Step #3: Getting reverse of each word.
String revString=new StringBuffer(word).reverse().toString();

// Step #4: Creating first letter in Upper case and rest in Lower case for each word.
revString=revString.substring(0, 1).toUpperCase()+revString.substring(1).toLowerCase();

// Step #5: Printing the expected Output
System.out.print(revString+" ");
}
System.out.println();
}
}


If anybody has any alternate solution please Post Your Answer in the comment box.

Please do comment and share the post with your friends and colleagues. For any query or question, you can also email me at ashu.kumar940@gmail.com.

Sunday, July 09, 2017

How to split String if it contains period symbol (.) in between?

Question: How to split String if it contains period symbol (.) in between?

Problem:
I have to write a program which takes file name with any extension as a parameter and it should return the extension of that file.


For Example:
If input parameters are as:
               input_1 = testdata.properties
               input_2 = userList.xlsx

Output would be:
              output_1 = .properties
              output_2 = .xlsx

Code Snippet:
Currently, I am using split() function with the regular expression ("."), of String class to achieve this as mentioned below:

                        String fileExt = getExtenstion("testdata.properties");
                        System.out.println("Extension of File : "+fileExt);

                        public String getExtension(String fileName)
                        {
                                String tempArray[] = fileName.split(".");
                                 String fileExt = tempArray[1];
                                 return fileExt;
                         }

When I executed this program I got, "ArrayIndexOutOfBoundsException" for yellow highlighted line of code as mentioned above.

Please help me out to get this code corrected. Thanks in advance.

Please do comment and share the post with your friends and colleagues. For any query or question, you can also mail me at ashu.kumar940@gmail.com.


Other Blogs: