簡體   English   中英

為什么 Integer.parseInt 在我的 java 代碼中不起作用?

[英]Why is the Integer.parseInt not working in my java code?

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class MainProgram {
   public static void main(String[] args ) throws IOException{
    String seconds = " ";

     Scanner sc2 = null;
        try {
            sc2 = new Scanner(new File("/Users/mohammadmuntasir/Downloads/customersfile.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();  
        }

        boolean first = true;
        while (sc2.hasNextLine()) {
                Scanner s2 = new Scanner(sc2.nextLine());    
            while (s2.hasNext()) {
                String s = s2.next();
                if (first == true){
                    seconds = s;
                    first = false;
                }

            }
        }
        System.out.println(Integer.parseInt(seconds)); // causes ERROR?

     }
 }

我正在嘗試從單獨位於第一行的文本文件中讀取一個數字。 我創建了一個名為 seconds 的整數,它將接收第一個數字並將被解析為一個整數。 但我總是收到一個數字異常錯誤,而且我無法解析它。 當我將 s 顯示為字符串時,它會顯示一個數字,旁邊沒有空格。 誰能解釋為什么會發生這種情況?

這是堆棧跟蹤:

Exception in thread "main" java.lang.NumberFormatException: For input string: "300" 
  at java.lang.NumberFormatException.forInputString(NumberFormatE‌xception.java:65) 
  at java.lang.Integer.parseInt(Integer.java:580) 
  at java.lang.Integer.parseInt(Integer.java:615) 
  at MainProgram.main(MainProgram.java:29)

如果異常消息真的是這樣的:

 java.lang.NumberFormatException: For input string: "300"

然后我們開始進入真正晦澀的原因。

  • 這可能是同形文字的問題; 即 Unicode 字符看起來像一個字符但實際上是不同的字符。

  • 它可能是一個非打印字符。 例如 ASCII NUL ... 或 Unicode BOM(字節順序標記)字符。

我可以想到三種方法來診斷這個:

  1. 在調試器中運行您的代碼並在 parseInt 方法上設置斷點。 然后查看您嘗試解析的 String 對象,檢查其長度(比如 N)和字符數組中的前 N ​​個char值。

  2. 使用文件工具以字節為單位檢查文件。 (在 UNIX / Linux / MacOSX 上,使用od命令。)

  3. 添加一些代碼以將字符串作為字符數組獲取。 對於每個數組條目,將char轉換為int並打印結果數字。

所有三種方法都應該告訴您字符串中的確切字符是什么,這應該可以解釋為什么parseInt認為它們是錯誤的。


另一種可能是您錯誤地復制了異常消息。 當您將其納入問題時,堆棧跟蹤有點混亂......

使用十六進制編輯器查看您的輸入文件。 它可能以一些稱為Byte Order Markers 的“奇怪”十六進制代碼開頭。 這將解釋為什么 Exception 如此具有誤導性,因為 BOM 不會顯示在控制台上。

你能試試這個嗎,我有同樣的問題,Integer.ParseInt(String) 對我不起作用,我添加了 .trim() 並且它完美地工作:

int id_of_command;
        try {
             id_of_command = Integer.parseInt(id_of_Commands_str.trim());
            }
            catch (NumberFormatException e)
            {
             id_of_command = 0;
            }

如果您嘗試解析空間,則會導致問題。 請檢查您嘗試解析的seconds

Exception in thread "main" java.lang.NumberFormatException: For input string: " "
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:569)
    at java.lang.Integer.parseInt(Integer.java:615)
    at HelloWorld.main(HelloWorld.java:22)

另外嘗試捕獲異常並打印導致它的值。 像這樣的東西:

try{
      System.out.println(Integer.parseInt(seconds));
    }
    catch(Exception e){
      System.out.println("String value: '" + seconds + "'");
    }

你能用堆棧跟蹤更新問題嗎? 不確定您的文件中的內容是什么。

如果你確定你正在讀取一個整數,你可以使用 seconds.trim() 來修剪任何空格並解析它。

是否可以用這樣的內容附加您的代碼並給出您的答案:

    StringBuilder sb = new StringBuilder();
    for (char c : seconds.toCharArray())
    {
        sb.append((int)c).append(",");
    }
    System.out.println(sb);

您的字符串可以為空,或者您的字符串可能包含空格。 所以使用修剪然后解析。

暫無
暫無

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

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