gpt4 book ai didi

java - 如何将字符串从包含字符和整数的变量转换为整数

转载 作者:行者123 更新时间:2023-11-30 01:39:17 24 4
gpt4 key购买 nike

我正在学习 java 和 android SDK。为了学习属性,我构建了一个计算器(尚未完成)。在我的代码中,我使用 ImageView 标签来获取用户点击的按钮的值:

ImageView button = (ImageView) view;        
String buttonTag = button.getTag().toString();

xml:

<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/zero"
android:layout_row="17"
android:layout_column="1"
android:tag="0"
android:onClick="calcPad" />

ImageView 标签值是:“0”-“9”、“加号”/“减号”等。所以我将它转换为字符以获取数字并将其转换回整数:

char[] charTag = buttonTag.toCharArray();
for (int z=0; z < charTag.length; z++) {
if (charTag[z] == '0') {number = 0;}
if (charTag[z] == '1') {number = 1;}
...
if (charTag[z] == '9') {number = 9;}

我曾尝试使用“try”和“catch”,但应用程序崩溃了。这是我试图做的:

try{
num = Integer.getInteger(buttonTag);

} catch(NumberFormatException e) {
Log.i("Exception", e.getMessage());
switch(buttonTag) {
case "Plus":
.....
default:
}

}

我需要添加什么才能“处理”异常?我试图在 catch 中添加一个获取 buttonTag 值(即字符串)的开关,但仍然是同样的问题。

完整代码(没有 try/catch 部分):

 package com.example.idan.calculator;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;


public class MainActivity extends AppCompatActivity {
double var1 = 0; //holds the first number
double var2 = 0; //holds the second number
int decimalPoint = 0; //indicate if the user clicked on the (.)
String var1string=""; //for processing
String var2string=""; //for processing
double var1Array[]={0,0}; // the return value of var1doublemaker
double var2Array[]={0,0}; // the return value of var2doublemaker
boolean mathAction = false; // indicate if the user clicked on a mathematical button
int mathType = 0; // help me identify between + - * /
char mathValue = 'x'; // for the display
double result = 0; // final result

public double[] var1doubleMaker (String buttonTag, int decimalPoint) {
if (decimalPoint == 1) { //need to add dot to string
var1string = var1string + "." + buttonTag;
var1 = Double.valueOf(var1string);
decimalPoint = 0; // set back to 0
var1Array[0] = var1;
var1Array[1] = decimalPoint;
} else {
var1string = var1string + buttonTag; // add the last number
var1 = Double.valueOf(var1string);
var1Array[0] = var1;
var1Array[1] = decimalPoint;
}
return(var1Array);


}
public double[] var2doubleMaker (String buttonTag, int decimalPoint) {
if (decimalPoint == 1) {
var2string = var2string + "." + buttonTag;
var2 = Double.valueOf(var2string);
decimalPoint = 0;
var2Array[0] = var2;
var2Array[1] = decimalPoint;
} else {
var2string = var2string + buttonTag;
var2 = Double.valueOf(var2string);
var2Array[0] = var2;
var2Array[1] = decimalPoint;
}
return(var2Array);


}


public void calcPad(View view) throws NumberFormatException{

//check which button the user clicked on-
// first: get the ImageView by id
ImageView button = (ImageView) view; // where the user clicked on
String buttonTag = button.getTag().toString(); // hold the tag as string for the switch
char[] charTag = buttonTag.toCharArray();
int number = 111; // 111 - just for initialization
int num = 111;
/*
try{
num = Integer.getInteger(buttonTag);

} catch(NumberFormatException e) {
Log.i("Exception", e.getMessage());

}
*/
Log.i("info - num value:", String.valueOf(num));
//Log.i("info char", String.valueOf(charTag[0]));

char numberToResolve = charTag[0];
switch (numberToResolve) {
case '0':
number = 0;
break;
case '1':
number = 1;
break;
case '2':
number = 2;
break;
case '3':
number = 3;
break;
case '4':
number = 4;
break;
case '5':
number = 5;
break;
case '6':
number = 6;
break;
case '7':
number = 7;
break;
case '8':
number = 8;
break;
case '9':
number = 9;
break;
default:
Log.i("error", "cant resolve number from charTag[]");
}


//Log.i("info number value", String.valueOf(number));

TextView textView = (TextView) findViewById(R.id.calcDisplay); // create an object to print the button that the user clicked

Log.i("info", buttonTag);

// math
if (buttonTag.length() > 1) {
switch(buttonTag) {
case "divid":
mathValue = '/';
mathAction = true;
mathType = 4;
break;
case "multi":
mathValue = '*';
mathAction = true;
mathType = 3;
break;
case "Plus":
mathValue = '+';
mathAction = true;
mathType = 1;
break;
case "minus":
mathValue = '-';
mathAction = true;
mathType = 2;
break;
case "dot":
decimalPoint = 1;
case "equal":
if (mathType == 1) {
result = var1 + var2;
} else if (mathType == 2) {
result = var1 - var2;
} else if (mathType == 3) {
result = var1 * var2;
} else if (mathType == 4) {
result = var1 / var2;
}
String display = Double.toString(result);
textView.setText(display);
break;
case "cc":
var1 = 0;
var2 = 0;
mathAction = false;
decimalPoint = 0;
result = 0;
var1string=""; //for processing
var2string="";
display = Double.toString(result);
textView.setText(display);
break;
default:
Log.i("error", "cant resolve math type from buttonTag");
}
}

//Log.i("info decimalpoint", String.valueOf(decimalPoint));


if (number >= 0 && number <= 9 ) {
// user clicked on a number
//call doubleMaker
if (mathAction) {
//working on var2
double[] var2temp = var2doubleMaker(buttonTag,decimalPoint);

Double dd= var2temp[1];
decimalPoint = dd.intValue(); // set decimalPoint back to 0

// display var1 + math + var2
String display = Double.toString(var1) + Character.toString(mathValue) + Double.toString(var2temp[0]);

textView.setText(display);
} else {
// working on var1
double[] var1temp = var1doubleMaker(buttonTag,decimalPoint);

Double d= var1temp[1];
decimalPoint = d.intValue();
String display = Double.toString(var1temp[0]);

textView.setText(display);
}


}

Log.i("info - var1:", Double.toString(var1));
Log.i("info - var2:", Double.toString(var2));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

你只需要将你的字符串传递给它:Integer.parseInt("string")。此方法帮助您从字符串转换为 int

 ImageView button = (ImageView) view;                
String buttonTag = button.getTag().toString();
int number = Integer.parseInt(buttonTag)

关于java - 如何将字符串从包含字符和整数的变量转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com