0
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.

5
  • Well that mean your try to parse a String that does not represent a number. Commented Feb 2, 2015 at 21:07
  • 2
    Typo: The escape sequence for newline is \n, not /n. Commented Feb 2, 2015 at 21:08
  • 3
    What are the contents of your file? Also, if you are trying to split by lines, the delimiter /n is incorrect, you should be using \n. Also the Scanner class has a function called 'hasNextInt()` and nextInt() methods you should look into. Commented Feb 2, 2015 at 21:09
  • Have you tried printing out the Strings without parsing to verify that you're trying to parse the right thing? Commented Feb 2, 2015 at 21:12
  • Change your statement for printing the exception to print e.getMessage() - it will often provide a more helpful message about what is going wrong. Commented Feb 2, 2015 at 21:20

1 Answer 1

1

This will work.

  • variable names start from small letter.
  • you need to use /r and /n for new lines - it depends is you are using windows or linux.

Here is the working code:

 public static void main(String[] args) { Pattern p = Pattern.compile("-?[0-9]+"); try { Scanner scn = new Scanner(new File("C:/workspace/test.txt")); scn.useDelimiter("[\\r\\n]+"); ArrayList<Integer> numbers = new ArrayList<Integer>(); while (scn.hasNext()) { String s = scn.next().trim(); if (p.matcher(s).matches()) { // this will skip invalid lines numbers.add(Integer.parseInt(s)); } } System.out.println("the given list is :" + numbers); scn.close(); } catch (Exception e) { System.out.println("the exception found is " + e); } } 

And second solution, using better Scanner class. Scanner can find integers for you.

 public static void main(String[] args) { try { Scanner scn = new Scanner(new File("C:/workspace/test.txt")); ArrayList<Integer> numbers = new ArrayList<Integer>(); while (scn.hasNext()) { if(scn.hasNextInt()){ numbers.add(scn.nextInt()); } } System.out.println("the given list is :" + numbers); scn.close(); } catch (Exception e) { System.out.println("the exception found is " + e); } } } 
Sign up to request clarification or add additional context in comments.

4 Comments

I have removed few lines:) Meaning is the same.
Hi thanks for the reply ..but my input file has some negative integers as well ..will this work fine ?
for (int k =0; k < Stringarraylist.size();k++){ int temp = Integer.parseInt(Stringarraylist.get(k)); System.out.println("the input just passed is :"+temp);
i am getting numbernotfound exception there

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.