Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Binary Search Loop

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary Search Loop

    Hi there,

    Im having problems with my netbeans java programming code for binary search. The problem is that, it keeps repeating the same sentence in the output and i cant figure out whats wrong with it. Anyone able to solve this without changing the program? No exta classes like break, return, true, false etc..

    import java.util.Scanner;
    public class binarys {
    public static void main(String[] args)
    {
    Scanner reader = new Scanner(System.in);
    int [] array = new int[10];
    int search, index;

    for (index=0;index<10;index++)
    {
    System.out.println("Enter numbers to array:");
    array[index] = reader.nextInt();
    }

    System.out.println("Enter number to search:");
    search = reader.nextInt();

    int left = 0;
    int right = array.length - 1;

    while (left<=right)
    {
    int mid = (left+right)/2;

    if (array[mid] == search)
    {
    System.out.println("The number is on index "+mid);
    }
    else if (array[mid] < search)
    {
    left = mid + 1;
    System.out.println("The number is on index "+left);
    }

    if (array[mid] > search)
    {
    right = mid - 1;
    System.out.println("The numer is on index "+right);
    }
    }
    }
    }
    Last edited by Tarakara; December 29th, 2010 at 07:43 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Binary Search Loop

    Quote Originally Posted by Tarakara View Post
    Im having problems with my netbeans java programming code for binary search. The problem is that, it keeps repeating the same sentence in the output and i cant figure out whats wrong with it. Anyone able to solve this without changing the program?
    What is the output? For that matter, what is the input (is it already sorted)? What is the expected output? What do you expect to happen? What happens instead?

    Quote Originally Posted by Tarakara View Post
    No exta classes like break, return, true, false etc..
    Why not? And those are not classes.

    See my signature on using code tags. Nobody wants to read unformatted code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Binary Search Tree in Java [HELP]
    By alan in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 5th, 2011, 06:44 AM
  2. Need help fixing Binary Search Tree code
    By fistpunch in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2010, 11:22 AM
  3. Polymorphic Binary Search Tree Question
    By MJS139115 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 9th, 2010, 06:08 PM
  4. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM
  5. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 12:23 AM