在 android 中将 Spinner 字符串值“20-34”转换为从 20 到 34 的整数
Convert Spinner String values "20-34" as integer from 20 to 34 in android
如何将Spinner字符串值如“20-34”、35-44“等转换为20到34的整数,android.Below中35到44的整数是取整数的代码微调器的值和分配点以进行数学计算
public static int age_value(String gender, String ag) {
int b;
b = Integer.parseInt(ag);
if (gender.equals("f")) {
if (20 <= b && b <= 34) {
ageValue = -7;
} else if (35 <= b && b <= 39) {
ageValue = -3;
} else if (40 <= b && b <= 44)
ageValue = 0;
else if (45 <= b && b <= 49)
ageValue = 3;
else if (50 <= b && b <= 54)
ageValue = 6;
else if (55 <= b && b < 59)
ageValue = 8;
else if (60 <= b && b <= 64)
ageValue = 10;
else if (65 <= b && b <= 69)
ageValue = 12;
else if (70 <= b && b <= 74)
ageValue = 14;
else if (75 <= b && b <= 79)
ageValue = 16;
} else if (gender.equals("m")) {
if (20 <= b && b <= 34) {
ageValue = -9;
} else if (35 <= b && b <= 39) {
ageValue = -4;
} else if (40 <= b && b <= 44)
ageValue = 0;
else if (45 <= b && b <= 49)
ageValue = 3;
else if (50 <= b && b <= 54)
ageValue = 6;
else if (55 <= b && b < 59)
ageValue = 8;
else if (60 <= b && b <= 64)
ageValue = 10;
else if (65 <= b && b <= 69)
ageValue = 11;
else if (70 <= b && b <= 74)
ageValue = 12;
else if (75 <= b && b <= 79)
ageValue = 13;
}
return ageValue;
}
下面显示了添加了上述方法的微调器
int array1 = R.array.gender;
// final String[] gender1 = getResources().getStringArray(R.array.gender);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getApplicationContext(), array1, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.spinnerdrop);
Gender.setAdapter(adapter);
Gender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
rowgender = parent.getItemAtPosition(position).toString();
if(rowage!=null && rowgender!=null) {
MainActivity.age_value(rowgender, rowage);
}
Gender.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
但是显示错误
java.lang.NumberFormatException: Invalid int: "20–34"
使用字符串 split()
方法来做到这一点
String ag = "20-34";
String[] value = ag.split("-");
int b= Integer.parseInt(value[0]);// here value 20
int c= Integer.parseInt(value[1]);// here value 34
然后进行操作
if (20 <= b && c <= 34) {
ageValue = -7;
} else if
....
....
您可以使用 Java 8 的 IntStream.range
:
public static void main(String[] args) throws Exception {
String s = "20-34";
int start = 0;
int end = 0;
if (s.matches("\d+-\d+")) { // Check if input String is in correct format
String[] arr = s.split("-"); // split based on "-"
start = Integer.parseInt(arr[0]); // get start range
end = Integer.parseInt(arr[1]); // get end range
}
List<Integer> nums = IntStream.range(start, end).boxed().collect(Collectors.toList()); // use IntStream.range method to generate range.
System.out.println(nums);
}
O/P :
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
有很多方法可以做到这一点。
你也可以这样做:
List<Integer> nums = new ArrayList<>();
IntStream.range(start, end).forEach(nums::add);
System.out.println(nums);
如何将Spinner字符串值如“20-34”、35-44“等转换为20到34的整数,android.Below中35到44的整数是取整数的代码微调器的值和分配点以进行数学计算
public static int age_value(String gender, String ag) {
int b;
b = Integer.parseInt(ag);
if (gender.equals("f")) {
if (20 <= b && b <= 34) {
ageValue = -7;
} else if (35 <= b && b <= 39) {
ageValue = -3;
} else if (40 <= b && b <= 44)
ageValue = 0;
else if (45 <= b && b <= 49)
ageValue = 3;
else if (50 <= b && b <= 54)
ageValue = 6;
else if (55 <= b && b < 59)
ageValue = 8;
else if (60 <= b && b <= 64)
ageValue = 10;
else if (65 <= b && b <= 69)
ageValue = 12;
else if (70 <= b && b <= 74)
ageValue = 14;
else if (75 <= b && b <= 79)
ageValue = 16;
} else if (gender.equals("m")) {
if (20 <= b && b <= 34) {
ageValue = -9;
} else if (35 <= b && b <= 39) {
ageValue = -4;
} else if (40 <= b && b <= 44)
ageValue = 0;
else if (45 <= b && b <= 49)
ageValue = 3;
else if (50 <= b && b <= 54)
ageValue = 6;
else if (55 <= b && b < 59)
ageValue = 8;
else if (60 <= b && b <= 64)
ageValue = 10;
else if (65 <= b && b <= 69)
ageValue = 11;
else if (70 <= b && b <= 74)
ageValue = 12;
else if (75 <= b && b <= 79)
ageValue = 13;
}
return ageValue;
}
下面显示了添加了上述方法的微调器
int array1 = R.array.gender;
// final String[] gender1 = getResources().getStringArray(R.array.gender);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getApplicationContext(), array1, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.spinnerdrop);
Gender.setAdapter(adapter);
Gender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
rowgender = parent.getItemAtPosition(position).toString();
if(rowage!=null && rowgender!=null) {
MainActivity.age_value(rowgender, rowage);
}
Gender.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
但是显示错误
java.lang.NumberFormatException: Invalid int: "20–34"
使用字符串 split()
方法来做到这一点
String ag = "20-34";
String[] value = ag.split("-");
int b= Integer.parseInt(value[0]);// here value 20
int c= Integer.parseInt(value[1]);// here value 34
然后进行操作
if (20 <= b && c <= 34) {
ageValue = -7;
} else if
....
....
您可以使用 Java 8 的 IntStream.range
:
public static void main(String[] args) throws Exception {
String s = "20-34";
int start = 0;
int end = 0;
if (s.matches("\d+-\d+")) { // Check if input String is in correct format
String[] arr = s.split("-"); // split based on "-"
start = Integer.parseInt(arr[0]); // get start range
end = Integer.parseInt(arr[1]); // get end range
}
List<Integer> nums = IntStream.range(start, end).boxed().collect(Collectors.toList()); // use IntStream.range method to generate range.
System.out.println(nums);
}
O/P :
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
有很多方法可以做到这一点。
你也可以这样做:
List<Integer> nums = new ArrayList<>();
IntStream.range(start, end).forEach(nums::add);
System.out.println(nums);