我想把字符串转换成长字符串

nkcskrwz  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(408)

我有这样的绳子 ""3333"" 我要转换成 Long 但当我看到这个例外

java.lang.NumberFormatException

我试过很多方法,但我不能改变
我正试着这样做

public void addNewFriend(List<String> mobileList, String login) {
    try {
        List<Long> list = new ArrayList<>();
        mobileList.forEach(x -> {
            log.debug(x);
            x=x.replace("\\\"","");
            Long y = Long.parseLong(x);
            log.debug(y.toString());
            list.add(y);
        });
        log.debug(list.toString());
        Set<AppUser> appUsers = appUserRepository.findByMobileNumberIn(list);
        if (appUsers.size() > 0) {
            AppUser appUser = appUserRepository.findByLogin(login);
            appUser.friends(appUsers);
            appUserRepository.save(appUser);
        } else {
            throw new BadRequestAlertException("cannot find user relation with author", "AppUser", "List is Empty");
        }
    } catch (NoSuchElementException e) {
        throw new BadRequestAlertException("Cannot find User", "Url", "Check your input");

    } catch (NullPointerException e) {
        throw new BadRequestAlertException("cannot find user relation with author", "AppUser", "List is Empty");

    } catch (Exception e) {
        throw new BadRequestAlertException(e.getMessage(), "Server Error", "Check it!");
    }

}

注意:我接受了Kafka的这个请求,我无法直接获得移动列表

kdfy810k

kdfy810k1#

移除多余的 \\ 因为 \\\" 我会找的 \" 你的绳子不是这样的。

public class Main {
    public static void main(String[] args) {
        System.out.println(Long.parseLong("\"3333\"".replace("\"", "")));
    }
}

输出:

3333
wz3gfoph

wz3gfoph2#

就这样换一个换一个

List<Long> list = new ArrayList<>();
mobileList.forEach(x -> {
    log.debug(x);
    x=x.replaceAll("\\\"","");
    Long y = Long.parseLong(x);
    log.debug(y.toString());
    list.add(y);
});

相关问题