Input:
First line contains an integer denoting the test cases 'T'. T testcases follow. Each testcase contains two lines of input. First line contains N the size of the array A. The second line contains the elements of the array.
Example:
Input:
2
3
3 2 1
4
1 2 3 4
Output:
6
10
Am trying to read the input with the help of console class and then convert the String
object to primitive type, using Integer.parseInt()
code:
import java.io.Console;
public class School {
public static void main(String[] args){
Console c = System.console();
// to run for the 'T' cases
int t = Integer.parseInt(c.readLine());
for(int a = 0; a<t;a++){
int size = Integer.parseInt(c.readLine());
int [] array = new int[size];
String str = c.readLine();
String [] nums = str.split(" ");
// to convert String array into int arrary
for(int i = 0; i<array.length;i++){
array[i]=Integer.parseInt(nums[i]);
}// end of inner for
int sum = 0;
// for sum
for(int i=0;i<array.length;i++){
sum+=array[i];
}// end of inner for
System.out.println(sum);
}// end of outer for
}// end of main
}// end of school
output:
Exception in thread "main" java.lang.NullPointerException
at linkedList/gfg.School.main(School.java:9)
Is it that c.readLine()
isn't able to take the input?