前提・実現したいこと
・前提
機能としては、主に情報の入力と情報の表示の2つです。
メニュー1では、codeとnameを入力させて、Informationをnewし、mapに入れて、ファイルに書き込んでいます。
メニュー2では、ファイルから読み取ったcode,nameを表示、それに対応するnumberを入力させ、mapに入れ、ファイルに書き込んでいます。
メニュー3ではファイルを読み込み、表示させています。
メニュー1では、numberを入力しないのでnullをセットしているのですが、一度終了させて、
メニュー3を実行しようとすると、nullを読み込めずエラーが出てしまいます。
・実現したいこと
numberを入力していない場合は、「未入力」と表示させたいです。
発生している問題・エラーメッセージ
Exception in thread "main" java.lang.ExceptionInInitializerError at Main.main(Main.java:26) Caused by: java.lang.NumberFormatException: Character n is neither a decimal digit number, decimal point, nor "e" notation exponential mark. at java.base/java.math.BigDecimal.<init>(BigDecimal.java:511) at java.base/java.math.BigDecimal.<init>(BigDecimal.java:394) at java.base/java.math.BigDecimal.<init>(BigDecimal.java:827) at Filer.getInventory(Filer.java:24) at EntryData.<clinit>(EntryData.java:19) ... 1 more
該当のソースコード
java
import java.math.BigDecimal; public class Information { private String code; private String name; private BigDecimal number ; public Information(String code, String name, BigDecimal number){ this.code = code; this.name = name; this.number = number; } public String getCode() { return this.code; } public String getName() { return this.name; } public BigDecimal getNumber() { return this.number; } public void setCode(String code) { this.code = code; } public void setName(String Name) { this.name = name; } public void setNumber(BigDecimal number) { this.number = number; } }
java
import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; class Main { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); boolean end = false; while (!end) { try { System.out.println(""); System.out.println("<<メニュー>>"); System.out.println("1:code,name入力 \n2:number入力 \n3:表示 \n4:終了"); System.out.print(">"); int choice = Integer.parseInt(input.readLine()); System.out.println(""); switch (choice) { case 1: EntryData.enter(); break; case 2: EntryData.input(); break; case 3: File.show(); break; case 4: end = true; break; } } catch (IOException e) { System.out.println("指定された値を入力してください。"); } catch (NumberFormatException e) { System.out.println("数字を入力してください。"); } } } }
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; class EntryData { public static HashMap<String, Information> stockMap = Filer.getInventory(); public static void enter() throws IOException { boolean finish = false; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("codeを入力してください\n>"); String code = input.readLine(); System.out.print("nameを入力してください\n>"); String name = input.readLine(); Information information = new Information(code, name, null); stockMap.put(information.getCode(),information); filewrite(); } public static void input() throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); for (String key : stockMap.keySet()) { System.out.println("code:" + key); System.out.println("name:" + stockMap.get(key).getName()); System.out.print("■numberを入力してください\n>"); BigDecimal number = new BigDecimal(input.readLine()); stockMap.get(key).setNumber(number); } filewrite(); } public static void filewrite() { try (PrintWriter dataFile = new PrintWriter(new BufferedWriter (new OutputStreamWriter(new FileOutputStream("StockData.csv"), "UTF-8")))) { for(Map.Entry<String, Information> stock : stockMap.entrySet()) { dataFile.println(stock.getValue().getCode() + "," + stock.getValue().getName() + "," + stock.getValue().getNumber()); } } catch (FileNotFoundException e) { System.out.println("ファイルがみつかりませんでした。確認してください。"); return; } catch (IOException e) { System.out.println("指定された値を入力しているか確認してください。"); return; } } }
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; public class Filer { public static HashMap<String, Information> getInventory() { HashMap<String, Information> map = new HashMap<String, Information>(); String text; try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("StockData.csv"), "UTF-8"))) { while((text = br.readLine()) != null) { String[] st = text.split(",",-1); BigDecimal st2 = new BigDecimal(st[2]); Information information = new Information(st[0], st[1], st2); map.put(information.getCode(),information); } } catch (FileNotFoundException e) { System.out.println("指定されたファイルが見つかりません。"); } catch (IOException e) { System.out.println("指定された値を入力しているか確認してください。"); } return map; } public static void show() { String line; try (BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("StockData.csv"), "UTF-8"))) { while ((line = file.readLine()) != null) { String[] data = line.split(",", 0); for (String elem : data) { System.out.println(elem); } } } catch (FileNotFoundException e) { System.out.println("指定されたファイルが存在するか確認してください。"); } catch (IOException e) { System.out.println("値が入力されているか確認してください。"); } } }
試したこと
メニュー1でnull以外をセットしようとしましたが、エラーが出ます。
また、numberはBigDecimal型なので「未入力」という文字列はセットできず、手詰まりの状態です。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
まだ回答がついていません
会員登録して回答してみよう