上記のサイトを参考にし、簡単なゲームを作っているのですが、例外が発生し、自分では対処できないと思いこちらで質問させていただきます。
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "judge"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at net.sougetu.third.CodeBreakerThird.actionPerformed(CodeBreakerThird.java:119) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6535) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6300) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4891) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Java
1package net.sougetu.third; 2 3import java.awt.BorderLayout; 4import java.awt.event.ActionEvent; 5import java.awt.event.ActionListener; 6 7import javax.swing.JButton; 8import javax.swing.JComboBox; 9import javax.swing.JFrame; 10import javax.swing.JOptionPane; 11import javax.swing.JPanel; 12import javax.swing.JScrollPane; 13import javax.swing.JTable; 14import javax.swing.ScrollPaneConstants; 15import javax.swing.table.DefaultTableModel; 16 17import net.sougetu.second.GameEngine; 18 19public class CodeBreakerThird extends JFrame implements ActionListener { 20 private JPanel panel; 21 private JPanel southPanel; 22 private JPanel centorPanel; 23 private JScrollPane sc; 24 private DefaultTableModel dtm; 25 private JTable resultTable; 26 private JComboBox[] inputBox = new JComboBox[3]; 27 private String[] selector = {"1", "2", "3", "4", "5", "6"}; 28 private JButton judge; 29 private GameEngine ge = new GameEngine(); 30 31 public CodeBreakerThird() { 32 panel = new JPanel(new BorderLayout()); 33 centorPanel = new JPanel(); 34 southPanel = new JPanel(); 35 dtm = new DefaultTableModel(); 36 dtm.addColumn("1"); 37 dtm.addColumn("2"); 38 dtm.addColumn("3"); 39 dtm.addColumn("HIT"); 40 dtm.addColumn("BLOW"); 41 42 resultTable = new JTable(dtm); 43 resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 44 sc = new JScrollPane(resultTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 45 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 46 centorPanel.add(sc); 47 48 for(int i=0; i<3; i++) { 49 inputBox[i] = new JComboBox(selector); 50 inputBox[i].setActionCommand("" + i); 51 inputBox[i].addActionListener(this); 52 southPanel.add(inputBox[i]); 53 } 54 judge = new JButton("JUDGEボタン");//テキスト付きのボタン生成 55 judge.setActionCommand("judge"); 56 judge.addActionListener(this); 57 southPanel.add(judge); 58 59 panel.add(centorPanel, BorderLayout.CENTER); 60 panel.add(southPanel, BorderLayout.SOUTH); 61 this.getContentPane().add(panel); 62 63 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 64 this.setBounds(100, 100, 400, 500); 65 this.pack(); 66 this.setTitle(ge.getTitle()); 67 this.setVisible(true); 68 69 JOptionPane.showMessageDialog(this, ge.getRule(), ge.getTitle(), JOptionPane.INFORMATION_MESSAGE); 70 71 ge.setInput(new int[] {1, 1, 1}); 72 } 73 74 public void actionPerformed(ActionEvent arg0) { 75 System.out.println(arg0.getActionCommand()); 76 if(arg0.getActionCommand().equalsIgnoreCase("judge")) { 77 78 boolean judge = ge.judge(); 79 80 int[] ans = ge.getAnswer(); 81 System.out.println("答え"); 82 for(int i=0; i<ans.length; i++) { 83 System.out.print(ans[i]); 84 } 85 System.out.println(); 86 System.out.println("入力"); 87 int[] input = ge.getInput(); 88 for(int i=0; i<input.length; i++) { 89 System.out.print(input[i]); 90 } 91 System.out.println(); 92 System.out.println("hit" + ge.getHit() + "blow" + ge.getBlow()); 93 94 Object[] row = new Object[5]; 95 for(int i=0; i<3; i++) { 96 row[i] = Integer.valueOf(input[i]); 97 } 98 row[3] = Integer.valueOf(ge.getHit()); 99 row[4] = Integer.valueOf(ge.getBlow()); 100 dtm.addRow(row); 101 if(judge) { 102 int res = JOptionPane.showConfirmDialog(this, "まだ続けますか?", "おめでとう", JOptionPane.YES_NO_OPTION); 103 if(res == JOptionPane.YES_OPTION) { 104 int count = dtm.getRowCount(); 105 for(int i=0; i<count; i++) { 106 dtm.removeRow(0); 107 } 108 ge.makeAnswers(); 109 ge.setInput(new int[] {1, 1, 1}); 110 ge.judge(); 111 112 for(int i=0; i<inputBox.length; i++) { 113 inputBox[i].setSelectedIndex(0); 114 } 115 }else { 116 System.exit(0); 117 } 118 }else { 119 int input2 = Integer.parseInt(arg0.getActionCommand()); 120 ge.inputAnswer(input2, (String) inputBox[input2].getSelectedItem()); 121 } 122 } 123 } 124 public static void main(String[] args) { 125 new CodeBreakerThird(); 126 } 127} 128
Java
1package net.sougetu.second; 2 3import java.util.Scanner; 4 5public class GameEngine { 6 Scanner std = new Scanner(System.in); 7 private String title = "***CoderBreaker***"; 8 private String rule = "隠された3つの数字を当てます。\n" 9 + "1つの数字は1から6の間です。\n" 10 + "3つの答えの中に同じ数字はありません。\n" 11 + "入力した数字の位置と数字があっていたらヒット\n" 12 + "数字だけあっていたらブローとします。\n" 13 + "全部当てたら終了です。"; 14 private int[] answer; 15 private int[] input; 16 private int numberOfAnswers = 3; 17 private int widthOfRandom = 6; 18 private int hit = 0; 19 private int blow = 0; 20 21 public GameEngine() { 22 answer = new int[numberOfAnswers]; 23 input = new int[numberOfAnswers]; 24 makeAnswers(); 25 } 26 27 public GameEngine(int numverOfAnswers) { 28 this.numberOfAnswers = numberOfAnswers; 29 answer = new int[numberOfAnswers]; 30 input = new int[numberOfAnswers]; 31 makeAnswers(); 32 } 33 34 public void makeAnswers() { 35 if(answer.length != numberOfAnswers) { 36 answer = new int[numberOfAnswers]; 37 input = new int[numberOfAnswers]; 38 } 39 for(int i=0; i<answer.length; i++) { 40 boolean flag = false; 41 answer[i] = (int)(Math.random() * 6 + 1); 42 do { 43 flag = false; 44 for(int j=i-1; j>=0; j--) { 45 if(answer[i] == input[j]) { 46 flag = true; 47 answer[i] = (int)(Math.random() * 6 + 1); 48 } 49 } 50 }while(flag == true); 51 } 52 } 53 54 public void inputAnswer(int index, int answer) { 55 if(index > -1 && index < numberOfAnswers) { 56 if(answer > 0 && answer <= widthOfRandom) { 57 input[index] = answer; 58 } 59 } 60 } 61 62 public void inputAnswer(int index, String string) { 63 int answer; 64 answer = Integer.parseInt(string); 65 inputAnswer(index, answer); 66 } 67 68 public boolean judge() { 69 hit = 0; 70 blow = 0; 71 for(int i=0; i<answer.length; i++) { 72 for(int j=0; j<answer.length; j++) { 73 if(i == j && answer[i] == input[j]) { 74 hit++; 75 }else if(answer[i] == input[j]) { 76 blow++; 77 } 78 } 79 } 80 return (hit == numberOfAnswers); 81 } 82 public int getNumberOfAnswers() { 83 return numberOfAnswers; 84 } 85 public int getWidthOfRandom() { 86 return widthOfRandom; 87 } 88 public int[] getAnswer() { 89 return answer; 90 } 91 public int[] getInput() { 92 return input; 93 } 94 public void setInput(int[] input) { 95 for(int i=0; i<input.length; i++) { 96 inputAnswer(i, input[i]); 97 } 98 } 99 public int getHit() { 100 return hit; 101 } 102 public int getBlow() { 103 return blow; 104 } 105 public String getRule() { 106 return rule; 107 } 108 public void setRule() { 109 this.rule = rule; 110 } 111 public String getTitle() { 112 return title; 113 } 114 public void setTitle() { 115 this.title = title; 116 } 117 118} 119
スタックトレースを見ると、CodeBreakerThirdクラスの119行目で問題が発生していると分かりました。原因としては、parseIntメソッドは文字列を10進数の整数に変換するものであるが、引数として与えているorg0.getActionCommand()の戻り値はString型で"judge"だったので、不一致が起きていると思われます。
ここからどのように訂正すれば良いのでしょうか?
分かりやすくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。