Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners StringIndexOutOfBound error !!! plz give me the reason for this error ,plz.........

Have Programming Question? Ask it here!
 
 
 


narendra choudhary
StringIndexOutOfBound error !!! plz give me the reason for this error ,plz.........
0 Answer(s)      2 years ago
Posted in : Java Beginners


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 ================


Advertisement
View Answers

Related Tutorials/Questions & Answers:
 

 

 

DMCA.com