public class Hello{ public static void main(String args[]) { int num = 190; String rev_num = " " ; while (num!=0){ int digit = num%10; rev_num = rev_num+digit; num = num /= 10 ; } System.out.println((Integer.parseInt(rev_num)));} } Exception in thread "main" java.lang.NumberFormatException: For input string: " 091" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:654) at java.base/java.lang.Integer.parseInt(Integer.java:786)
Question
public class Hello{ public static void main(String args[]) { int num = 190; String rev_num = " " ; while (num!=0){ int digit = num%10; rev_num = rev_num+digit; num = num /= 10 ; } System.out.println((Integer.parseInt(rev_num)));} } Exception in thread "main" java.lang.NumberFormatException: For input string: " 091" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:654) at java.base/java.lang.Integer.parseInt(Integer.java:786)
Solution
The error you're seeing is a NumberFormatException. This exception is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
In your case, the error is occurring because you're trying to parse the string " 091" to an integer using the Integer.parseInt() method. However, this string starts with a space, which is not a valid character for a number.
Here's how you can fix the issue:
-
Initialize the
rev_numvariable as an empty string ("") instead of a string with a space (" "). -
Remove the extra assignment in the line
num = num /= 10;. It should benum /= 10;.
Here's the corrected code:
public class Hello {
public static void main(String args[]) {
int num = 190;
String rev_num = "";
while (num != 0) {
int digit = num % 10;
rev_num = rev_num + digit;
num /= 10;
}
System.out.println(Integer.parseInt(rev_num));
}
}
With these corrections, the code should run without errors and print the reversed number as output.
Similar Questions
Kevin is trying to solve a number puzzle. If the number is greater than fifty, he wants the difference between the digits of the number; otherwise, if the number is less than or equal to fifty, he wants to reverse the number and find the difference in digits. Help Kevin to solve the problem using a Java program.Requirements:The input number should be a two-digit integer, or else display "Invalid number".Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input/Output 1:Enter the number56-1Explanation:56 is greater than 50, so subtract 5 - 6 = -1Sample Input/Output 2:Enter the number484Explanation:48 is less than 50, so reverse 48 to 84 then subtract 8 - 4 = 4Sample Input/Output 3:Enter the number9Invalid number
public class Main { public static void main(String args[]) { blank }}Drag and drop the correct code which will cause NumberFormatException when the above code is compiled.int x = Integer.parseInt("5.6"); double pi=Double.parseDouble("3.14"); boolean status=Boolean.parseBoolean("TRUE");
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0) { String[] np = br.readLine().split(" "); int n = Integer.parseInt(np[0]); int p = Integer.parseInt(np[1]); String[] arr = br.readLine().split(" "); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = Integer.parseInt(arr[i]); } Arrays.sort(a); int[] prefix = new int[n]; prefix[0] = a[0]; for(int i = 1; i < n; i++) { prefix[i] = a[i] + prefix[i - 1]; } int ans = a[p - 1] * p - prefix[p - 1]; for(int i = p; i < n; i++) { ans = Math.min(ans, a[i] * p - (prefix[i] - prefix[i - p])); } System.out.println(ans); } } }
Write the output of the below code(1 Marks)public static void main(String[] args) { double num = 9032; double val = 0.5; int i = 5; do { num *= val; i *= 10; } while ( i <= 9032 ); System.out.println(num); }
public class TestNumbers{ public static void main(String[] args) { // Get a random number from -10 up to 10. int number = (int) (Math.random() * 21) - 10; System.out.println("The number is " + number); // is it positive? if (number > 0) { System.out.println(number + " is positive!"); } // is it 0? if (number == 0) { System.out.println(number + " is zero!"); } // is it negative? // Add another if statement }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.