calculate string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sang
    New Member
    • Sep 2006
    • 83

    calculate string

    Hai

    i am working in java development team, i want to calulate the number of string occurs in the input. that is
    String s = ("java","mysql" ); in this java is one string and mysql is one string in the output it will give 2.

    i am tried but i will count the character not a string.

    so please help me as soon as possible.

    Thanks
    Sang
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sang
    Hai

    i am working in java development team, i want to calulate the number of string occurs in the input. that is
    String s = ("java","mysql" ); in this java is one string and mysql is one string in the output it will give 2.

    i am tried but i will count the character not a string.

    so please help me as soon as possible.

    Thanks
    Sang
    You need to be more specific on the input format.
    Do you want to count the number of words in a string?
    What is your separator(comma , space, etc)?
    String s = ("java","mysql" ); will not compile

    Comment

    • sang
      New Member
      • Sep 2006
      • 83

      #3
      i want to count the no.of string present in the input.

      my seperator is comma

      input in string array. (ie) String[] input ={"java","mysql "); output is 2

      Thanks
      Sang

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sang
        i want to count the no.of string present in the input.

        my seperator is comma

        input in string array. (ie) String[] input ={"java","mysql "); output is 2

        Thanks
        Sang
        You could use StringTokenizer or better still regular expressions. It should not be too difficult to look these up and use them.

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          Either input.length() or maybe input.length should tell you how many entries are in the String array. Assuming 1 string per entry, that's what you want isn't it?

          Comment

          • sang
            New Member
            • Sep 2006
            • 83

            #6
            i am tried with this but its also give the no.of.character in the input string please give the solution.


            import java.io.*;
            import java.lang.Strin g.*;
            class teststr
            {
            public static void main(String arg[]) throws Exception
            {
            String aString = "this is a string i am a programmer";
            int length = aString.length( );

            System.out.prin tln (aString);
            System.out.prin tln (length);

            }
            }

            i tried to seperates the string using cooma but i will get the error

            Thanks
            Sang

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by sang
              i am tried with this but its also give the no.of.character in the input string please give the solution.


              import java.io.*;
              import java.lang.Strin g.*;
              class teststr
              {
              public static void main(String arg[]) throws Exception
              {
              String aString = "this is a string i am a programmer";
              int length = aString.length( );

              System.out.prin tln (aString);
              System.out.prin tln (length);

              }
              }

              i tried to seperates the string using cooma but i will get the error

              Thanks
              Sang
              First you do not need to import anything in java.lang.
              The whole java.lang package is automatically imported for you in any program you write.
              You also imported java.io which is not used in your program anyway.

              Here is a program that will split a string using a delimiter
              Code:
              class TestStr {
              	public static void main(String arg[]) {
              		String aString = "this is a string i am a programmer";
              		String[] arr = aString.split(" ");
              		int length = arr.length;
              
              		System.out.println (aString);
              		System.out.println (length);
              
              	}
              }

              Comment

              Working...