前提・実現したいこと
・前提
機能としては、主に情報の入力と情報の表示の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
1import java.math.BigDecimal; 2 3public class Information { 4 private String code; 5 private String name; 6 private BigDecimal number ; 7 8 public Information(String code, String name, BigDecimal number){ 9 this.code = code; 10 this.name = name; 11 this.number = number; 12 } 13 14 public String getCode() { 15 return this.code; 16 } 17 public String getName() { 18 return this.name; 19 } 20 public BigDecimal getNumber() { 21 return this.number; 22 } 23 24 public void setCode(String code) { 25 this.code = code; 26 } 27 public void setName(String Name) { 28 this.name = name; 29 } 30 public void setNumber(BigDecimal number) { 31 this.number = number; 32 } 33}
java
1import java.io.IOException; 2import java.io.InputStreamReader; 3import java.io.BufferedReader; 4 5class Main { 6 public static void main(String[] args) throws IOException { 7 8 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 9 boolean end = false; 10 11 while (!end) { 12 try { 13 System.out.println(""); 14 System.out.println("<<メニュー>>"); 15 System.out.println("1:code,name入力 \n2:number入力 \n3:表示 \n4:終了"); 16 System.out.print(">"); 17 int choice = Integer.parseInt(input.readLine()); 18 System.out.println(""); 19 switch (choice) { 20 case 1: 21 EntryData.enter(); 22 break; 23 24 case 2: 25 EntryData.input(); 26 break; 27 28 case 3: 29 File.show(); 30 break; 31 32 case 4: 33 end = true; 34 break; 35 } 36 } catch (IOException e) { 37 System.out.println("指定された値を入力してください。"); 38 } catch (NumberFormatException e) { 39 System.out.println("数字を入力してください。"); 40 } 41 } 42 } 43}
java
1import java.io.BufferedReader; 2import java.io.BufferedWriter; 3import java.io.File; 4import java.io.FileInputStream; 5import java.io.FileNotFoundException; 6import java.io.FileOutputStream; 7import java.io.IOException; 8import java.io.InputStreamReader; 9import java.io.OutputStreamWriter; 10import java.io.PrintWriter; 11import java.util.ArrayList; 12import java.math.BigDecimal; 13import java.util.HashMap; 14import java.util.Map; 15 16class EntryData { 17 18 public static HashMap<String, Information> stockMap = Filer.getInventory(); 19 20 public static void enter() throws IOException { 21 boolean finish = false; 22 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 23 24 System.out.print("codeを入力してください\n>"); 25 String code = input.readLine(); 26 System.out.print("nameを入力してください\n>"); 27 String name = input.readLine(); 28 29 Information information = new Information(code, name, null); 30 stockMap.put(information.getCode(),information); 31 filewrite(); 32 33 } 34 35 public static void input() throws IOException { 36 37 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 38 for (String key : stockMap.keySet()) { 39 System.out.println("code:" + key); 40 System.out.println("name:" + stockMap.get(key).getName()); 41 System.out.print("■numberを入力してください\n>"); 42 BigDecimal number = new BigDecimal(input.readLine()); 43 44 stockMap.get(key).setNumber(number); 45 } 46 filewrite(); 47 } 48 49 public static void filewrite() { 50 51 try (PrintWriter dataFile = new PrintWriter(new BufferedWriter 52 (new OutputStreamWriter(new FileOutputStream("StockData.csv"), "UTF-8")))) { 53 54 for(Map.Entry<String, Information> stock : stockMap.entrySet()) { 55 dataFile.println(stock.getValue().getCode() + "," + stock.getValue().getName() + 56 "," + stock.getValue().getNumber()); 57 } 58 } catch (FileNotFoundException e) { 59 System.out.println("ファイルがみつかりませんでした。確認してください。"); 60 return; 61 } catch (IOException e) { 62 System.out.println("指定された値を入力しているか確認してください。"); 63 return; 64 } 65 } 66}
java
1import java.io.BufferedReader; 2import java.io.BufferedWriter; 3import java.io.FileInputStream; 4import java.io.FileNotFoundException; 5import java.io.FileOutputStream; 6import java.io.IOException; 7import java.io.InputStreamReader; 8import java.io.OutputStreamWriter; 9import java.io.PrintWriter; 10import java.math.BigDecimal; 11import java.util.HashMap; 12import java.util.Map; 13 14public class Filer { 15 16 public static HashMap<String, Information> getInventory() { 17 18 HashMap<String, Information> map = new HashMap<String, Information>(); 19 String text; 20 21 try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("StockData.csv"), "UTF-8"))) { 22 while((text = br.readLine()) != null) { 23 String[] st = text.split(",",-1); 24 BigDecimal st2 = new BigDecimal(st[2]); 25 Information information = new Information(st[0], st[1], st2); 26 map.put(information.getCode(),information); 27 } 28 } catch (FileNotFoundException e) { 29 System.out.println("指定されたファイルが見つかりません。"); 30 } catch (IOException e) { 31 System.out.println("指定された値を入力しているか確認してください。"); 32 } 33 return map; 34 } 35 36 public static void show() { 37 String line; 38 try (BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("StockData.csv"), "UTF-8"))) { 39 while ((line = file.readLine()) != null) { 40 String[] data = line.split(",", 0); 41 for (String elem : data) { 42 System.out.println(elem); 43 } 44 } 45 } catch (FileNotFoundException e) { 46 System.out.println("指定されたファイルが存在するか確認してください。"); 47 } catch (IOException e) { 48 System.out.println("値が入力されているか確認してください。"); 49 } 50 } 51}
試したこと
メニュー1でnull以外をセットしようとしましたが、エラーが出ます。
また、numberはBigDecimal型なので「未入力」という文字列はセットできず、手詰まりの状態です。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー