Доброго времени суток, друзья!
Подскажите, пожалуйста, с чем связана ошибка ниже: параметры не правильно указываю, форматирую не верно или вообще что то иное?
Возникает после метода getProduct()
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 46, end 50, length 48package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
//напишите тут ваш код
String fileName = new BufferedReader(new InputStreamReader(System.in)).readLine();
ArrayList<Product> priceList = new ArrayList<>();
if (args.length > 0 && args[0].equals("-u") || args[0].equals("-d")) {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while (reader.ready()) {
Product product = getProduct(reader.readLine());
priceList.add(product);
switch (args[0]) {
case "-u" :
for (Product s : priceList) {
if(s.id == Integer.parseInt(args[1])) {
s.id = Integer.parseInt(args[1]);
s.name = args[2];
s.price = args[3];
s.quantity = args[4];
}
}
case "-d" :
priceList.removeIf(prod -> prod.id == Integer.parseInt(args[1]));
}
}
reader.close();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));
for (Product s : priceList) {
writer.write("\n" + s);
}
writer.close();
}
public static class Product {
int id;
String name;
String price;
String quantity;
public Product(int id, String name, String price, String quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
@Override
public String toString() {
return String.format("%-8d%-30s%-8s%-4s", id, name, price, quantity);
}
}
public static Product getProduct(String string) {
String id = string.substring(0, 8).trim();
String name = string.substring(8, 38).trim();
String price = string.substring(38, 46).trim();
String quantity = string.substring(46, 50).trim();
return new Product(Integer.parseInt(id), name, price, quantity);
}
}