473,811 Members | 3,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with print statement in java

1 New Member
I have written a binary search algorithm in java. I have a statement that when the search key is found it is printed to a terminal window. This statement when when called just keeps printing over and over and i cant figure out why :confused: . Can any one help?

Expand|Select|Wrap|Line Numbers
  1.  import java.util.Random; 
  2.  
  3.  
  4. public class BinarySearch
  5. {
  6. Random RandomGenerator; // declares the random generator that generates the array
  7. int maxLength; // initialises the varible max lenght
  8. private int[] a; // the array is declared
  9.  
  10. public BinarySearch() 
  11. RandomGenerator = new Random(); // create random generator
  12. maxLength = 100; // set the maximum value for max lenght
  13. }
  14.  
  15. public void BinarySearchFunction(int n) 
  16. {
  17. a = new int[n]; // create array
  18. for (int i=0; i<n; i++) 
  19. {
  20. a[i] = RandomGenerator.nextInt(maxLength); // new random numbers
  21. }
  22.  
  23. print(a);
  24.  
  25. {
  26. int in, out;
  27.  
  28. for(out=1; out < a.length; out++) // out is dividing line
  29. {
  30. int temp = a[out]; // remove marked item
  31. in = out; // start shifts at out
  32. while(in>0 && a[in-1] >= temp) // until one is smaller,
  33. {
  34. a[in] = a[in-1]; // shift item to right
  35. --in; // go left one position
  36. }
  37. a[in] = temp; // insert marked item
  38.  
  39.  
  40.  
  41. print(a); // prints sorted numbers
  42. }
  43. }
  44.  
  45.  
  46. public void BinarySearch(int key)
  47. {
  48. int low = 0;
  49. int high = a.length - 1; //zero based array
  50. int middle;
  51.  
  52. while( low <= high )
  53. {
  54. middle = ( low + high ) / 2;
  55.  
  56. if( key == a[ middle ] ) {//match
  57. System.out.println("Search key " + a[middle] + " was found at index: " + middle); }
  58.  
  59. else if( key < a[ middle ] ){
  60. high = middle - 1; //search low end of array
  61. }
  62. else{
  63. low = middle + 1; //search high end of array
  64. }
  65.  
  66. System.out.println("Search key not found"); //search key not found
  67. }
  68.  
  69.  
  70.  
  71.  
  72. public void print(int[] a)
  73. {
  74. for (int i = 0; i < a.length; i++)
  75. System.out.print(a[i] + " ");
  76. System.out.println();
  77. }
  78.  
Nov 24 '05 #1
0 4988

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2604
by: kaiwing18 | last post by:
Hi , I have a problem relate to java and database. Could anyone answer me ?Please see the following code. import java.sql.*; public class Result { public static void main(String args) {
4
9024
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
1
3140
by: Unebrion | last post by:
Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program. This part just explains a check digit.. i think this part in my program is alright These barcodes actually have four parts: a tall line as the first bar, a series of tall and short lines corresponding to the digits in the ZIP code, a check digit,...
1
2619
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc program is as follows import java.sql.*; import java.lang.*; import java.io.*; import java.util.*;
5
3562
by: koonda | last post by:
Hi all, I am a student and I have a project due 20th of this month, I mean May 20, 2007 after 8 days. The project is about creating a Connect Four Game. I have found some code examples on the internet which helped me little bit. But there are lot of problems I am facing developing the whole game. I have drawn the Board and the two players can play. The players numbers can fill the board, but I have problem implementing the winner for the...
6
2353
by: redashley40 | last post by:
This is my first attempt in SQL and PreparedStatement I have add the PreparedStatement and I'm not to sure if I'm doing it correctly. When I do a test run on Choose 1 ,or 2 I get this error. Error - com.mysql.jdbc.Statement here is my code below public class DBAssign { /**
6
1633
by: IvanJ2EE | last post by:
Hello partners. I have an issue using jdbc and a DB in MySQL. The problem is that I want to make a seach using the Last Name of a person stored in a MySQL DB, from a JSP The problem is that I cannot determine which sentence to use for this purpose, that can help me to display the information of the person who match with the search criteria. This is my actual Servlet. import java.io.IOException;
1
1235
by: tkempy | last post by:
okay so i have a CIS class called abstraction and design. i'm completely new to this "java" stuff. I've never seen any of it in my life. our second homework is to create a program that produces a "ASCII art" version of the lincoln financial field where the eagles play. anyone who can help???? These are all the guidelines and what not... Assignment Description Your second assignment will provide you with practice using static methods,...
0
1815
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner; public class project { public static void main( String args ) {
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.