0

This is the program to show sum of series 1-(a^2/3!)+(a^4/5!)-(a^6/7!)+..... I used recursion show factorial of number

    import java.io.*;
    class Series{
       public static void main(String args[])throws IOException{
         int n,a;
         double sum=0;
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         String str;

         System.out.println("Enter the value of n & a");

         str=br.readLine();
         n=Integer.parseInt(str);
         str=br.readLine();
         a=Integer.parseInt(str);

         for(int i=0;i<=n;i++){
             sum=sum+(Math.pow(-1,i)*(Math.pow(a,2*i)/fact(2*i+1)));
         }
         System.out.println(sum);
      }

      static int fact(int n){
        int  fact=1,i;
        for(i=1;i<=n;i++){
           fact=fact*i;
        }
        return(fact);
      }
   }

output =

Exception in thread "main" java.lang.NumberFormatException: For input string: "3 6"    
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Series.main(Series.java:12)

Please help me out it is showing error in java programming Exception in thread "main" java.lang.NumberFormatException

2
  • 1
    you have entered 3 and 6 on same line and using method readLine() it both time reads "3 6" and throws number format exception because it cant parse "3 6" into an integer. if I have understood your problem right then this is the problem Commented Aug 17, 2014 at 14:16
  • "3 6" contains a space so java can't convert that to a number. That is why you are getting that error. You can replace all the spaces with "" and then try to convert that to the number Commented Aug 17, 2014 at 14:20

4 Answers 4

2
Integer.parseInt(str);

this is the erroneous line as str="3 6"(in your case) and it cannot be cast into an Integer.

Sign up to request clarification or add additional context in comments.

Comments

2

"3 6" contains a space so java can't convert that to a number. That is why you are getting that error. You can replace all the spaces with "" and then try to convert that to the number

String line = br.readLine().replaceAll("\\s+", "");
//if you only have spaces in either of side. then you can use trim()
int number = Integer.parseInt(line);

But the best way is to replace all non digit characters with ""

String line = br.readLine().replaceAll("\\D+", "");
int no = Integer.parseInt(line);

Comments

1

Try this:-

Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n & a");
n = sc.nextInt();
a = sc.nextInt();

Comments

0

readLine() will read characters until a newline is found.

In your case, try entering the value of n and a in newline

or Use a Scanner instead

Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
a = scanner.nextInt();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.