By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

split() and String []

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Mike Frayn wrote:
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


MySplits.length
Jul 17 '05 #2

P: n/a
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" <je******@comcast.net> wrote in message
news:T_********************@comcast.com...
Mike Frayn wrote:
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


MySplits.length

Jul 17 '05 #3

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


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
Jul 17 '05 #4

P: n/a
Mike Frayn wrote:
Okay, that's embarrassing. I remember looking for that


and
not finding it... must've been looking at a regular


string.
Ouch.

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

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.
Jul 17 '05 #5

P: n/a
Yoyoma_2 wrote:
Mike Frayn wrote:
Okay, that's embarrassing. I remember looking for that

and
not finding it... must've been looking at a regular

string.
Ouch.


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


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.)

Jul 17 '05 #6

P: n/a
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Xs********************@comcast.com...
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.)

http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html

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.
Jul 17 '05 #7

P: n/a
Ryan Stewart wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Xs********************@comcast.com...
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.)


http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html


Those are conventions. The title of the document is Code Conventions.
Not standards.
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.
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.
Granted those are laws rather than standards, but nobody's ever gotten
killed by practicing improper coding techniques (unfortunately?).
The OP's capitalization isn't "improper," it just doesn't follow your
preferred conventions.
If you
don't support following code standards, you must not have ever had to
develop code in cooperation with other people.


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.
Jul 17 '05 #8

P: n/a
Jeff Schwab wrote:
Ryan Stewart wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Xs********************@comcast.com...
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.)

http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html

Those are conventions. The title of the document is Code Conventions.
Not standards.


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.

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.

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.


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.
Granted those are laws rather than standards, but nobody's ever gotten
killed by practicing improper coding techniques (unfortunately?).


The OP's capitalization isn't "improper," it just doesn't follow your
preferred conventions.


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();"
If you
don't support following code standards, you must not have ever had to
develop code in cooperation with other people.

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.

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.
Jul 17 '05 #9

P: n/a

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

Fair enough.
Jul 17 '05 #10

P: n/a
"Jeff Schwab" <je******@comcast.net> wrote in message
news:ot********************@comcast.com...
Ryan Stewart wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Xs********************@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.


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.
Jul 17 '05 #11

P: n/a
>But most juniors don't have that concept. And I got the
feeling that this guy was not a seasoned professional.


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
Jul 17 '05 #12

P: n/a
Mike Frayn wrote:
But most juniors don't have that concept. And I got the
feeling that this guy was not a seasoned professional.

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 :)


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


LOL
Jul 17 '05 #13

P: n/a
Ryan Stewart wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:ot********************@comcast.com...
Ryan Stewart wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:Xs********************@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.

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


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

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.


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

P: n/a
"Jeff Schwab" <je******@comcast.net> wrote in message
news:C5********************@comcast.com...
Ryan Stewart wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:ot********************@comcast.com...
Ryan Stewart wrote:

"Jeff Schwab" <je******@comcast.net> wrote in message
news:Xs********************@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.

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
In the context of a group dedicated to the discussion of a particular
programming language, "standard" has stricter connotations than convention.

Oh, I see. We're going to redefine words so that language becomes an
impediment to comprehension.
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.


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


What's the point of having them if their use is not encouraged?
Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.