15,882,315 members
See more:
```import java.io.*;
import java.util.Scanner;
class String2
{

public static void Vowels()
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the number of elements in the array :");
int n=scan.nextInt();
int a[]=new int[n];
System.out.println("Enter the elements of the array :");
for(int i=0;i<=n-1;i++)
{
a[i]=scan.nextInt();
}
System.out.println("Enter the element  be searched :");
int x=scan.nextInt();//here instead of the compiler allowing to enter only one value it is allowing to enter infinite number of values
int lb=0,ub=n-1;
int pos=0;
while(ub>0)
{
int mid=(lb+ub)/2;
if(a[mid]<x)
{
lb=mid-1;
}
if(a[mid]==x)
{
pos=mid+1;
}
if(a[mid]>x)
{
ub=mid+1;
}
}

if(pos==0)
{
}
else
{
System.out.println(x+"is found at the position :"+pos);
}
}
public static void main(String[]args)
{
Vowels();
}
}```

What I have tried:

The above code is what i have tried.
Posted
Updated 15-Jun-20 20:38pm

## Solution 2

As already pointed out by our Griff, your `while` loop never ends. So you're basically left with an empty console to play with.
That because your implementation of the binary search algorithm is wrong.
Try
C++
```while( lb+1 < ub )
{
int mid = (lb + ub)/2;

if( a[mid] < x)
{
lb = mid;
}
else if( a[mid] == x )
{
pos = mid + 1;
break;
}
else
{
ub = mid;
}
}
```

Member 14843380 16-Jun-20 3:31am
can you please tell me why is the compiler allowing to enter more than one value for x?
when i enter a value for x it allows to enter more values infinitely.x is the number in the above code which is to be searched.
CPallini 16-Jun-20 4:46am
The compiler is not allowing you to enter more than one value.
Howver, the program (or the OS, or whatever..) is allowing you to freely type in the dark. Try, for instance:
class Foo
{
public static void main( String args[])
{
try
{
for (;;)
}
catch ( InterruptedException ie )
{
System.out.println("Goodbye!");
}
}
}
jsc42 16-Jun-20 4:26am
You cannot enter an infinite number of numbers - it would take the whole of eternity to do it. Do you mean that you can type more numbers than you want? Does it find the solution for every number that you type or just the first one that you are searching for? My guess is that it will only search for the first one and the rest will be ignored. This is because you are just filling in data into a scan buffer and your program is consuming data from the scan buffer. Data not relevant to the program (e.g. your infinite set of numbers) just sit there waiting in the buffer in case any code wants to scan them. However, if you are entering multiple numbers and each is being searched in turn, please verify that the code that you have posted is what you are running and that there isn't another loop somewhere that is picking up the other numbers.

## Solution 1

At a guess, because `ub` is always greater than zero, so it never leaves your `while` loop.

But to be sure, you look at your code and variables while your app is running and find out exactly what it is doing.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!