前提・実現したいこと
JTextFieldに文字を入れるとこのエラーが出ます。対処法が本当に分からないので教えていただきたいです。
発生している問題・エラーメッセージ
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "tokyo" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at J13_2.actionPerformed(J13_2.java:59) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342) at java.desktop/java.awt.Component.processEvent(Component.java:6401) at java.desktop/java.awt.Container.processEvent(Container.java:2263) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
該当のソースコード
Java
1import java.awt.FlowLayout; 2import java.awt.event.ActionEvent; 3import java.awt.event.ActionListener; 4import java.io.DataOutputStream; 5import java.io.InputStream; 6import java.io.ObjectInputStream; 7import java.io.OutputStream; 8import java.net.Socket; 9 10import javax.swing.JButton; 11import javax.swing.JFrame; 12import javax.swing.JTextField; 13 14public class J13_3Client extends JFrame implements ActionListener{ 15 16 JTextField tf1; 17 JTextField tf2; 18 JButton button; 19 20 int a, b; 21 static String server; 22 static int port; 23 24 J13_3Client(String title) { 25 super(title); 26 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 27 setSize(150, 150); 28 29 setLayout(new FlowLayout()); 30 31 tf1 = new JTextField(11); 32 tf2 = new JTextField(11); 33 add(tf1); 34 add(tf2); 35 tf1.setHorizontalAlignment(JTextField.CENTER); 36 tf2.setHorizontalAlignment(JTextField.CENTER); 37 38 button = new JButton("送信"); 39 add(button); 40 button.addActionListener(this); 41 42 setVisible(true); 43 } 44 45 public static void main(String[] args) { 46 // サーバーとポートを取得 47 server = args[0]; 48 port = Integer.parseInt(args[1]); 49 50 new J13_2("課題3"); 51 } 52 53 public void actionPerformed(ActionEvent e) { 54 String skey = tf1.getText(); 55 System.out.println(skey); 56 57 try { 58 // ソケットを作成 59 Socket s = new Socket(server, port); 60 61 OutputStream os = s.getOutputStream(); 62 DataOutputStream dos = new DataOutputStream(os); 63 dos.writeUTF(skey); 64 65 // サーバーから結果を読み取る 66 InputStream is = s.getInputStream(); 67 ObjectInputStream ois = new ObjectInputStream(is); 68 Object o = ois.readObject(); 69 70 // 結果を表示 71 if(o == null) { 72 tf2.setText("該当なし"); 73 }else { 74 tf2.setText(o.toString()); 75 } 76 77 // ソケットをクローズ 78 s.close(); 79 } catch (Exception ee) { 80 ee.printStackTrace(); 81 } 82 } 83}
試したこと
補足情報(FW/ツールのバージョンなど)
あなたの回答
tips
プレビュー