fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. class Data{
  6. String code;
  7. String name;
  8. Integer price;
  9.  
  10. int maxCodeLength;
  11. int maxNameLength;
  12. int maxPriceLength;
  13.  
  14. int zenkaku;
  15. int hankaku;
  16.  
  17. final String undefStr = "未設定";
  18. int undefStrLen;
  19.  
  20. Data(String code, String name, Integer price){
  21. this.code = code;
  22. this.name = name;
  23. this.price = price;
  24.  
  25. this.undefStrLen = method2(undefStr);
  26. }
  27. public int codeLength(){
  28. return price == null ? undefStrLen : method2(code);
  29. }
  30. public int nameLength(){
  31. return name == null ? undefStrLen : method2(name);
  32. }
  33. public int priceLength(){
  34. return price == null ? undefStrLen : price.toString().length();
  35. }
  36. public String toString(){
  37. String spaceCode = code == null ? getSpace(maxCodeLength, method2(undefStr)) : getSpace(maxCodeLength, method2(code));
  38. String spaceName = name == null ? getSpace(maxNameLength, method2(undefStr)) : getSpace(maxNameLength, method2(name));
  39. return code+spaceCode+" "+name+spaceName+" "+getStrInt(price, maxPriceLength);
  40. }
  41. private String getStrInt(Integer value, int max){
  42. final String fmt = "%"+max+"d";
  43. return price == null ? getSpace(undefStrLen, max)+undefStr : String.format(fmt, price);
  44. }
  45. private String getSpace(int max, int len){
  46. String space = "";
  47. for(int i=0; i<max-len; i++){
  48. space += " ";
  49. }
  50. return space;
  51. }
  52. public void setMaxCodeLength(int value){
  53. maxCodeLength = value;
  54. }
  55. public void setMaxNameLength(int value){
  56. maxNameLength = value;
  57. }
  58. public void setMaxPriceLength(int value){
  59. maxPriceLength = value;
  60. }
  61. public int method2(String s) {//コマンドライン上での文字列の見映えの長さ。コマンドライン上で見えている文字列の長さ。見えている長さ。見た時の長さ。
  62. // Javaで全角・半角の判定 - のらくら備忘録
  63. // http://k...content-available-to-author-only...2.com/blog-entry-111.html
  64.  
  65. int length = 0;
  66. zenkaku = 0;
  67. hankaku = 0;
  68.  
  69. char[] chars = s.toCharArray();
  70. for (int i = 0; i < chars.length; i++) {
  71. char c = chars[i];
  72. if ((c <= '\u007e') || // 英数字
  73. (c == '\u00a5') || // \記号
  74. (c == '\u203e') || // ~記号
  75. (c >= '\uff61' && c <= '\uff9f') // 半角カナ
  76. ) {
  77. //System.out.print("半");
  78. hankaku++;
  79. } else {
  80. //System.out.print("全");
  81. zenkaku++;
  82. }
  83. }
  84. return hankaku + zenkaku*2;
  85. }
  86. }
  87. class CsvOrm{
  88. public static void main(String[] args){
  89. String fileName = "商品一覧.csv";
  90. BufferedReader br = null;
  91. ArrayList<Data> data = new ArrayList<>();
  92.  
  93. try{
  94. File file = new File(fileName);
  95. br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
  96. String line;
  97. while((line = br.readLine()) != null){
  98. //System.out.println("check:"+line);//check, ok
  99. //ArrayList<String> temp = line.split(",");
  100. String[] temp = line.split(",");
  101. //System.out.println("len:"+temp.length);//check, ok
  102.  
  103. final Integer price = temp.length == 2 ? null : Integer.parseInt(temp[2]);//末尾のpriceが欠損してる場合。
  104. data.add(new Data(func(temp[0]), func(temp[1]), price));
  105. }
  106. }catch(Exception e){
  107. System.out.println(e.getMessage());
  108. }finally{
  109. try{
  110. br.close();
  111. }catch (Exception e){
  112. System.out.println(e.getMessage());
  113. }
  114. }
  115.  
  116. adjust(data);
  117. show(data);
  118. }
  119. public static void adjust(ArrayList<Data> data){
  120. final Comparator<Data> cmpCodeLen = (s1, s2) -> Integer.compare(s1.codeLength(), s2.codeLength());
  121. final Comparator<Data> cmpNameLen = (s1, s2) -> Integer.compare(s1.nameLength(), s2.nameLength());
  122. final Comparator<Data> cmpPriceLen = (s1, s2) -> Integer.compare(s1.priceLength(), s2.priceLength());
  123.  
  124. Optional<Data> maxCodeData = data.stream().max(cmpCodeLen);
  125. Optional<Data> maxNameData = data.stream().max(cmpNameLen);
  126. Optional<Data> maxPriceData = data.stream().max(cmpPriceLen);
  127.  
  128. final int maxCodeLength = maxCodeData.get().codeLength();
  129. final int maxNameLength = maxNameData.get().nameLength();
  130. final int maxPriceLength = maxCodeData.get().priceLength();
  131.  
  132. System.out.println("maxCodeLength :"+maxCodeLength);
  133. System.out.println("maxNameLength :"+maxNameLength);
  134. System.out.println("maxPriceLength:"+maxPriceLength);
  135.  
  136. for(Data d: data){
  137. d.setMaxCodeLength(maxCodeLength);
  138. d.setMaxNameLength(maxNameLength);
  139. d.setMaxPriceLength(maxPriceLength);
  140. }
  141. }
  142. public static void show(ArrayList<Data> data){
  143. for(Data d: data){
  144. System.out.println(d);
  145. }
  146. }
  147. public static String func(String data){
  148. if(data.isEmpty()){
  149. return "未設定";
  150. }
  151. if(data == null){
  152. return "null";
  153. }
  154. return data;
  155. }
  156. }
  157.  
Runtime error #stdin #stdout #stderr 0.1s 48096KB
stdin
Standard input is empty
stdout
商品一覧.csv (No such file or directory)
null
stderr
Exception in thread "main" java.util.NoSuchElementException: No value present
	at java.base/java.util.Optional.get(Optional.java:148)
	at CsvOrm.adjust(Main.java:128)
	at CsvOrm.main(Main.java:116)