如何在Java中将String转换为int,其值包含类似“1,2,3,4,5,6”的数字并转换为Array?

时间:2017-10-12 07:33:06

标签: java android arrays variables

我有这样的代码

 private void profileUser() {
    ApiServices api = InitLibrary.getInstance();

    Call<ResponseProfile> call = api.profile_request(username, api_key, userID);

    call.enqueue(new Callback<ResponseProfile>() {
        @Override
        public void onResponse(Call<ResponseProfile> call, Response<ResponseProfile> response) {
            if (response.isSuccessful()) {
                String return_value = response.body().getReturnValue();
                data = response.body().getData();

                tvUsername.setText(data.getUserName());
                tvEmail.setText(data.getUserEmail());
                tvNoHp.setText(data.getMobile());

                String gender = data.getSex();
                switch (gender) {
                    case "1":
                        gender = "Laki-laki";
                        break;
                    case "2":
                        gender = "Perempuan";
                        break;
                    case "M":
                        gender = "Laki-laki";
                        break;
                    case "F":
                        gender = "Perempuan";
                }

                tvGender.setText(gender);
                tvBirthdate.setText(data.getBirthdate());

                String edu = data.getIdEdu();
                switch (edu) {
                    case "1":
                        edu = "SD";
                        break;
                    case "2":
                        edu = "SMP/sederajat";
                        break;
                    case "3":
                        edu = "SMA/sederajat";
                        break;
                    case "4":
                        edu = "Diploma";
                        break;
                    case "5":
                        edu = "S1";
                        break;
                    case "6":
                        edu = "S2";
                        break;
                    case "7":
                        edu = "S3";
                        break;
                    default:
                        edu = "Lainnya";
                }

                tvEdukasi.setText(edu);
                //=============================================================================================================
                List<Integer> exp_1;
                List<Integer> exp_2;
                List<Integer> exp_3;
                List<Integer> exp_4;
                List<Integer> exp_5;

                String experience  = (data.getIdExp());
                int exx = Integer.valueOf(experience);

                int[] research_exp = {exx};

                Toast.makeText(HomeActivity.this, experience, Toast.LENGTH_SHORT).show();

                exp_1 = Arrays.asList(research_exp[0]);
                exp_2 = Arrays.asList(research_exp[1]);
                exp_3 = Arrays.asList(research_exp[2]);
                exp_4 = Arrays.asList(research_exp[3]);
                exp_5 = Arrays.asList(research_exp[4]);

                Toast.makeText(HomeActivity.this, exp_1.toString(), Toast.LENGTH_SHORT).show();

                switch (exp_1) {
                    case 1:
                        exp_1 = Integer.parseInt("Belum punya pengalaman");
                        break;
                    case 2:
                        exp_1 = Integer.parseInt("Industri barang konsumen");
                        break;
                    case 3:
                        exp_1 = Integer.parseInt("Industri otomotif");
                        break;
                    case 4:
                        exp_1 = Integer.parseInt("Industri telekomunikasi");
                        break;
                    case 5:
                        exp_1 = Integer.parseInt("Industri perbankan");
                        break;
                    case 6:
                        exp_1 = Integer.parseInt("Industri real estate");
                        break;
                    case 7:
                        exp_1 = Integer.parseInt("Industri barang elektronik");
                        break;
                    default:
                        exp_1 = Integer.parseInt("Lainnya");
                }

               switch (exp_2) {
                case 1:
                    exp_2 = Integer.parseInt("Belum punya pengalaman");
                    break;
                case 2:
                    exp_2 = Integer.parseInt("Industri barang konsumen");
                    break;
                case 3:
                    exp_2 = Integer.parseInt("Industri otomotif");
                    break;
                case 4:
                    exp_2 = Integer.parseInt("Industri telekomunikasi");
                    break;
                case 5:
                    exp_2 = Integer.parseInt("Industri perbankan");
                    break;
                case 6:
                    exp_2 = Integer.parseInt("Industri real estate");
                    break;
                case 7:
                    exp_2 = Integer.parseInt("Industri barang elektronik");
                    break;
                default:
                    exp_2 = Integer.parseInt("Lainnya");
            }


                switch (exp_3) {
                    case 1:
                        exp_3 = Integer.parseInt("Belum punya pengalaman");
                        break;
                    case 2:
                        exp_3 = Integer.parseInt("Industri barang konsumen");
                        break;
                    case 3:
                        exp_3 = Integer.parseInt("Industri otomotif");
                        break;
                    case 4:
                        exp_3 = Integer.parseInt("Industri telekomunikasi");
                        break;
                    case 5:
                        exp_3 = Integer.parseInt("Industri perbankan");
                        break;
                    case 6:
                        exp_3 = Integer.parseInt("Industri real estate");
                        break;
                    case 7:
                        exp_3 = Integer.parseInt("Industri barang elektronik");
                        break;
                    default:
                        exp_3 = Integer.parseInt("Lainnya");
                }

                switch (exp_4) {
                    case 1:
                        exp_4 = Integer.parseInt("Belum punya pengalaman");
                        break;
                    case 2:
                        exp_4 = Integer.parseInt("Industri barang konsumen");
                        break;
                    case 3:
                        exp_4 = Integer.parseInt("Industri otomotif");
                        break;
                    case 4:
                        exp_4 = Integer.parseInt("Industri telekomunikasi");
                        break;
                    case 5:
                        exp_4 = Integer.parseInt("Industri perbankan");
                        break;
                    case 6:
                        exp_4 = Integer.parseInt("Industri real estate");
                        break;
                    case 7:
                        exp_4 = Integer.parseInt("Industri barang elektronik");
                        break;
                    default:
                        exp_4 = Integer.parseInt("Lainnya");
                }

                switch (exp_5) {
                    case 1:
                        exp_5 = Integer.parseInt("Belum punya pengalaman");
                        break;
                    case 2:
                        exp_5 = Integer.parseInt("Industri barang konsumen");
                        break;
                    case 3:
                        exp_5 = Integer.parseInt("Industri otomotif");
                        break;
                    case 4:
                        exp_5 = Integer.parseInt("Industri telekomunikasi");
                        break;
                    case 5:
                        exp_5 = Integer.parseInt("Industri perbankan");
                        break;
                    case 6:
                        exp_5 = Integer.parseInt("Industri real estate");
                        break;
                    case 7:
                        exp_5 = Integer.parseInt("Industri barang elektronik");
                        break;
                    default:
                        exp_5 = Integer.parseInt("Lainnya");
                }

                tvExperience.setText(Arrays.toString(research_exp));
}

我有一个字符串变量,其中包含一个数字,例如像“1,2,3,4,5,6”。但我想将其转换为整数并再次转换为数组变量。其中每个数字的目的都有自己的变量值,使用switch case再次转换为字符串,但我的代码不起作用,所以我请求帮助,谢谢

1 个答案:

答案 0 :(得分:2)

Integer.parseInt(String s);需要"1""135"等字符串中的数字。该方法无法转换文字。

这不起作用Integer.parseInt("Belum punya pengalaman");

这是有效的Integer.parseInt("356");