English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

what's wrong with this java code:

if (number.indexOf("111") == -1)
threeConsecutive = false;
else
threeConsecutive = true;

I have 111 on my number but why does it still return to false, what's wrong?

2006-10-04 16:04:11 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

i tried your program and it works ok

class Np{
public static void main(String arg[]){
boolean threeConsecutive;
String number="000111222333444";
System.out.println(number.indexOf("11"));
if (number.indexOf("11")==-1){
threeConsecutive = false;
}
else
{threeConsecutive = true;
}System.out.println(threeConsecutive);
}
}
>java -cp ./:$CLASSPATH Np
3
true

better use
(number.indexOf("111")==-1)?false:true)
may be a bug with your jvm version.

2006-10-04 16:57:03 · answer #1 · answered by howsureyouare 3 · 0 0

Do you stop the loop you are in when checking the next three characters? If so, the next loop around will change threeConsecutive to false, and at the end it will be false.

If there are 3 consecutive 1s, it would be best to stop the loop then. If this is not the case, then it may be the old if else else problem, so you may need to place the brackets around your code { }.

2006-10-08 04:41:41 · answer #2 · answered by Mark aka jack573 7 · 0 0

the mission is that some human beings, ignorant on God's note, imagine that it truly is authentic and they do no longer recognize it really is depending in secrets and techniques that they do no longer recognize yet. so as that they studied some authentic "information" and they got here out with some speculative solutions. till they don't locate that secret(probable they are going to not in any respect locate it) you adult adult males seek for on the Bible it really is the holy reality to ascertain about Jesus and God in what they are, what they opt for and what they are gonna do. do not waste a at the same time as in speculations. have you ever study the Bible? - have you ever study the Da Vince Code?....shame on you.

2016-11-26 03:22:23 · answer #3 · answered by Anonymous · 0 0

if number is a string, you have to use .compareTo to determine equality. == will determine if they share the same memory which is not what you want most likely.

2006-10-05 08:17:32 · answer #4 · answered by litlbruce 1 · 1 0

{ proper semicolons? }

2006-10-04 16:12:12 · answer #5 · answered by J G 4 · 0 0

two equal signs?

2006-10-04 16:06:26 · answer #6 · answered by metallhd62 4 · 0 2

fedest.com, questions and answers