D
D
Den4_x2020-09-11 18:39:52
Java
Den4_x, 2020-09-11 18:39:52

Why doesn't the code (BufferedReader) work?

package com.company.Labs.lab_5.File;// Вывод чисел и строк в текстовый файл

import com.company.Labs.lab_5.Collection.ex1_2.A;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;

import static java.lang.Integer.*;

public class Main {
    public static void main(String[] args) throws Exception {

        File file = new File("C:\\Users\\Даниил\\Desktop\\Java Projects\\JavaUniversity\\1\\src\\com\\company\\Labs\\lab_5\\File\\1.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));


        ArrayList<Product> arrayList = new ArrayList<>();
        int position = 0;
        String line;

        while ((line = reader.readLine()) != null) {

            Product product = new Product();

            String[] str_array = line.split("/");

            product.setId(Integer.parseInt((str_array[0])));  // 28 строка с якобы ошибкой 
            product.setName(str_array[1]);
            product.setPrice(Double.parseDouble(str_array[2]));

            arrayList.add(position, product);

            position++;

        }

        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i).getId() + "/" + arrayList.get(i).getName() + "/" + arrayList.get(i).getPrice());
        }

        reader.close();
    }
}


writes an error, although everything is logically correct ...

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt( Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at com.company.Labs.lab_5.File.Main.main(Main.java:28)

Process finished with exit code 1

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question