package com.codegym.task.task09.task0921;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.io.*;
/*
Method in a try-catch
*/
public class Solution {
public static void main(String[] args) {
readData();
}
public static void readData() {
//write your code here
Scanner sc = new Scanner(System.in);
ArrayList<Integer> arl = new ArrayList<Integer>();
try {
while (sc.hasNextInt() ) {
int i = sc.nextInt();
// System.out.println(i);
arl.add(i);
}
}catch (NumberFormatException e) {
for (int i=0; i <= arl.size(); i++) {
System.out.println(arl.get(i));
}
}
}
}
The code is not going into catch though I was able to complete the task by keeping the print order code in the try block itself (I know it's not correct). Please help me with the right solution
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Miki
11 September 2019, 20:50
im not sure what this program must do for the task. I see that you have problem in for loop and it gives indexoutofbounds exception for sure. I would use finally block after catch with this for loop and then it seems to work. I could not get this NumberFormatException by testing and changing your code. Im not that far in the course yet, hopefully it's explained.
0
Ash Ka
12 September 2019, 17:14
Thank you.. I guess using 'Finally' would be the right way..
0