gpt4 book ai didi

java - 获取 numberFormatException 无效 double 值

转载 作者:行者123 更新时间:2023-12-02 06:10:59 27 4
gpt4 key购买 nike

我将一个数值放入 EditText 中。假设我得到输入首先说“输入”。然后对输入执行toString()。然后parseDouble()。假设结果是输入*2。所以我然后在 EditText 中进行设置。假设,

          package com.hasibshovon.unitconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends Activity {
// data source
private static String[] types = { "Angle", "Area", "Bits&Bytes", "Density",
"Electric Current", "Energy", "Force", "Length", "Mass", "Power",
"Pressure", "Speed", "Temperature", "Time", "Volume" };

private static String[] AngleUnits = { "Degree", "Radian", "Grad", "Gon",
"Minute", "Second", "SIgn", "Mil", "Revolution", "Circle", "Turn",
"Quadrant", "Right", "Triangle", "Sextant" };

private static String[] AreaUnits = { "SquareMeter", "SquareKilometer",
"SquareHectometer", "SquareDekameter", "SquareDecimeter",
"SquareCentimeter", "SquareMilimeter", "SquareMicrometer",
"SquareNanometer", "Hectare", "SquareMile", "SquareYard",
"SquareFoot", "SquareInch", "Acre" };

private static String[] BitsBytesUnits = { "bit", "byte", "kilobit",
"kilobyte", "megabit", "megabyte", "gigabit", "gigabyte",
"terabit", "terabyte", "petabit", "petabyte", "exabit", "exabyte" };

private static String[] DensityUnits = { "kilogram/cubicmeter",
"kilogram/cubiccentimeter", "gram/cubicmeter",
"gram/cubiccentimeter", "gram/cubicmilimeter",
"miligram/cubicmeter", "miligram/cubiccentimeter",
"miligram/cubicmilimeter", "exagram/liter", "petagram/liter",
"teragram/liter", "gigagram/liter", "meggram/liter",
"kilogram/liter", "hectogram/liter", "decagram/liter",
"gram/liter", "decigram/liter", "centigram/liter",
"miligram/liter", "microgram/liter", "nanogram/liter",
"picogram/liter" };

private static String[] CurrentUnits = { "ampere", "kiloampere",
"miliampere", "biot", "abaampere", "stataampere" };

private static String[] EnergyUnits = { "joule", "gigajoule", "megajoule",
"kilojoule", "milijoule", "microjoule", "nanojoule", "attojoule" };

private static String[] ForceUnits = { "newton", "dyne", "joul/meter",
"joul/centimeter", "gram-force", "kilogram-force", "kip-force",
"pond", "kilo-pond" };

private static String[] LengthUnits = { "meter", "exameter", "terameter",
"petameter", "gigameter", "megameter", "kilometer", "hectometer",
"dekameter", "decimeter", "centimeter", "milimeter", "micrometer",
"nanometer", "picometer", "femtometer", "attometer" };

private static String[] MassUnits = { "gram", "kilogram", "miligram",
"dalton", "pound", "ounce", "ton", "quarter", "grain" };

private static String[] PowerUnits = { "watt", "megawatt", "horsepower",
"kilocalorie", "joul/hour", "kilojoul/hour", "joul/second" };

private static String[] PressureUnits = { "pascal", "newton/squaremeter",
"bar", "kilogram-force/squaremeter", "ton-force/squarefoot",
"poundal/squarefoot", "atmosphere(atm)" };

private static String[] SpeedUnits = { "meter/second", "kilometer/hour",
"foot/hour", "yard/hour", "mile/hour", "mile/second", "knot" };

private static String[] TemperatureUnits = { "kelvin", "celsius",
"farenheit", "rankine", "reaumur" };

private static String[] TimeUnits = { "second", "milisecond",
"microsecond", "nanosecond", "minute", "hour", "day", "week",
"month", "year", "decade", "century", "millenium" };

private static String[] VolumeUnits = { "cubicmeter", "cubickilometer",
"cubiccentimeter", "liter", "cc", "barrel", "cup", "cubicmile",
"cubicfoot", "cubicyard", "cubicinch", "acre-foot" };

private Spinner spnUnitTypes;
private Spinner spnFromUnit;
private Spinner spnToUnit;
private EditText etFrom;
private EditText etTo;
public String value1, value2;
public double fromDouble;
public double result;
// adapters
ArrayAdapter<String> adaptUnittype;
ArrayAdapter<String> adaptFrom;
ArrayAdapter<String> adaptTo;

// private OnItemSelectedListener UnitLisetener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// spnUnitTypes.setOnItemSelectedListener(UnitLisetener);
etFrom = (EditText) findViewById(R.id.etConvertFrom);

etTo = (EditText) findViewById(R.id.etToResult);

// initialize view
spnUnitTypes = (Spinner) findViewById(R.id.spnUnitType);
spnFromUnit = (Spinner) findViewById(R.id.spnFromUnit);
spnToUnit = (Spinner) findViewById(R.id.spnToUnit);

// initialize adapter
adaptUnittype = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, types);

// bind adapter and view
spnUnitTypes.setAdapter(adaptUnittype);
spnUnitTypes.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int postion, long arg3) {
if (types[postion].equals("Time")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, TimeUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, TimeUnits);

} else if (types[postion].equals("Mass")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, MassUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, MassUnits);

} else if (types[postion].equals("Angle")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AngleUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AngleUnits);

} else if (types[postion].equals("Area")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AreaUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AreaUnits);

} else if (types[postion].equals("Density")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, DensityUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, DensityUnits);

} else if (types[postion].equals("Bits&Bytes")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
BitsBytesUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
BitsBytesUnits);

} else if (types[postion].equals("Electric Current")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, CurrentUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, CurrentUnits);

} else if (types[postion].equals("Energy")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, EnergyUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, EnergyUnits);

} else if (types[postion].equals("Force")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, ForceUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, ForceUnits);

} else if (types[postion].equals("Length")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, LengthUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, LengthUnits);

} else if (types[postion].equals("Power")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PowerUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PowerUnits);

} else if (types[postion].equals("Pressure")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PressureUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PressureUnits);

} else if (types[postion].equals("Speed")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, SpeedUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, SpeedUnits);

} else if (types[postion].equals("Temperature")) {

adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
TemperatureUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
TemperatureUnits);
}
spnFromUnit.setAdapter(adaptFrom);
spnToUnit.setAdapter(adaptTo);
spnFromUnit
.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int pos, long arg3) {
Object item = parent.getItemAtPosition(pos);
value1 = item.toString();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});
spnToUnit
.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int pos, long arg3) {
Object item1 = parent.getItemAtPosition(pos);
value2 = item1.toString();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
});
String fromstr = etFrom.getText().toString();
double fromDouble = Double.parseDouble(fromstr);

}


public void convert(View v) {
if (value1.equals("Degree") && value2.equals("Radian")) {
etTo.setText(String.valueOf(fromDouble*2));
}
}

@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;
}

}

最佳答案

您的输入似乎为空或不包含要解析的数字。通过使用 try/catch 记录或打印出值来尝试调试

String fromstr = etFrom.getText().toString();
try {
double fromDouble = Double.parseDouble(fromstr);
} catch (NumberFormatException e) {
System.out.println(fromstr)
}

关于java - 获取 numberFormatException 无效 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886516/

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