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 8 of 8

Thread: finding a character from a given string value

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

    Default finding a character from a given string value

    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.


    i am looking for a code.

    thank you

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: finding a character from a given string value

    The String class's indexOf and substring methods could be used to find the end of each sentence and extract the sentence.
    Or a newline character could be inserted in the String following the locations of the . or ?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    sumon_bd (December 10th, 2021)

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

    Default Re: finding a character from a given string value

    Quote Originally Posted by Norm View Post
    The String class's indexOf and substring methods could be used to find the end of each sentence and extract the sentence.
    Or a newline character could be inserted in the String following the locations of the . or ?
    would you like to give me the code. i have done it by own but there is a problem that ? mark goes to the next line. i need to add newline after the ? is printed

     
    package com.company;
     
    import java.util.Arrays;
     
    public class Main {
     
        public static void main(String[] args) {
            String str = "My name? it's me sumon";
     
            for(int i=0;i<str.length();i++)
            {
                if (str.charAt(i)=='?'){
                    System.out.print("\n");}
                System.out.print(str.charAt(i));
            }
     
                }
            }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: finding a character from a given string value

    a problem that ? mark goes to the next line.
    Print the newline AFTER printing the ?
    In other words: print the characters one by one and if the last one printed was a ? print a newline
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding a character from a given string value

    i have forcefully solved it but still there is a problem of one space.

     
    package com.company;
     
    import java.util.Arrays;
     
    public class Main {
     
        public static void main(String[] args) {
            String str = "My name? it's me sumon";
     
            for(int i=0;i<str.length();i++) {
                if (str.charAt(i) =='?')
                {
                    System.out.print("?");
                    System.out.println();
                }
                else
                    System.out.print(str.charAt(i));
            }
     
     
            }
     
     
     
        }





    if possible give me a solution

    thank you.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: finding a character from a given string value

    there is a problem of one space
    Please describe in more detail. Is there one space too many or one space too few.
    Where is it?
    Copy the output and paste it here will comments about the space problem.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    sumon_bd (December 10th, 2021)

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

    Default Re: finding a character from a given string value

    very interesting i have solved it.

    yes it's perfect now. there was a space counter into the string.

    now i will use this code into a large section .. try catch expection and reading file from a large text or pdf where it will make a para when it will get fullstop, question marks etc.


  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: finding a character from a given string value

    I am glad you have it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. Replies: 1
    Last Post: April 1st, 2013, 04:13 PM
  3. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  4. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM