错误出现在下面这些代码: String s = tx.getText().trim();
   int end1 = s.indexOf("+");
   int end2 = s.indexOf("=");
   String temp1 = s.substring(0,end1);
   String temp2 = s.substring(end1,end2);
   temp1 = temp1.trim(); 
   temp1 = temp2.trim(); 
   int a = Integer.parseInt(temp1);
   int b = Integer.parseInt(temp2);出错信息:Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "+"
当计算个位数的时候不会报错,就是计算多位数的时候会报这个错误,求助啊package work2;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;import work.ColorFrame;public class Calculator2 extends JFrame implements ActionListener
{

 public static final int WINDOW_WIDTH = 330;
 public static final int WINDOW_HEIGHT = 430;
 JTextArea tx = null;
 JButton[] jb=null; JButton jb1; 
 JButton jb2;
 JButton jb3;  
 JButton jb4; 
 JButton jb5; 
 JButton jb6; 
 public static void main(String[] args)
 {
  new Calculator().lunchFrame();
 } public void lunchFrame()
 {
  setTitle("Calculator");
  int width = getToolkit().getScreenSize().width;
  int height = getToolkit().getScreenSize().height;
  setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  setLocation((width - WINDOW_WIDTH) / 2,(height - WINDOW_HEIGHT) / 2);
  tx = new JTextArea();
  tx.setText("");
//  tx.setHorizontalAlignment(JTextField.RIGHT);
  tx.setEditable(false);
  tx.setBounds(0, 0, 330, 40);
  tx.setFont(new Font("仿宋", 25, 25));
  JPanel jp = new JPanel();
  jp.setBounds(0, 40, 330, 390);
  // 初始化0—-9这10个按钮
   jb= new JButton[10];
  this.setLayout(null);
  jp.setLayout(null);
  for (int i = 0; i < 10; i++)
  {
   jb[i] = new JButton("" + i);
   jb[i].addActionListener(this);
  }
  jb[7].setBounds(10, 70, 70, 50);
  jb[8].setBounds(90, 70, 70, 50);
  jb[9].setBounds(170, 70, 70, 50);
  jb[4].setBounds(10, 150, 70, 50);
  jb[5].setBounds(90, 150, 70, 50);
  jb[6].setBounds(170, 150, 70, 50);
  jb[1].setBounds(10, 230, 70, 50);
  jb[2].setBounds(90, 230, 70, 50);
  jb[3].setBounds(170, 230, 70, 50);
  jb[0].setBounds(80, 300, 100, 50);
  for (int i = 0; i < 10; i++)
   jp.add(jb[i]);
  // 初始化+-*/=c这6个按钮
  jb1 = new JButton("+");
  jb2 = new JButton("-");
  jb3 = new JButton("*");
  jb4 = new JButton("/");
  jb5 = new JButton("=");
  jb6 = new JButton("c");  jb1.setBounds(10, 10, 70, 50);
  jb2.setBounds(90, 10, 70, 50);
  jb3.setBounds(170, 10, 70, 50);
  jb4.setBounds(250, 10, 60, 90);
  jb5.setBounds(250, 110, 60, 90);
  jb6.setBounds(250, 210, 60, 90);
  jp.add(jb1);
  jp.add(jb2);
  jp.add(jb3);
  jp.add(jb4);
  jp.add(jb5);
  jp.add(jb6);
  // 设置监听
  jb1.addActionListener(this);
  jb2.addActionListener(this);
  jb3.addActionListener(this);
  jb4.addActionListener(this);
  jb5.addActionListener(this);
  jb6.addActionListener(this);  addWindowListener(new WindowAdapter()
  {
   @Override
   public void windowClosing(WindowEvent arg0)
   {
    System.exit(0);
   }
  });  add(jp);
  add(tx);
  this.setResizable(false);
  setVisible(true);
 } @Override
 public void actionPerformed(ActionEvent e)
 {
  for (int i = 0; i < 10; i++) {
  /*String s = tx.getText().trim();
  int m = Integer.parseInt(s);*/
  if(e.getSource()==jb[i]) {
  switch (i)
  {
   case 0:
//    if(m==0||s.matches("*//=*")){  
    tx.append("0");
 
//    }
    break;
   case 1:
    tx.append("1");
 
    break;
   case 2:
    tx.append("2");
  
    break;
   case 3:
    tx.append("3");
  
    break;
   case 4:
    tx.append("4");
   
    break;
   case 5:
    tx.append("5");
 
    break;
   case 6:
    tx.append("6");
    
    break;
   case 7:
    tx.append("7");    break;
   case 8:
    tx.append("8");
   
    break;
   case 9:
    tx.append("9");
  
    break;
   }
  }
  }
  if (e.getSource() == jb1)
  {

   tx.append("+");
  }
  if (e.getSource() == jb2)
  {
  
   tx.append("-");
  }
  if (e.getSource() == jb3)
  {
  
   tx.append("*");
  }
  if (e.getSource() == jb4)
  {
 
  tx.append("/");
  }
  if (e.getSource() == jb5)
  {
   tx.append("=");
   String s = tx.getText().trim();
   int end1 = s.indexOf("+");
   int end2 = s.indexOf("=");
   String temp1 = s.substring(0,end1);
   String temp2 = s.substring(end1,end2);
   temp1 = temp1.trim(); 
   temp1 = temp2.trim(); 
   int a = Integer.parseInt(temp1);
   int b = Integer.parseInt(temp2);
   if((s.matches("^\\d+\\+\\d+\\=$"))==true)
   {
 
    int c=a+b;
    tx.append(c+"");
   }
   if((s.matches("^\\d+\\-\\d+\\=$"))==true)
   {
    int c=a-b;
    tx.append(c+"");
   }
   if((s.matches("^\\d+\\*\\d+\\=$"))==true)
   {
    int c=a*b;
    tx.append(c+"");
   }
   if((s.matches("^\\d+\\/\\d+\\=$"))==true)
   {
   if(b == 0) {
   
   tx.setText("除数不能为0");
   }
   else {
   int c=a/b;
   tx.append(c+"");
   }
   } 
  }
  if (e.getSource() == jb6)
  {
   tx.setText("");
  }
 }}

解决方案 »

  1.   

     String temp2 = s.substring(end1,end2);写错了
    改成:
     String temp2 = s.substring(end1+1,end2);substring()方法的第一个参数表示子字符串开始的index,位于该index的子符会被包括在生成的子串中
      

  2.   

     public static void main(String[] args)
     {
      new Calculator().lunchFrame();
     }
    这里Calculator类哪来的?是你的老版本吧
    应该是   new Calculator2().lunchFrame();......
      

  3.   

    对于下面这个式子:String s = tx.getText().trim();
       int end1 =  s.indexOf("+");
       int end2 = s.indexOf("=");
       String temp1 = s.substring(0,end1);
       String temp2 = s.substring(end1+1,end2);
       temp1 = temp1.trim(); 
       temp1 = temp2.trim(); 
       int a = Integer.parseInt(temp1);
       int b = Integer.parseInt(temp2);  我想把里面的加改成+或-或*或/应该怎么改  int end1 =  s.indexOf("+");
      

  4.   


    int end1 =  s.indexOf("+");
    改成
    int end1 =  s.indexOf("*"); //  - / 都可以