Question: How do I remove the Exception in thread main java.lang.NumberFormatException: from the main output: Test 1 : failure Expected _ _ _ _ _ _

How do I remove the "Exception in thread "main" java.lang.NumberFormatException:" from the main output:
Test 1: failure Expected________________________________________________________________________ Enter sequence of integers, each followed by a space: Error: Invalid character 'x' found at index 0 in input stream. Received________________________________________________________________________ Enter sequence of integers, each followed by a space: Exception in thread "main" java.lang.NumberFormatException: Invalid character 'x' found at index 0 in input stream. at InversionCounter.readArrayFromStdin(InversionCounter.java:103) at InversionCounter.main(InversionCounter.java:130) Expected length: 117, received length: 290 Test 2: failure Expected________________________________________________________________________ Enter sequence of integers, each followed by a space: Error: Invalid character 'x' found at index 4 in input stream. Received________________________________________________________________________ Enter sequence of integers, each followed by a space: Exception in thread "main" java.lang.NumberFormatException: Invalid character 'x' found at index 4 in input stream. at InversionCounter.readArrayFromStdin(InversionCounter.java:103) at InversionCounter.main(InversionCounter.java:130)
private static int[] readArrayFromStdin() throws IOException,
NumberFormatException {
List intList = new LinkedList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
int value =0, index =0, ch;
boolean valueFound = false;
while ((ch = reader.read())!=-1){
if (ch >='0' && ch <='9'){
valueFound = true;
value = value *10+(ch -48);
} else if (ch ==''|| ch =='
'|| ch =='\r'){
if (valueFound){
intList.add(value);
value =0;
}
valueFound = false;
if (ch !=''){
break;
}
} else {
throw new NumberFormatException(
"Invalid character '"+(char)ch +
"' found at index "+ index +" in input stream.");
}
index++;
}
int[] array = new int[intList.size()];
Iterator iterator = intList.iterator();
index =0;
while (iterator.hasNext()){
array[index++]= iterator.next();
}
return array;
}
public static void main(String[] args) throws IOException, NumberFormatException {
if (args.length >1){
System.out.println("Usage: java InversionCounter [slow]");
System.exit(1);
}
if (args.length ==1 && !args[0].equals("slow")){
System.out.println("Error: Unrecognized option '"+ args[0]+"'.");
System.exit(1);
}
System.out.print("Enter sequence of integers, each followed by a space: ");
int[] array = readArrayFromStdin();;
if (array.length ==0){
System.out.println("Error: Sequence of integers not received.");
System.exit(1);
}
if (args.length ==1 && args[0].equals("slow")){
System.out.println("Number of inversions: "+ countInversionsSlow(array));
}
else {
System.out.println("Number of inversions: "+ countInversionsFast(array));
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!