Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: String problem ..kindly give me a code in alternative methods(solved)

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default String problem ..kindly give me a code in alternative methods(solved)

    I have forcefully solved it but if this is possible with split() or using replace method or another way kindly give me the code. i have tried for a long time.


    suppose the string is "It's a wonderful forum. Are you looking for a new code? It's here. "
    I want to print the line like

    It's a wonderful forum.
    Are you looking for a new code?
    It's here.

    when the character will found "." or "?" then it will get a newline.



     
    package com.company;
     
    import java.util.Arrays;
     
    public class Main {
     
    public static void main(String[] args) {
    String str = "My name?It's me Sumon?There was a logic behind it.where is the basic knowledge?i can go the the room.";
     
    for(int i=0;i<str.length();i++) {
    if ((str.charAt(i) =='?'))
    {
    System.out.print("?");
    System.out.println();
    }
    else if (str.charAt(i)=='.')
    {
    System.out.print(".");
    System.out.println();
    }
    else
    System.out.print(str.charAt(i));
    }
     
    }
     
    }


    here was the output.


    now kindly give me a solution if any possible with split or replace or replaceall.



    thank you.
    Last edited by sumon_bd; December 15th, 2021 at 03:14 AM. Reason: solved

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String problem ..kindly give me a code in alternative methods

    The posted code is missing proper indentations. All the source lines should NOT start in the first column.
    Please post code with proper indentations.
    Also please do not post images of code or error messages or computer output. It is not possible to copy text from an image to include in a response.

    Sorry, I do not give out code for solutions. If you have some java programming questions, please ask them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: String problem ..kindly give me a code in alternative methods

    Quote Originally Posted by Norm View Post
    The posted code is missing proper indentations. All the source lines should NOT start in the first column.
    Please post code with proper indentations.
    Also please do not post images of code or error messages or computer output. It is not possible to copy text from an image to include in a response.

    Sorry, I do not give out code for solutions. If you have some java programming questions, please ask them.
    I gave the code in the code section. Kindly check it.

    which language you are doing ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String problem ..kindly give me a code in alternative methods

    Please fix the indentations for the posted code.

    Is your code working? Do you have any questions about your code?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: String problem ..kindly give me a code in alternative methods

        String str =  "My name?It's me Sumon?There was a logic behind it.where is the basic knowledge?i can go the the room.";
        String[] splited = str.split( "(?<=[\\.|\\?])" );
     
        for (String s: splited)
              System.out.println(s);

  6. The Following User Says Thank You to zemiak For This Useful Post:

    sumon_bd (December 15th, 2021)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String problem ..kindly give me a code in alternative methods

    Ah yes. Regular expressions can do magic.

    Where did you get that code?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: String problem ..kindly give me a code in alternative methods

    Quote Originally Posted by zemiak View Post
        String str =  "My name?It's me Sumon?There was a logic behind it.where is the basic knowledge?i can go the the room.";
        String[] splited = str.split( "(?<=[\\.|\\?])" );
     
        for (String s: splited)
              System.out.println(s);
    this is the perfect and easy system .thanks millions of time.

    i have solved it with replace split with a bigger code.

     
    String str="Hello man.How are you?I am fine and what's about your programming nowadays?would you like to take anymore pressure?yes i will take more pressure.";
     
            String[] word=str.replace("?","?\n").split("");
            for(String w:word)
     
            System.out.print(w);

Similar Threads

  1. Replies: 3
    Last Post: October 11th, 2017, 05:47 AM
  2. Problem with converting Numbers to Words! Kindly ask for help:(
    By ProgrammablePeter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 22nd, 2013, 10:26 AM
  3. Difference in time with alternative methods
    By CruelCoin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 8th, 2013, 02:58 AM
  4. Kindly Review My Code :-) Thanks in advance
    By bhuppi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2012, 10:10 AM
  5. Java coding for writing telephone bill calculator program
    By eyeore in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 8th, 2009, 10:06 AM

Tags for this Thread