There is a problem with startsWith() method in my application!!!!!

Hi,

A piece of my application is like this:

int TarimSayac=0;

int TarimEksiltSayac=0;

for (int j = 0; j < v.size(); j++){

...

String ipc = obj.getIPCID();

if(ipc.startsWith("A01")) TarimSayac++;

elseif(ipc.startsWith("A01N")) TarimEksiltSayac++;

elseif(...) .....

else .....

}

System.out.println("tarim sayac "+TarimSayac);

System.out.println("tarim eksilt sayac "+TarimEksiltSayac);

I think, there is a problem about startsWith in my application.Because the statement

System.out.println("tarim sayac "+TarimSayac);

has run correctly, but it hasn't run with the statement

System.out.println("tarim eksilt sayac "+TarimEksiltSayac);

In the loop while the value of counter

TarimSayac

is increasing, value of counter

TarimEksiltSayac

returns allways null.

I have compared the result of my application with database and there must exist value of

TarimEksiltSayac

in my application.

I don't understand why my application doesn' t count ipc s that starts with

"A01N"

?

I will be very plaesed when I learn my mistakes.

Thank u:))

[1781 byte] By [a5xa] at [2007-11-26 18:26:37]
# 1

Hello,

The problem is with your if statements.

Suppose you have ipc equals to A01Nwhatever.

The code that will be executed is if (ipc.startsWith("A01"))

which will return true so it will never execute else if (ipc.startsWith("A01N"))

.

You need to change your logic.

virusakosa at 2007-7-9 6:00:41 > top of Java-index,Java Essentials,New To Java...
# 2

Hi,

Change your existing code to

if(ipc.startsWith("A01")) TarimSayac++;

if(ipc.startsWith("A01N")) TarimEksiltSayac++;

if(...) .....

if you feel it would be better.

bye for now

sat

AnanSmritia at 2007-7-9 6:00:41 > top of Java-index,Java Essentials,New To Java...
# 3

> Hi,

>

>Change your existing code to

> ]if(ipc.startsWith("A01")) TarimSayac++;

> if(ipc.startsWith("A01N")) TarimEksiltSayac++;

> if(...) .....[/code]

> if you feel it would be better.

>

> bye for now

> sat

i do not think this is a good solution,

because in a situation of "A01N"

both TarimSayac and TarimEksiltSayac will be increased

if(ipc.startsWith("A01N")) TarimEksiltSayac++;

else if(ipc.startsWith("A01")) TarimSayac++;

will be much better ;)

instead of

if(ipc.startsWith("A01"))

this can be used:

if(ipc.indexOf("A01")==0)

kolay gelsin ;)

Onura at 2007-7-9 6:00:41 > top of Java-index,Java Essentials,New To Java...
# 4

Thank u for your helps:))

But I didn't change all if-else if blocks. In the rest of my codes, there are similar problems with TarimSayac

and TarimEksiltSayac

I have corrected like this:

....

if(ipc.startsWith("A01N")) TarimEksiltSayac++;

else if (ipc.startsWith("A01")) TarimSayac++;

....

else if (ipc.startsWith("A61K")) TibbiSayac++;

else if((ipc.startsWith("A61")||ipc.startsWith("A62")||ipc.startsWith("A63"))&&(!ipc.startsWith("A61K"))) SaglikSayac++;

....

te篹kk黵ler onur:))

a5xa at 2007-7-9 6:00:41 > top of Java-index,Java Essentials,New To Java...
# 5

> i do not think this is a good solution,

> because in a situation of "A01N"

> both TarimSayac and TarimEksiltSayac will be

> increased

Depends on what the OP wants. Maybe he wants both increased.

> if(ipc.startsWith("A01N")) TarimEksiltSayac++;

> else if(ipc.startsWith("A01")) TarimSayac++;

>

> will be much better ;)

>

> instead of

> if(ipc.startsWith("A01"))

> this can be used:

> if(ipc.indexOf("A01")==0)

He should use startsWith in this case. That's what it's there for. Yes, ipc.indexOf("...")==0 gives the same answer, but startsWith is a better description of what he wants.

doremifasollatidoa at 2007-7-9 6:00:41 > top of Java-index,Java Essentials,New To Java...
# 6

> > i do not think this is a good solution,

> > because in a situation of "A01N"

> > both TarimSayac and TarimEksiltSayac will be

> > increased

>

> Depends on what the OP wants. Maybe he wants both

> increased.

You are right but i do not think thats what the OP wants ;)

rica ederim :)

Onura at 2007-7-9 6:00:41 > top of Java-index,Java Essentials,New To Java...
# 7
What was wrong with this thread you had. http://forum.java.sun.com/thread.jspa?threadID=5136666&messageID=9498155#9498155
lupansanseia at 2007-7-9 6:00:41 > top of Java-index,Java Essentials,New To Java...