
import java.util.Scanner;
class Even {
static int getEven(int firstno,int secondno)
{
int counteven=0;
for(int i=firstno;i<=secondno;i++)
{
if(i%2==0)
{
counteven++;
}
}
return counteven;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("enter the two numbers::");
int fno=sc.nextInt();
int sno=sc.nextInt();
int noofeven=getEven(fno,sno);
System.out.println("no of even numbers is: "+noofeven);
System.out.println("continue:Y/N");
String str=sc.nextLine(); //i simply input Y
if(str.charAt(0)=='y') //this line cause an exception
break;
}
}
}
output:::::::::::::::::::::
java Even
java Even
Process started >>>
enter the two numbers:: 1
10
no of even numbers is: 5
continue:Y/N
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at Even.main(Even.java:27)
<<< Process finished. ================ READY ================