简体   繁体   中英

How to read multiple integers without symbols out of arraylist?

I need to read multiple integers which are written in the format (4, 5), 242 . There are 10 lines and each line differs from the other. For example the second line could be something like (11, 24, 5, 6, 2, 7), 43 .

I want to use these values in a method with the parameter ( int[] n, int x).I want to print the size of input of n for each line .

How can I do that?

The straight forward solution that you can apply while reading in your data from your filed:

  • treat each line as string
  • remove all characters but digits and commas

Now you should be left with strings like "2,3,4,5".

You can use the split() method on the String class to turn such a string into an array that contains the different numbers only (by splitting on ","). Finally, you parse the number strings. Meaning: you iterate the array of number strings and you use Integer.psrseInt() to turn each of that strings into an int or Integer value. Which you then can add to a new list (of Integer) for example!

As this is probably some sort of homework, the above is meant as inspiration, turning it into code is left as exercise to the reader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM