簡體   English   中英

我想將 String 轉換為 long

[英]I want convert String to long

我有這樣的字符串""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 的這個請求,我無法直接獲取移動列表

刪除額外的\\因為\\\"會尋找\"這不是你的字符串的情況。

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

Output:

3333

就像這樣將替換替換為替換所有

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM