转换Spinner字符串值" 20-34"在android中为20到34的整数

时间:2016-12-05 07:19:31

标签: java android android-studio-2.1

如何将Spinner字符串值(例如" 20-34",35-44"等)转换为20到34之间的整数,android中的整数从35到44.Below是代码用于从微调器获取整数值并为数学计算分配点

 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:无效的int:&#34; 20-34&#34;

2 个答案:

答案 0 :(得分:1)

使用字符串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
     .... 
     ....

答案 1 :(得分:1)

您可以使用Java 8&#39; 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);