我正在尝试创建一个计算器app.IN命令以避免重复运算符我正在检查字符串中的前一个字符是否是一个运算符。但是当我输入第三个字符时,我得到stringindexoutofboundsexception。
package com.example.calculatorproject;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity {
EditText txtresult;
ImageButton ib_backspace;
String result="";
char value;
Button btnc,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btn_add,btn_sub,btn_multi,btn_divide,btn_decimal,btn_equal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtresult=(EditText) findViewById(R.id.txtresult);
ib_backspace=(ImageButton) findViewById(R.id.ib_backspace);
txtresult.setInputType(InputType.TYPE_NULL);
btnc=(Button) findViewById(R.id.btnC);
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2);
btn3=(Button) findViewById(R.id.btn3);
btn4=(Button) findViewById(R.id.btn4);
btn5=(Button) findViewById(R.id.btn5);
btn6=(Button) findViewById(R.id.btn6);
btn7=(Button) findViewById(R.id.btn7);
btn8=(Button) findViewById(R.id.btn8);
btn9=(Button) findViewById(R.id.btn9);
btn0=(Button) findViewById(R.id.btn0);
btn_add=(Button) findViewById(R.id.btn_add);
btn_sub=(Button) findViewById(R.id.btn_sub);
btn_multi=(Button) findViewById(R.id.btn_multi);
btn_divide=(Button) findViewById(R.id.btn_divide);
btn_decimal=(Button) findViewById(R.id.btn_decimal);
btn_equal=(Button) findViewById(R.id.btn_equal);
ib_backspace.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result=txtresult.getText().toString();
if(result.length()!=0)
{
result = result.substring(0, result.length()-1);
setresult();
}
}
});
btnc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
result="";
setresult();
}
});
btn0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="0";
setresult();
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="1";
setresult();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="2";
setresult();
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="3";
setresult();
}
});
btn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="4";
setresult();
}
});
btn5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="5";
setresult();
}
});
btn6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="6";
setresult();
}
});
btn7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="7";
setresult();
}
});
btn8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="8";
setresult();
}
});
btn9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+="9";
setresult();
}
});
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
value=result.substring(result.length()-1).charAt(result.length()-1);
if((value=='+')||(value=='-')||(value=='*')||(value=='/')||(value=='.'))
{
result=(result.substring(0,result.length()-1))+"+";
setresult();
}
else
{
result+="+";
setresult();
}
}
});
btn_sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
value=result.substring(result.length()-1).charAt(result.length()-1);
if((value=='+')||(value=='-')||(value=='*')||(value=='/')||(value=='.'))
{
result=result.substring(0,result.length()-1)+"-";
setresult();
}
else
{
result+="-";
setresult();
}
}
});
btn_divide.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
value=result.substring(result.length()-1).charAt(result.length()-1);
if((value=='+')||(value=='-')||(value=='*')||(value=='/')||(value=='.'))
{
result=result.substring(0,result.length()-1)+"/";
setresult();
}
else
{
result+="/";
setresult();
}
}
});
btn_multi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
value=result.substring(result.length()-1).charAt(result.length()-1);
if((value=='+')||(value=='-')||(value=='*')||(value=='/')||(value=='.'))
{
result=result.substring(0,result.length()-1)+"*";
setresult();
}
else
{
result+="*";
setresult();
}
}
});
btn_decimal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
result+=".";
setresult();
}
});
btn_equal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public void setresult()
{
txtresult.setText(result);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
例如,如果我键入" 9 + 5 - *"因为表达式*应该替换 - 符号。但是当我输入9 + 5时,当我输入+时,会出现一个stringoutofboundsexception。请帮助!!
logcat
05-15 06:03:50.317: E/AndroidRuntime(1492): FATAL EXCEPTION: main
05-15 06:03:50.317: E/AndroidRuntime(1492): Process: com.example.calculatorproject, PID: 1492
05-15 06:03:50.317: E/AndroidRuntime(1492): java.lang.StringIndexOutOfBoundsException: length=1; index=2
05-15 06:03:50.317: E/AndroidRuntime(1492): at com.example.calculatorproject.MainActivity$14.onClick(MainActivity.java:175)
05-15 06:03:50.317: E/AndroidRuntime(1492): at android.view.View.performClick(View.java:4424)
05-15 06:03:50.317: E/AndroidRuntime(1492): at android.view.View$PerformClick.run(View.java:18383)
05-15 06:03:50.317: E/AndroidRuntime(1492): at android.os.Handler.handleCallback(Handler.java:733)
05-15 06:03:50.317: E/AndroidRuntime(1492): at android.os.Handler.dispatchMessage(Handler.java:95)
05-15 06:03:50.317: E/AndroidRuntime(1492): at android.os.Looper.loop(Looper.java:137)
05-15 06:03:50.317: E/AndroidRuntime(1492): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-15 06:03:50.317: E/AndroidRuntime(1492): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 06:03:50.317: E/AndroidRuntime(1492): at java.lang.reflect.Method.invoke(Method.java:515)
05-15 06:03:50.317: E/AndroidRuntime(1492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-15 06:03:50.317: E/AndroidRuntime(1492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-15 06:03:50.317: E/AndroidRuntime(1492): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
对于长度不是1的任何字符串,此行都会被中断:
value = result.substring(result.length()-1).charAt(result.length()-1);
考虑不同的长度:
result
是一个空字符串):result.substring(result.length() - 1)
将失败,因为您正在调用result.substring(-1)
。邦。result
是单字符字符串):result.substring(0)
返回一个字符的字符串(相当于result)
,然后charAt(0)
返回该单个字符result.substring(result.length() - 1)
返回一个字符的字符串,然后您调用的值charAt
的值大于0. Bang。如果你只是想找到字符串的最后一个字符,你需要:
value = result.charAt(result.length() - 1);
无需拨打substring
。
我还强烈建议您考虑使用更多局部变量和更少的实例变量。为什么value
是一个实例变量?当你基本上想要操作文本框内容时,为什么result
是一个实例变量?