split() and String []

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Frayn

    split() and String []

    Hey,

    Okay, so with the following code:

    // assume this is full of input already
    String MyInput;
    String [] MySplits;

    MySplits = MyInput.split(" ;");

    How can I determine how many strings were returned into
    MySplits without iterating from 0 to 1,000,000 and waiting
    for an exception to be thrown?

    Thanks,
    Mike


  • Jeff Schwab

    #2
    Re: split() and String []

    Mike Frayn wrote:[color=blue]
    > Hey,
    >
    > Okay, so with the following code:
    >
    > // assume this is full of input already
    > String MyInput;
    > String [] MySplits;
    >
    > MySplits = MyInput.split(" ;");
    >
    > How can I determine how many strings were returned into
    > MySplits without iterating from 0 to 1,000,000 and waiting
    > for an exception to be thrown?
    >
    > Thanks,
    > Mike
    >
    >[/color]

    MySplits.length

    Comment

    • Mike Frayn

      #3
      Re: split() and String []

      Okay, that's embarrassing. I remember looking for that and
      not finding it... must've been looking at a regular string.
      Ouch.

      Thanks,
      Mike

      "Jeff Schwab" <jeffplus@comca st.net> wrote in message
      news:T_GdndDjW5 Sqw_DdRVn-gg@comcast.com. ..[color=blue]
      > Mike Frayn wrote:[color=green]
      > > Hey,
      > >
      > > Okay, so with the following code:
      > >
      > > // assume this is full of input already
      > > String MyInput;
      > > String [] MySplits;
      > >
      > > MySplits = MyInput.split(" ;");
      > >
      > > How can I determine how many strings were returned into
      > > MySplits without iterating from 0 to 1,000,000 and[/color][/color]
      waiting[color=blue][color=green]
      > > for an exception to be thrown?
      > >
      > > Thanks,
      > > Mike
      > >
      > >[/color]
      >
      > MySplits.length[/color]


      Comment

      • Mike Frayn

        #4
        Re: split() and String []

        > Okay, that's embarrassing. I remember looking for that
        and[color=blue]
        > not finding it... must've been looking at a regular[/color]
        string.[color=blue]
        > Ouch.[/color]

        Uhhhh, nope that's not a valid excuse as a String has that
        too. Right, so I have no idea why I missed that!!! Double
        ouch.

        Mike


        Comment

        • Yoyoma_2

          #5
          Re: split() and String []

          Mike Frayn wrote:[color=blue][color=green]
          >>Okay, that's embarrassing. I remember looking for that[/color]
          >
          > and
          >[color=green]
          >>not finding it... must've been looking at a regular[/color]
          >
          > string.
          >[color=green]
          >>Ouch.[/color]
          >
          >
          > Uhhhh, nope that's not a valid excuse as a String has that
          > too. Right, so I have no idea why I missed that!!! Double
          > ouch.
          >
          > Mike[/color]


          In your original code you had a variable name like "String MyString[]=".
          Java standard requires that variable and function names start with lower
          case. Its a good habit to pick up. An external programmer can drop
          into your code and know that MyString isn't a class that you are making
          static calls to. Also it makes the code a lot prettyer.

          Just a suggestion.

          Comment

          • Jeff Schwab

            #6
            Re: split() and String []

            Yoyoma_2 wrote:[color=blue]
            > Mike Frayn wrote:
            >[color=green][color=darkred]
            >>> Okay, that's embarrassing. I remember looking for that[/color]
            >>
            >>
            >> and
            >>[color=darkred]
            >>> not finding it... must've been looking at a regular[/color]
            >>
            >>
            >> string.
            >>[color=darkred]
            >>> Ouch.[/color]
            >>
            >>
            >>
            >> Uhhhh, nope that's not a valid excuse as a String has that
            >> too. Right, so I have no idea why I missed that!!! Double
            >> ouch.
            >>
            >> Mike[/color]
            >
            >
            >
            > In your original code you had a variable name like "String MyString[]=".
            > Java standard requires that variable and function names start with lower
            > case. Its a good habit to pick up. An external programmer can drop
            > into your code and know that MyString isn't a class that you are making
            > static calls to. Also it makes the code a lot prettyer.
            >
            > Just a suggestion.[/color]


            What standard? Do you mean Sun's guidelines? They're not a standard,
            and nobody has to follow them. (That said, I agree with you completely
            that variables should begin with lower-case letters.)

            Comment

            • Ryan Stewart

              #7
              Re: split() and String []

              "Jeff Schwab" <jeffplus@comca st.net> wrote in message
              news:Xs2dnUnfqv VLhvPdRVn-sA@comcast.com. ..[color=blue]
              > Yoyoma_2 wrote:[color=green]
              > > In your original code you had a variable name like "String MyString[]=".
              > > Java standard requires that variable and function names start with lower
              > > case. Its a good habit to pick up. An external programmer can drop
              > > into your code and know that MyString isn't a class that you are making
              > > static calls to. Also it makes the code a lot prettyer.
              > >
              > > Just a suggestion.[/color]
              >
              >
              > What standard? Do you mean Sun's guidelines? They're not a standard,
              > and nobody has to follow them. (That said, I agree with you completely
              > that variables should begin with lower-case letters.)
              >[/color]


              They are a standard, but you're right. Nobody has to follow them, just like
              nobody has to drive their car in the correct lane or obey traffic signals.
              Granted those are laws rather than standards, but nobody's ever gotten
              killed by practicing improper coding techniques (unfortunately? ). If you
              don't support following code standards, you must not have ever had to
              develop code in cooperation with other people.


              Comment

              • Jeff Schwab

                #8
                Re: split() and String []

                Ryan Stewart wrote:[color=blue]
                > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                > news:Xs2dnUnfqv VLhvPdRVn-sA@comcast.com. ..
                >[color=green]
                >>Yoyoma_2 wrote:
                >>[color=darkred]
                >>>In your original code you had a variable name like "String MyString[]=".
                >>>Java standard requires that variable and function names start with lower
                >>>case. Its a good habit to pick up. An external programmer can drop
                >>>into your code and know that MyString isn't a class that you are making
                >>>static calls to. Also it makes the code a lot prettyer.
                >>>
                >>>Just a suggestion.[/color]
                >>
                >>
                >>What standard? Do you mean Sun's guidelines? They're not a standard,
                >>and nobody has to follow them. (That said, I agree with you completely
                >>that variables should begin with lower-case letters.)
                >>[/color]
                >
                > http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html[/color]

                Those are conventions. The title of the document is Code Conventions.
                Not standards.
                [color=blue]
                > They are a standard, but you're right. Nobody has to follow them, just like
                > nobody has to drive their car in the correct lane or obey traffic signals.[/color]

                I like your analogy. Suppose that you are from the US, and are thus
                used to driving on the right [direction, not correctness] side of the
                road. Now, suppose you are with someone in the UK, and you notice that
                they are driving on the left. Do you suggest they switch to the other
                side, since they are not complying with the Standard? Of course not.
                To do so would not fit local custom, and would be silly and
                irresponsible. Moreover, you would be steering them into on-coming traffic.
                [color=blue]
                > Granted those are laws rather than standards, but nobody's ever gotten
                > killed by practicing improper coding techniques (unfortunately? ).[/color]

                The OP's capitalization isn't "improper," it just doesn't follow your
                preferred conventions.
                [color=blue]
                > If you
                > don't support following code standards, you must not have ever had to
                > develop code in cooperation with other people.[/color]

                In the presence of guidelines, I follow them as closely as possible,
                unless there is a darned good reason to do otherwise; for example, I
                tend to use whitespace to align semantically related items. I've used a
                number of different sets of coding guidelines over the years, and most
                of them conflict with each other. My problem isn't with coding
                guidelines, though; it's with telling somebody who never agreed to
                follow a particular convention that he's being "non-standard," possibly
                for complying with his own company's entirely different guidelines. The
                term "standard" has a meaning different from "convention " or
                "guideline. " Plenty of tools exist to reformat code automatically, but
                there's no magic wand you can wave to fix violations of an actual standard.

                Comment

                • Yoyoma_2

                  #9
                  Re: split() and String []

                  Jeff Schwab wrote:[color=blue]
                  > Ryan Stewart wrote:
                  >[color=green]
                  >> "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                  >> news:Xs2dnUnfqv VLhvPdRVn-sA@comcast.com. ..
                  >>[color=darkred]
                  >>> Yoyoma_2 wrote:
                  >>>
                  >>>> In your original code you had a variable name like "String
                  >>>> MyString[]=".
                  >>>> Java standard requires that variable and function names start with
                  >>>> lower
                  >>>> case. Its a good habit to pick up. An external programmer can drop
                  >>>> into your code and know that MyString isn't a class that you are making
                  >>>> static calls to. Also it makes the code a lot prettyer.
                  >>>>
                  >>>> Just a suggestion.
                  >>>
                  >>>
                  >>>
                  >>> What standard? Do you mean Sun's guidelines? They're not a standard,
                  >>> and nobody has to follow them. (That said, I agree with you completely
                  >>> that variables should begin with lower-case letters.)
                  >>>[/color]
                  >>
                  >> http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html[/color]
                  >
                  >
                  > Those are conventions. The title of the document is Code Conventions.
                  > Not standards.[/color]

                  Yes it is a convention, i'me sorry, the company I worked for before
                  adopted them into there coding practices standard. Yes they are
                  guidelines, but i'lle admit they are adopted pretty much at every major
                  java firm i've seen. So it could be considered a defacto standard.

                  [color=blue]
                  >[color=green]
                  >> They are a standard, but you're right. Nobody has to follow them, just
                  >> like
                  >> nobody has to drive their car in the correct lane or obey traffic
                  >> signals.[/color]
                  >
                  >
                  > I like your analogy. Suppose that you are from the US, and are thus
                  > used to driving on the right [direction, not correctness] side of the
                  > road. Now, suppose you are with someone in the UK, and you notice that
                  > they are driving on the left. Do you suggest they switch to the other
                  > side, since they are not complying with the Standard? Of course not. To
                  > do so would not fit local custom, and would be silly and irresponsible.
                  > Moreover, you would be steering them into on-coming traffic.[/color]

                  But we aren't talking about driving, this is standards within a global
                  economy. It is not geographically specific, it is corporation specific.
                  Corporations try to minimize the learning curve of new employees as
                  much as possible because of the high turnover rate of IT professionals.
                  Adopting standards such as this one helps with that. A programmer is
                  not repulsed by "Ugly Code". Consistancy is an issue here. If you
                  write your code in this fashion, make sure all the other code from your
                  corporation is written in the same way.
                  [color=blue]
                  >[color=green]
                  >> Granted those are laws rather than standards, but nobody's ever gotten
                  >> killed by practicing improper coding techniques (unfortunately? ).[/color]
                  >
                  > The OP's capitalization isn't "improper," it just doesn't follow your
                  > preferred conventions.[/color]

                  Frustration over standards sure tend to make me homicidal LOL. No you'r
                  right its not my "preffered" convention, but sun recomends this one. I
                  was saying my original comment because junior programmers tend to write
                  code like this sometimes. I remind them of the guidelines to enforce
                  the idea of coding standards. So to me it looks amateurish because only
                  junior programmers do "MyClass MyClassInstance = new MyClass();
                  MyClassInstance .Foo();"
                  [color=blue][color=green]
                  >> If you
                  >> don't support following code standards, you must not have ever had to
                  >> develop code in cooperation with other people.[/color]
                  >
                  >
                  > In the presence of guidelines, I follow them as closely as possible,
                  > unless there is a darned good reason to do otherwise; for example, I
                  > tend to use whitespace to align semantically related items. I've used a
                  > number of different sets of coding guidelines over the years, and most
                  > of them conflict with each other. My problem isn't with coding
                  > guidelines, though; it's with telling somebody who never agreed to
                  > follow a particular convention that he's being "non-standard," possibly
                  > for complying with his own company's entirely different guidelines. The
                  > term "standard" has a meaning different from "convention " or
                  > "guideline. " Plenty of tools exist to reformat code automatically, but
                  > there's no magic wand you can wave to fix violations of an actual standard.[/color]


                  Like i said, new programmers should learn about coding standards.
                  Students are obliged to use the java standard to write code (even
                  sometimes in C++ related classes). Junior programmers as well. They
                  need to learn the principles of standards. If your corporation
                  developed their own and is fully adopted, fine congradulations , what a
                  nice thing. But most juniors don't have that concept. And I got the
                  feeling that this guy was not a seasoned professional.

                  Comment

                  • Jeff Schwab

                    #10
                    Re: split() and String []


                    <snip> conversation re. coding conventions </snip>

                    Fair enough.

                    Comment

                    • Ryan Stewart

                      #11
                      Re: split() and String []

                      "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                      news:otednS4mm_ tsXvPdRVn-iQ@comcast.com. ..[color=blue]
                      > Ryan Stewart wrote:[color=green]
                      > > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                      > > news:Xs2dnUnfqv VLhvPdRVn-sA@comcast.com. ..
                      > > http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html[/color]
                      >
                      > Those are conventions. The title of the document is Code Conventions.
                      > Not standards.[/color]

                      Convention: A practice or procedure widely observed in a group
                      Standard: Something, such as a practice or a product, that is widely
                      recognized or employed
                      -- The American Heritage® Dictionary of the English Language, Fourth Edition

                      The standards, or conventions if you prefer, set forth in that document are
                      a condensation of those suggested in the JLS. Are they laws? No. Are they
                      guidelines? Yes. Are there very good reasons to follow them? Yes, especially
                      if you ever intend 1) for anyone else to see your code or 2) to read code
                      written by others.


                      Comment

                      • Mike Frayn

                        #12
                        Re: split() and String []

                        >But most juniors don't have that concept. And I got the[color=blue]
                        > feeling that this guy was not a seasoned professional.[/color]

                        Allow me to interject into your debate for a moment if I
                        may, as I was the instigator here and you are now presuming
                        as to my abilities :)

                        I've actually never used Java before (as I mentioned), but
                        have been using C and C++ for as long as I can remember. I
                        am using Java temporarily as my University says that I have
                        to <grin>. So, as I normally use Hungarian Notation (very
                        strictly) in my C++ code, I will undoubtedly use the same in
                        Java (like it or hate it is not the point).

                        My point here is this, regardless of whatever I wrote in
                        here, somebody would tell me that it was wrong:

                        1) Hungarian: "This is not C++" or "It is not necessary to
                        abide to that standard mindlessly"

                        2) Your Java standard: "It is not necessary to abide to that
                        standard mindlessly"

                        3) No standard: Your response.

                        By luck of the draw I used no notation here, why I don't
                        remember but more than likely because it was a simple
                        example and notation had no effect on its readability. Last
                        week I posted something in a C++ newsgroup with Hungarian
                        Notation and a similar thread "discussion " was created about
                        whether that standard was necessary, etc..

                        Regardless, I did appreciate the help. Please feel free to
                        continue the argument now.

                        Mike


                        Comment

                        • Jeff Schwab

                          #13
                          Re: split() and String []

                          Mike Frayn wrote:[color=blue][color=green]
                          >>But most juniors don't have that concept. And I got the
                          >>feeling that this guy was not a seasoned professional.[/color]
                          >
                          >
                          > Allow me to interject into your debate for a moment if I
                          > may, as I was the instigator here and you are now presuming
                          > as to my abilities :)[/color]

                          <snip> OP uses strict convention in C++, hasn't decided on one in Java </>
                          [color=blue]
                          > Regardless, I did appreciate the help. Please feel free to
                          > continue the argument now.[/color]

                          LOL

                          Comment

                          • Jeff Schwab

                            #14
                            Re: split() and String []

                            Ryan Stewart wrote:[color=blue]
                            > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                            > news:otednS4mm_ tsXvPdRVn-iQ@comcast.com. ..
                            >[color=green]
                            >>Ryan Stewart wrote:
                            >>[color=darkred]
                            >>>"Jeff Schwab" <jeffplus@comca st.net> wrote in message
                            >>>news:Xs2dnUn fqvVLhvPdRVn-sA@comcast.com. ..
                            >>>http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html[/color]
                            >>
                            >>Those are conventions. The title of the document is Code Conventions.
                            >>Not standards.[/color]
                            >
                            >
                            > Convention: A practice or procedure widely observed in a group
                            > Standard: Something, such as a practice or a product, that is widely
                            > recognized or employed
                            > -- The American Heritage® Dictionary of the English Language, Fourth Edition[/color]

                            In the context of a group dedicated to the discussion of a particular
                            programming language, "standard" has stricter connotations than convention.

                            [color=blue]
                            > The standards, or conventions if you prefer, set forth in that document are
                            > a condensation of those suggested in the JLS. Are they laws? No. Are they
                            > guidelines? Yes. Are there very good reasons to follow them? Yes, especially
                            > if you ever intend 1) for anyone else to see your code or 2) to read code
                            > written by others.[/color]

                            I wasn't arguing against the use of code conventions, only the attempt
                            to impose one particular convention on the whole, wide world.

                            Comment

                            • Ryan Stewart

                              #15
                              Re: split() and String []

                              "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                              news:C5ydnW3U9Y TgJ_LdRVn-jg@comcast.com. ..[color=blue]
                              > Ryan Stewart wrote:[color=green]
                              > > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                              > > news:otednS4mm_ tsXvPdRVn-iQ@comcast.com. ..[color=darkred]
                              > >>Ryan Stewart wrote:
                              > >>
                              > >>>"Jeff Schwab" <jeffplus@comca st.net> wrote in message
                              > >>>news:Xs2dnUn fqvVLhvPdRVn-sA@comcast.com. ..
                              > >>>http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html
                              > >>
                              > >>Those are conventions. The title of the document is Code Conventions.
                              > >>Not standards.[/color]
                              > >
                              > >
                              > > Convention: A practice or procedure widely observed in a group
                              > > Standard: Something, such as a practice or a product, that is widely
                              > > recognized or employed
                              > > -- The American Heritage® Dictionary of the English Language, Fourth[/color][/color]
                              Edition[color=blue]
                              >
                              > In the context of a group dedicated to the discussion of a particular
                              > programming language, "standard" has stricter connotations than[/color]
                              convention.[color=blue]
                              >[/color]
                              Oh, I see. We're going to redefine words so that language becomes an
                              impediment to comprehension.
                              [color=blue][color=green]
                              > > The standards, or conventions if you prefer, set forth in that document[/color][/color]
                              are[color=blue][color=green]
                              > > a condensation of those suggested in the JLS. Are they laws? No. Are[/color][/color]
                              they[color=blue][color=green]
                              > > guidelines? Yes. Are there very good reasons to follow them? Yes,[/color][/color]
                              especially[color=blue][color=green]
                              > > if you ever intend 1) for anyone else to see your code or 2) to read[/color][/color]
                              code[color=blue][color=green]
                              > > written by others.[/color]
                              >
                              > I wasn't arguing against the use of code conventions, only the attempt
                              > to impose one particular convention on the whole, wide world.[/color]

                              What's the point of having them if their use is not encouraged?


                              Comment

                              Working...