Угадайте о матических проблемах
У меня возникла проблема с моей текстовой областью/полем make a guess. Я могу ввести все, что захочу, но когда я нажимаю enter, я получаю ошибку в консоли.
да. моя плохая ошибка заключается в том, что
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at java.lang.Integer.parseInt(Integer.java:615) at gom.GOM.newGuess(GOM.java:175) at gom.GOM.keyReleased(GOM.java:147) at java.awt.Component.processKeyEvent(Component.java:6494) at javax.swing.JComponent.processKeyEvent(JComponent.java:2832) at java.awt.Component.processEvent(Component.java:6310) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771) at java.awt.Component.dispatchEventImpl(Component.java:4760) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) 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:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) 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:80) 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)
вот и вся ошибка, которую я получаю
Что я уже пробовал:
package gom; /** * * @author stephenwessels */ import java.awt.*; import java.awt.event.*; import java.util.Random; import java.util.Scanner; import javax.swing.*; public class GOM extends JFrame implements ActionListener, KeyListener { Scanner in = new Scanner(System.in); Container content = this.getContentPane(); JTextField theGuess = new JTextField(); JLabel bankRoll = new JLabel("100.00 Zipoids"); JLabel d1 = new JLabel(" "); JLabel d2 = new JLabel(" "); JButton newPlayer = new JButton("New Player"); JButton newNumber = new JButton("New Number"); JTextField thePlayer = new JTextField(); JTextArea theOutput = new JTextArea("Enter your name" + "\n" + "Press ENTER to play"); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JScrollPane scrollArea = new JScrollPane(theOutput); Random randomizer = new Random(); String playerName = ""; int theNumber = 20; int numTries = 0; int numGames = 0; double amtRemaining = 100.0; public GOM() { JLabel guess = new JLabel("Make your guess:"); this.setVisible(true); this.setSize(500,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Guess O'Matic"); content.add(p1, BorderLayout.NORTH); p1.setLayout(new BorderLayout()); p1.add(bankRoll, BorderLayout.EAST); p1.add(guess, BorderLayout.WEST); p1.add(theGuess); content.add(p2, BorderLayout.SOUTH); p2.setLayout(new BorderLayout()); p2.add(thePlayer); p2.add(newPlayer, BorderLayout.WEST); p2.add(newNumber, BorderLayout.EAST); content.add(scrollArea, BorderLayout.CENTER); content.add(d1, BorderLayout.WEST); content.add(d2, BorderLayout.EAST); newPlayer.addActionListener(this); newNumber.addActionListener(this); thePlayer.addKeyListener(this); theGuess.addKeyListener(this); theOutput.setForeground(Color.LIGHT_GRAY); } public void actionPerformed(ActionEvent e) { JButton btn = (JButton) e.getSource(); if (btn == newPlayer) { this.newPlayer(); }else { newGame(); thePlayer.setText(playerName + " Game # " + numGames); theGuess.requestFocus(); theGuess.setEnabled(true); theGuess.setBackground(Color.yellow); } } private void newGame() { numGames++; randomizer.nextInt(100); numTries = 0; displayInstructions(); } public void newPlayer() { theOutput.setText(""); theOutput.setEnabled(false); theGuess.setEnabled(false); newPlayer.setEnabled(false); newNumber.setEnabled(false); thePlayer.setEnabled(true); thePlayer.setText(playerName); } @Override public void keyTyped(KeyEvent ke) { } @Override public void keyPressed(KeyEvent ke) { } @Override public void keyReleased(KeyEvent ke) { JTextField tf = (JTextField) ke.getSource(); int key = ke.getKeyCode(); if(key == KeyEvent.VK_ENTER) { if(tf == thePlayer) { addPlayer(); }else { newGuess(); } } } public void addPlayer() { playerName = thePlayer.getText(); if(playerName.equals("")) { theOutput.append("\nEnter a name to play"); }else { amtRemaining = 100; numGames = 0; thePlayer.setEnabled(false); thePlayer.setBackground(Color.WHITE); newPlayer.setEnabled(true); newNumber.setEnabled(true); theGuess.setEnabled(true); } } private void newGuess() { String curStr = ""; int curGuess = 0; Integer.parseInt(curStr); numTries++; theOutput.append(curStr); if(theNumber == curGuess) { gameWon(); }else { theOutput.append("Try a higher or lower number"); theGuess.requestFocus(); theGuess.setBackground(Color.yellow); theGuess.selectAll(); } } public void gameWon() { double curWinnings = 0; theOutput.setText("******* WINNER ********" + "\n" + "Amount Wagered "); theOutput.append("Amount of tries " + numTries + "\n" + "Amount Wagered: " + curWinnings); switch(numTries) { case 1: if(numTries == 1) { curWinnings = 2.00; } case 2: if(numTries == 2) { curWinnings = 1.75; } case 3: if(numTries == 3) { curWinnings = 1.50; } case 4: if(numTries == 4) { curWinnings = 1.25; } case 5: if(numTries == 5) { curWinnings = 1.00; } case 6: if(numTries == 6) { curWinnings = 0.75; } case 7: if(numTries == 7) { curWinnings = 0.50; } case 8: if(numTries == 8) { curWinnings = 0.25; } case 9: if(numTries == 9) { curWinnings = 0; } default: } } private void displayInstructions() { theOutput.append("Instructions test text"); } /** * @param args the command line arguments */ public static void main(String[] args) { GOM gui = new GOM(); } }
Patrice T
"когда я нажимаю enter, я получаю ошибку в консоли."
И вы планируете рассказать нам, что такое сообщение об ошибке и положение ошибки?
Barais_19
о да. мой плохой, ошибка-исключение в потоке "AWT-EventQueue-0" java.lang.NumberFormatException: для входной строки: ""
в java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
на Яве,Лэнг.Integer.parseInt(Integer.java:592)
на Яве,Лэнг.Integer.parseInt(Integer.java:615)
в gom.GOM.newGuess(GOM.java:175)
at gom.GOM.keyReleased(GOM.java:147)
в java.awt.Component.processKeyEvent(Component.java:6494)
в джавакс-свинг.JComponent.processKeyEvent(JComponent.java:2832)
в java.awt.Component.processEvent(Component.java:6310)
в java.awt.Container.processEvent(Container.java:2236)
в java.awt.Component.dispatchEventImpl(Component.java:4889)
в java.awt.Container.dispatchEventImpl(Container.java:2294)
в java.awt.Component.dispatchEvent(Component.java:4711)
в java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
в java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
в java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
в java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
в java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
в java.awt.Component.dispatchEventImpl(Component.java:4760)
в java.awt.Container.dispatchEventImpl(Container.java:2294)
в java.awt.Window.dispatchEventImpl(Window.java:2746)
в java.awt.Component.dispatchEvent(Component.java:4711)
в java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
в java.awt.EventQueue.доступ$500(EventQueue.java:97)
в java.awt.EventQueue$3.run(EventQueue.java:709)
в java.awt.EventQueue$3.run(EventQueue.java:703)
в java.security.AccessController.doPrivileged(Native метод)
в java.security.Нового Protectiondomain$JavaSecurityAccessImpl.doIntersectionPrivilege(нового protectiondomain.Ява:80)
в java.security.Нового Protectiondomain$JavaSecurityAccessImpl.doIntersectionPrivilege(нового protectiondomain.Ява:90)
в java.awt.EventQueue$4.run(EventQueue.java:731)
в java.awt.EventQueue$4.run(EventQueue.java:729)
в java.security.AccessController.doPrivileged(Native метод)
в java.security.Нового Protectiondomain$JavaSecurityAccessImpl.doIntersectionPrivilege(нового protectiondomain.Ява:80)
в java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
в java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
в java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
в java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
в java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
в java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
в java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
вот и вся ошибка, которую я получаю
Patrice T
Воспользуйся Улучшить вопрос чтобы обновить ваш вопрос.
Чтобы каждый мог обратить внимание на эту информацию.
Richard MacCutchan
Ваш метод newGuess не получает входную строку от пользователя, он просто использует нулевую строку, заданную в вашем коде.