How do I convert a BufferedReader-Input (String) to Integer and save it in an Integer List in java?

Valeroschka

I would like to search for an Integer in a String given by a BufferedReader. The Integers have to be saved inside an Integer-List and be returned. My Idea was splitting the String in a String [ ] and save the Integers with Integer.parseInt directly inside the Array-List, but unfortunatelly i only get NumberFormatExceptions, although the String [ ] is filled. Could someone give me some advice?

    public List<Integer> getIntList(BufferedReader br) {
        List <Integer> List = new ArrayList<>();
        try{
            while(br.ready()){
                try{
                    String line = (br.readLine());
                    String [] arr = line.split("\\s");
                    for (String s : arr) {
                        System.out.println(s);
                    }
                    if(line.equals("end")){
                        return List;
                    }
                    for (String s : arr) {
                        List.add(Integer.parseInt(s));

                    }
                }
                catch(IOException e){
                    System.err.println("IOException");
                }
                catch(NumberFormatException e){
                    System.out.println("Number");
                }
            }
            return List;
        }
        catch(IOException e){
            System.err.println("IOException");
        }
    return null;
    }
Dmitry Pukhov

You catch NumberFormatException in a wrong place so that you cannot continue number searching loop. You have to wrap this line List.add(Integer.parseInt(s)); into try catch block. Also never start variable name with capital letter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java: Convert List<Integer> to String

Java: Convert List<Integer> to String

How do i check the input is an integer in java

Can I convert a Map<String, List<Integer>> into a MultiValueMap<String, Integer>

How do I convert from an integer to a string?

How Do I Convert an Integer to a String in Excel VBA?

How do I convert an integer to string as part of a PostgreSQL query?

How do I convert a String to an integer in Rust?

How do I convert an integer to a list of bits in Python

In spark MLlib, How do i convert string to integer in spark scala?

How do I convert (or cast) a String value to an Integer value?

How to convert input list into an integer list?

How do I convert an integer to a list of indexes in Lua

Using Java 8 streams how do I convert Map<String, List<Something>> to Map<String, Integer>?

How do I convert a JSON string object value to an integer?

How do I convert a string of numbers with commas in it to an integer and add them?

How do I convert string to integer in this case?

Ansible - How to convert list of string to list of integer?

How to convert List<String> to List<Integer>?

How convert input String to Integer groovy

How to convert List of List String into List of List Integer in java

How do I convert an integer to string in CakePHP?

how to convert String into Integer in java

How can I convert my input String to an integer with a base conversion

Java 8 stream: how to Convert Map<String, List<Integer>> to Map<Integer, List<String>> using groupingBy(.)

How do I convert a string from an input into an integer? (Python 3.9)

How to convert a text string in an input function into an integer?

How Do I Convert Integer To String Without Using int()

How Do I Convert String to Integer Without Using int()

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive