| | |
Find out the problem of my code.
![]() |
•
•
Join Date: Jul 2004
Posts: 16
Reputation:
Solved Threads: 0
hi,
can anyone pls find out the problem of my code.
Code:
public class binarysearch {
public static void main ( String[] args ) {
int[] array = {2,2,10,20,55,100};
int target = 2;
int left = 0;
int right = array.length - 1;
int mid = -1;
while ( left <= right ) {
mid = ( left + right ) / 2;
if ( target == array[mid] )
break;
else if ( target < array[mid] )
right = mid - 1;
else
left = mid - 1;
}
if ( mid != -1 )
System.out.println ( target + " found at index " + mid );
else
System.out.println ( target + " not found" );
}
}
Problem :
here it's only showing me the 1st position, but i want it to show me both the position. can anyone pls fix-up the error of my code.
thank you
shantuli
can anyone pls find out the problem of my code.
Code:
public class binarysearch {
public static void main ( String[] args ) {
int[] array = {2,2,10,20,55,100};
int target = 2;
int left = 0;
int right = array.length - 1;
int mid = -1;
while ( left <= right ) {
mid = ( left + right ) / 2;
if ( target == array[mid] )
break;
else if ( target < array[mid] )
right = mid - 1;
else
left = mid - 1;
}
if ( mid != -1 )
System.out.println ( target + " found at index " + mid );
else
System.out.println ( target + " not found" );
}
}
Problem :
here it's only showing me the 1st position, but i want it to show me both the position. can anyone pls fix-up the error of my code.
thank you
shantuli
ooh! I get it, you want to find all the indexes where a number "target" has appeared, right? Well it's showing 0, you also need 1, cause '2' also appears in that index. However, according to your program, there is no way to put that position. I' have no idea about Java, but I have done it in C++ (just now LOL), so I'll try to convert it, and tell you. You can use continue statement too for more clarity.
•
•
•
•
I' have no idea about Java, but I have done it in C++
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
>this post is nearly 3 years old
Argh! no ways.. I thought I was a recent one, I saw it in the display of similar threads -_-' Sorry. I didn't wanted to resurrect an old thread. I'm so sorry.
I'm trying to learn java, so I was going through this forum, and saw the thread. Didn't realize it was THAT old. I feel like a crap. and couldn't even get the effin result right T_T
Argh! no ways.. I thought I was a recent one, I saw it in the display of similar threads -_-' Sorry. I didn't wanted to resurrect an old thread. I'm so sorry.
I'm trying to learn java, so I was going through this forum, and saw the thread. Didn't realize it was THAT old. I feel like a crap. and couldn't even get the effin result right T_T
![]() |
Similar Threads
- GSL (newbie) problem. (Is it the code?) (C)
- Problem with AES encryption in J2ME (Java)
- small problem in the string (C++)
- Why this code cant work ???? (ASP.NET)
- Please help me find problem in code :) (C#)
- find out the error in my code (Java)
Other Threads in the Java Forum
- Previous Thread: basic PrintWriter question
- Next Thread: Making a table, help please
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying openjavafx pearl problem program programming project qt recursion repositories scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver state string superclass swing system thread threads tree variablebinding windows xor






