-1

I have String like this ""3333"" I want convert to Long but when convert I am seeing this Exception

java.lang.NumberFormatException

I try many ways but I cant convert

I am trying this way

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


}

note: I am take this request from kafka I can't get mobile list in long direct

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Does this answer your question? [How can I prevent java.lang.NumberFormatException: For input string: "N/A"?](https://stackoverflow.com/questions/18711896/how-can-i-prevent-java-lang-numberformatexception-for-input-string-n-a) – Zoe Apr 07 '21 at 12:34

2 Answers2

1

Remove the extra \\ because \\\" will look for \" which is not the case with your string.

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

Output:

3333
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

Just soultion change replace to replaceAll like this

    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);
});
  • 1
    `replace("\"", "")` and `replaceAll("\\\"","")` will both produce the same result, but the second uses regex, and there is no need for that. Since the first is simpler, you should accept the [answer by Arvind Kumar Avinash](https://stackoverflow.com/a/66952451/5221149). – Andreas Apr 05 '21 at 12:00
  • i cant accept anthor soultion because my input like this ` ["[""33333"",""44444"", ""11111" ",""2020""]" ` @Andreas – Abdalrhman Alkraien Apr 05 '21 at 12:10
  • Did you test both the replace("\"", "") and replaceAll("\\\"", "") solutions? – NomadMaker Apr 05 '21 at 12:37
  • 1
    `replace("\"", "")` and `replaceAll("\\\"","")` does the **exact same thing**, i.e. fix the problem with the `replace("\\\"","")` in the question, so I can't see why you can't accept Arvind's answer, which tells you to use the simpler `replace("\"", "")`, with proof that it works. – Andreas Apr 05 '21 at 15:07