前提・実現したいこと
Javaにおいて、逆ポーランド記法のコードを書いています。
発生している問題・エラーメッセージ
Exception in thread "main" java.lang.NumberFormatException: empty String at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.base/java.lang.Double.parseDouble(Double.java:543) at rensyu1/rensyu4.Calculator.getAnswer(Calculator.java:52) at rensyu1/rensyu4.CalculatorMain.main(CalculatorMain.java:12)
該当のソースコード
Java
1public class Stack { 2 private static final int DEFAULT_INIT_SIZE = 16; 3 4 private int capacity; 5 private int size; 6 private String[] dataSet; 7 8 public Stack() { 9 this(DEFAULT_INIT_SIZE); 10 } 11 12 public Stack(int initSize) { 13 capacity = initSize; 14 size = 0; 15 dataSet = new String[capacity]; 16 } 17 18 public boolean isEmpty() { 19 if (size == 0) { 20 return true; 21 } 22 else { 23 return false; 24 } 25 } 26 27 public void push(String s) { 28 if (size < capacity) { 29 for (int i = capacity - 1; i > 0; i--) 30 dataSet[i] = dataSet[i - 1]; 31 dataSet[0] = s; 32 size = size + 1; 33 } 34 else if (size == capacity) { 35 doubleCapacity(); 36 for (int i = capacity - 1; i > 0; i--) 37 dataSet[i] = dataSet[i - 1]; 38 dataSet[0] = s; 39 size = size + 1; 40 } 41 } 42 43 public String pop() { 44 if (size == 0) { 45 return ""; 46 } 47 else { 48 String top = dataSet[0]; 49 for (int i = 0; i < capacity -1; i++) 50 dataSet[i] = dataSet[i + 1]; 51 dataSet[capacity - 1] = null; 52 size = size - 1; 53 return top; 54 } 55 } 56 57 public String peek() { 58 if (size == 0) { 59 return ""; 60 } 61 else { 62 return dataSet[0]; 63 } 64 } 65 66 private void doubleCapacity() { 67 System.out.println("Capacity: "+capacity+"->"+2*capacity); 68 String[] datakeep = new String[capacity]; 69 for (int i = 0; i < capacity; i++) 70 datakeep[i] = dataSet[i]; 71 capacity = 2 * capacity; 72 dataSet = new String[capacity]; 73 for (int i = 0; i < capacity / 2; i++) 74 dataSet[i] = datakeep[i]; 75 } 76} 77 78class Calculator { 79 private boolean isOperator (String token) { 80 if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) { 81 return true; 82 } 83 else { 84 return false; 85 } 86 } 87 88 private boolean isNumber (String token) { 89 if (token == "0" || token == "1" || token == "2" || token == "3" || token == "4" || token == "5" || token == "6" || token == "7" || token == "8" || token == "9") { 90 return true; 91 } 92 else { 93 return false; 94 } 95 } 96 public double getAnswer (String equation) { 97 int count = equation.length(); 98 int initSize = 4; 99 Stack stack = new Stack(initSize); 100 String a; 101 String b; 102 double x = 0; 103 double y = 0; 104 for (int i = 0; i < count; i++) { 105 String s = equation.substring(i, i + 1); 106 if (isOperator(s)) { 107 a = stack.pop(); 108 b = stack.pop(); 109 x = Double.parseDouble(a); 110 y = Double.parseDouble(b); 111 switch (s) { 112 case "+": x = x + y; break; 113 case "-": x = y - x; break; 114 case "*": x = x * y; break; 115 case "/": x = y / x; break; 116 } 117 a = String.valueOf(x); 118 stack.push(a); 119 } 120 else if (isNumber(s)) { 121 stack.push(s); 122 } 123 } 124 a = stack.pop(); 125 x = Double.parseDouble(a); 126 return x; 127 } 128} 129 130import java.util.Scanner; 131 132public class CalculatorMain { 133 public static void main(String[] args) { 134 Scanner stdIn = new Scanner(System.in); 135 System.out.println("算術式を逆ポーランド記法で入力してください。"); 136 String equation = stdIn.next(); 137 stdIn.close(); 138 Calculator calculator =new Calculator(); 139 double answer = calculator.getAnswer(equation); 140 System.out.println("Answer = "+ answer); 141 } 142}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/24 17:20