0

I use Android Studio.I have a text file with some of numbers and I want to calculate them with others numbers. When I am try to convert them from string with method Integer.parseInt on program start I get error and program close.Error is :

at java.lang.Integer.parseInt(Integer.java:521)
at java.lang.Integer.parseInt(Integer.java:556)

I am just beginner and sorry for bad english , I hope you understand my problem. This is part of my code.

public void read (){

        try {
            FileInputStream fileInput = openFileInput("example.txt");
            InputStreamReader reader = new InputStreamReader(fileInput);
            BufferedReader buffer = new BufferedReader(reader);
            StringBuffer strBuffer = new StringBuffer();
            String lines;
            while ((lines = buffer.readLine()) != null) {
                strBuffer.append(lines + "\n");


            }

       int numbers = Integer.parseInt(strBuffer.toString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Rusla
  • 1
  • 1
  • Can you post the file format example, please? – vincenzopalazzo Aug 25 '19 at 13:36
  • 3
    You're trying to parse the content of a whole file, containing several numbers (I guess), and line breaks, into a single Integer. That makes no sense. You probably want to parse each line into an Integer (assuming the file contains one number per line) – JB Nizet Aug 25 '19 at 13:39
  • 1
    `strBuffer` looks like a multiline string. `Integer.parseInt()` convert a string to a single integer value, not a formated list of numbers – jhamon Aug 25 '19 at 13:40
  • The text file with number 100 – Rusla Aug 25 '19 at 13:42

2 Answers2

1

Integer.parseInt(String) throws a NumberFormatException when its argument can't be converted to a number.

Break your problem into smaller, more manageable blocks. Your code currently gets the entire content of example.txt and tries to parse the whole thing to an Integer.

One possibility for reading all integer values is to do this with a java.util.Scanner object instead and use its nextInt() method.

Consider the following example, given a file example.txt with integers separated by spaces.

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class App {
    public static void main(String...args) throws Exception {
        File file = new File("/home/william/example.txt");
        try (InputStream is = Files.newInputStream(file.toPath())) {
            Scanner scanner = new Scanner(is);
            List<Integer> ints = new ArrayList<>();
            while (scanner.hasNextInt()) {
                int i = scanner.nextInt();
                System.out.printf("Read %d%n", i);
                ints.add(i);
            }
        }
    }
}
geco17
  • 5,152
  • 3
  • 21
  • 38
1

Here:

int numbers = Integer.parseInt(strBuffer.toString());

You should read the javadoc for the library methods you are using. parseInt() parses one number from a string that contains one number.

So you need

  • to learn how to use arrays of int (because you want to read and process multiple numbers), not just a single one
  • to then use parseInt() on the individual number strings in that file

Also note that you can use the Scanner to directly work on an InputStream, there is no need to first turn the complete file content into one large string in memory!

GhostCat
  • 137,827
  • 25
  • 176
  • 248