0

Can't convert String[] to int[],Why?

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); String b=sc.nextLine(); String[]b1=b.split(" "); int[] arr = new int[b1.length]; for (int i = 0; i < b1.length; i++) { arr[i] = Integer.parseInt(b1[i]); } } }

24th Dec 2022, 1:50 PM
Killiam Chances
Killiam Chances - avatar
2 Respostas
+ 1
For a perfect input, the problem lies because of the "int a = sc.nextInt()" statement. By removing it, the program works nicely. As after the "nextInt()", the program is accepting a null string "" as the error displays.
24th Dec 2022, 2:24 PM
Prashant Narendra Solanki
Prashant Narendra Solanki - avatar
0
What input is being provided? If it's a sentence then the String won't convert to an Int.
24th Dec 2022, 2:08 PM
Rivan