簡體   English   中英

JSoup從字符串到Int

[英]JSoup From String to Int

我曾在android studio中使用JSoup。 在我的代碼中,我有:

Elements description = document.select("something");

如果我這樣做-

String foo = description.text();

一切正常。

但是如果我這樣做-

int y = Integer.parseInt(description.text());

為什么我的應用程序崩潰?

我所有的代碼:

public Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup
                    .connect("something")
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko")
                    .get();
            Elements description = document.select("something");

            String foo = description.text();
            int fuu = Integer.parseInt(foo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

請嘗試int foo = Integer.parse(description.ownText());

如果沒有堆棧跟蹤和原始的HTML或URL,幾乎不可能說出代碼中正在發生什么。 但是,我看到發生崩潰的一種可能性,即該字符串不包含有效數字。 由於您聲明該字符串的打印輸出實際上將打印出“ 65”,因此我只能假定該字符串可能使用了奇怪的uft8 Unicode字符,這些字符看起來像或是數字,但不能由Integer.parseInt(String)解釋。

例如,如果您輸入的字符串是“𝟼𝟻”,則可能看起來像“ 65”,但是使用U + 1D7FC(數學單點數字六)和U + 1D7FB(數學單點數字五)進行Unicode編碼

在這樣的Unicode字符數的名單,可以發現這里

解決方法( 在此處的注釋中建議 )可能是使用Normalizer類

String numberStr = Normalizer.normalize(description.text(), Form.NFKC);
Integer.parseInt(numberStr);

暫無
暫無

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

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