Народ, помогите разобраться, при подстановке пишет "Exception in thread "main" java.lang.NumberFormatException: For input string: "19847"", но не понятно какого рожна, в случае с "-d" парсит без проблем.
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/*
Прайсы 2
*/
public class Solution {
public static void main(String[] args) throws Exception {
//args = new String[] {"-u", "19847", "Ватные трусы с начёсом против шерсти", "234193.1234245", "953444"};
//args = new String[] {"-d", "198478"};
if(args.length==0){
return;
}
BufferedReader reader1 = new BufferedReader(new InputStreamReader(System.in));
List<Product> product = new ArrayList<>();
try {
String path = reader1.readLine();
reader1.close();
try(BufferedReader reader2 = new BufferedReader(new FileReader(path))){
while (reader2.ready()){
Product prod = getProducts(reader2.readLine());
product.add(prod);
}
}
switch(args[0]){
case "-u" :
int id = Integer.parseInt(args[1].substring(0,8).trim());
String productName = "";
for (int i = 1; i < args.length-2; i++){
productName +=args[i];
}
if (productName.length()>30){
productName.substring(0,30);
}
String price = "";
for (int i = 1; i < args.length-1; i++){
price +=args[i];
}
if (price.length()>8){
price.substring(0,8);
}
String quantity = args[args.length-1];
if (quantity.length()>4){
quantity.substring(0,4);
}
Product nP = new Product(id,productName,price,quantity);
for(int i=0; i<product.size(); i++){
if(product.get(i).id == id){
product.set(i,nP);
}
}
case "-d" :
int id2 = Integer.parseInt(args[1]);
for(int i = 0; i<product.size(); i++){
if((product.get(i).id)==id2){
product.remove(i);
}
}
}
FileWriter fW = new FileWriter(path);
for(Product p : product){
fW.write(p.toString());
fW.write("\n");
}
fW.close();
}
catch (IOException e){
e.printStackTrace();
}
}
public static Product getProducts(String s){
String id= s.substring(0,8).trim();
String productName = s.substring(8,38).trim();
String price = s.substring(38,46).trim();
String quantity = s.substring(46,50).trim();
return new Product(Integer.parseInt(id), productName, price, quantity);
}
public static class Product{
int id;
String productName;
String price;
String quantity;
public Product(int id, String productName, String price, String quantity){
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
@Override
public String toString() {
return String.format("%-8d%-30s%-8s%-4s",id,productName,price,quantity);
}
}
}