Exception in thread “main” java.lang.NumberFormatException: For input string: “”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at Main.main(Main.java:10)
What is this error
This is caused when you try to convert integer to string or vice-versa. Can you please share your code as well? I will be able to help you better
import java.util.; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int j=0; int decimal=0; int i=1; while(i<=N){ String s = sc.nextLine(); //int j=1; while(N!=NULL) int a= s%10; decimal=decimal+aMath.pow(2,j); s=s/10; }System.out.println(s); } } }
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int i = 1; while(i<=N){ String s = sc.nextLine(); int decimal = Integer.parseInt(s,2); System.out.println(decimal); } } }
@mishikasrvastava,
Your second approach is incorrect and we won’t be using that anywhere. It is suggested that you use your own method for conversion to decimal.
@mishikasrvastava,
For first approach:
https://ide.codingblocks.com/s/298637 corrected code.
Errors:
- int decimal and int j will be declared inside the for loop because we want them to reset after every iteration.
- Take input of binary numbers are long/int. If using string for that, you need to subtract the ASCII value of ‘0’ from every char to get the digit.
- Also, while using int we don’t use null, we use 0.
- i++ inside the while loop. Your loop was going to infinity!
thank you for th help