public class PGM2 { public static void main (String [] args){ try { Scanner scn = new Scanner(new File("/D:filename")); scn.useDelimiter("/n"); ArrayList<String> Stringarraylist = new ArrayList<String>(); while (scn.hasNext()){ Stringarraylist.add((scn.next())); } ArrayList<Integer> numbers = new ArrayList<Integer>(); for (String s : Stringarraylist){ numbers.add (Integer.parseInt(s)); } //numbers.add((Stringarraylist.get(i))); System.out.println("the given list is :"+ numbers); scn.close();`enter code here` } catch (Exception e) { System.out.println("the exception found is "+ e); } } } My input file has some values I intend to use them later in the program. I have used an ArrayList to collect all those values but unable to store them directly into the ArrayList<Integer> hence tried to type cast but NumberNotFound exception is coming.
\n, not/n./nis incorrect, you should be using\n. Also theScannerclass has a function called 'hasNextInt()` andnextInt()methods you should look into.e.getMessage()- it will often provide a more helpful message about what is going wrong.