JAVA- Integer.parseInt( str ) 给出 NumberFormatException,输入是表示整数的 str
JAVA- Integer.parseInt( str ) gives NumberFormatException, input is a str representing an integer
更新 -
chardetect
认为 srt 文件编码为 UTF-8,置信度为 1.0。我一直在阅读文件:
Files.readAllLines
设置为将其解释为 utf-8。在其他人的帮助下,我确定单个数字字符串的长度实际上是 2 ... 现在的诀窍是找出额外字符的来源。以下是十六进制转储的全文摘录:
00000000: efbb bf31 0d0a 3030 3a30 303a 3034 2c35 ...1..00:00:04,5
00000010: 3031 202d 2d3e 2030 303a 3030 3a30 362c 01 --> 00:00:06,
00000020: 3439 300d 0ae3 8199 e381 9fe3 8198 e381 490.............
00000030: 8ae3 8198 e381 b6e3 828a e381 95e3 818f ................
00000040: e381 b2e3 8293 0d0a 2d20 4c75 7069 6e20 ........- Lupin
00000050: 3230 3033 2070 7265 7365 6e74 7320 2d0d 2003 presents -.
00000060: 0a0d 0a32 0d0a 3030 3a30 303a 3036 2c35 ...2..00:00:06,5
00000070: 3030 202d 2d3e 2030 303a 3030 3a30 382c 00 --> 00:00:08,
00000080: 3032 340d 0a41 2053 7475 6469 6f20 4768 024..A Studio Gh
00000090: 6962 6c69 2046 696c 6d0d 0a0d 0a33 0d0a ibli Film....3..
000000a0: 3030 3a30 303a 3133 2c30 3437 202d 2d3e 00:00:13,047 -->
000000b0: 2030 303a 3030 3a31 352c 3132 360d 0a3c 00:00:15,126..<
000000c0: 666f 6e74 2063 6f6c 6f72 3d22 2338 3838 font color="#888
000000d0: 3838 3822 3ee3 81a1 e381 b2e3 828d e380 888">...........
000000e0: 80e3 8192 e382 93e3 818d e381 a7e3 81ad ................
000000f0: e380 82e3 8080 e381 bee3 819f e380 80e3 ................
00000100: 8182 e381 8ae3 8186 e381 ade3 8080 e382 ................
原问题:
我正在使用 java.nio.file.Files.readAllLines()
.
读取 .srt 文件
在 .srt 文件中,每个字幕都有一个数字行 - 一个索引字幕的整数。
正如捕获的那样,这一行是一个字符串。
当我使用 Integer.parsint( numberLineString )
时,我得到
java.lang.NumberFormatException
我已尽我所能解决此问题,方法是:
找一个非常具体的字幕所以我知道这不是由一些错误的字幕引起的,这些字幕可能有一个包含非数字字符的错误索引。
从索引号中删除任何可能的 \n 或 \r
打印传递给Integer.parsint()
的变量以验证它确实是索引字符串,代表一个整数。
如你所见,抛出的异常甚至承认它抛出了输入字符串“1”的异常
Exception in thread "main" java.lang.NumberFormatException: For input string: "1"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:658)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at japanese.engine.kana.JPSubsParser.parseTitles(JPSubsParser.java:218)
at japanese.engine.kana.srtManager.main(srtManager.java:25)
我不知所措 - 任何帮助将不胜感激。
这是正在读取的文件的摘录:
1
00:00:04,501 --> 00:00:06,490
すたじおじぶりさくひん
- Lupin 2003 presents -
2
00:00:06,500 --> 00:00:08,024
A Studio Ghibli Film
3
00:00:13,047 --> 00:00:15,126
<font color="#888888">ちひろ げんきでね。 また あおうね りさ</font>
<font color="#8888FF">Good Luck, Chihiro. We'll meet again</font>
这里是相关的Java代码:
public int parseTitles( Map<String, ArrayList<String>> subsMap ) {
/* Setup, get iterator for the Map
* Instantiate a charMatch object for checking for the various characters */
Iterator<String> keyIter = subsMap.keySet().iterator();
JPcharMatch charMatch = new JPcharMatch();
// Loop through the map
int tempCounter = 0;
while (keyIter.hasNext() && tempCounter == 0) {
//get the current title
ArrayList<String> titleLines = subsMap.get(keyIter.next());
String trimmedLine0 = titleLines.get(0).replace("\n", "").replace("\r", "").replace("\"", "");
int indexLineInt = Integer.parseInt( trimmedLine0 );
public class srtManager {
public static void main( String[] args ) {
Path subsFilePath = Paths.get("/Volumes/Multimedia/Coding/experimenting_srts/Sen to Chihiro Kanji+Hir+Eng.srt");
SubsFileDAO subsFileDAO = new SubsFileDAO(subsFilePath);
List<String> fileLines = subsFileDAO.getLinesList();
JPSubsParser parser = new JPSubsParser( fileLines );
Map<String, ArrayList<String>> subsMap = parser.getSubsMap();
// TODO analyze to detect kanji lines and store.
parser.parseTitles(subsMap);
}
}
您试图将文件开头的 BOM 解释为数字。
您可以在解析之前手动删除它:
String BOM = new String(new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf});
int indexLineInt = Integer.parseInt( trimmedLine0.replaceAll(BOM, ""));
或者,BOMInputStream 等解决方案将从提供的 InputStream
中透明地删除 BOM
更新 -
chardetect
认为 srt 文件编码为 UTF-8,置信度为 1.0。我一直在阅读文件:
Files.readAllLines
设置为将其解释为 utf-8。在其他人的帮助下,我确定单个数字字符串的长度实际上是 2 ... 现在的诀窍是找出额外字符的来源。以下是十六进制转储的全文摘录:
00000000: efbb bf31 0d0a 3030 3a30 303a 3034 2c35 ...1..00:00:04,5
00000010: 3031 202d 2d3e 2030 303a 3030 3a30 362c 01 --> 00:00:06,
00000020: 3439 300d 0ae3 8199 e381 9fe3 8198 e381 490.............
00000030: 8ae3 8198 e381 b6e3 828a e381 95e3 818f ................
00000040: e381 b2e3 8293 0d0a 2d20 4c75 7069 6e20 ........- Lupin
00000050: 3230 3033 2070 7265 7365 6e74 7320 2d0d 2003 presents -.
00000060: 0a0d 0a32 0d0a 3030 3a30 303a 3036 2c35 ...2..00:00:06,5
00000070: 3030 202d 2d3e 2030 303a 3030 3a30 382c 00 --> 00:00:08,
00000080: 3032 340d 0a41 2053 7475 6469 6f20 4768 024..A Studio Gh
00000090: 6962 6c69 2046 696c 6d0d 0a0d 0a33 0d0a ibli Film....3..
000000a0: 3030 3a30 303a 3133 2c30 3437 202d 2d3e 00:00:13,047 -->
000000b0: 2030 303a 3030 3a31 352c 3132 360d 0a3c 00:00:15,126..<
000000c0: 666f 6e74 2063 6f6c 6f72 3d22 2338 3838 font color="#888
000000d0: 3838 3822 3ee3 81a1 e381 b2e3 828d e380 888">...........
000000e0: 80e3 8192 e382 93e3 818d e381 a7e3 81ad ................
000000f0: e380 82e3 8080 e381 bee3 819f e380 80e3 ................
00000100: 8182 e381 8ae3 8186 e381 ade3 8080 e382 ................
原问题:
我正在使用 java.nio.file.Files.readAllLines()
.
在 .srt 文件中,每个字幕都有一个数字行 - 一个索引字幕的整数。
正如捕获的那样,这一行是一个字符串。
当我使用 Integer.parsint( numberLineString )
时,我得到
java.lang.NumberFormatException
我已尽我所能解决此问题,方法是:
找一个非常具体的字幕所以我知道这不是由一些错误的字幕引起的,这些字幕可能有一个包含非数字字符的错误索引。
从索引号中删除任何可能的 \n 或 \r
打印传递给
Integer.parsint()
的变量以验证它确实是索引字符串,代表一个整数。
如你所见,抛出的异常甚至承认它抛出了输入字符串“1”的异常
Exception in thread "main" java.lang.NumberFormatException: For input string: "1"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:658)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at japanese.engine.kana.JPSubsParser.parseTitles(JPSubsParser.java:218)
at japanese.engine.kana.srtManager.main(srtManager.java:25)
我不知所措 - 任何帮助将不胜感激。
这是正在读取的文件的摘录:
1
00:00:04,501 --> 00:00:06,490
すたじおじぶりさくひん
- Lupin 2003 presents -
2
00:00:06,500 --> 00:00:08,024
A Studio Ghibli Film
3
00:00:13,047 --> 00:00:15,126
<font color="#888888">ちひろ げんきでね。 また あおうね りさ</font>
<font color="#8888FF">Good Luck, Chihiro. We'll meet again</font>
这里是相关的Java代码:
public int parseTitles( Map<String, ArrayList<String>> subsMap ) {
/* Setup, get iterator for the Map
* Instantiate a charMatch object for checking for the various characters */
Iterator<String> keyIter = subsMap.keySet().iterator();
JPcharMatch charMatch = new JPcharMatch();
// Loop through the map
int tempCounter = 0;
while (keyIter.hasNext() && tempCounter == 0) {
//get the current title
ArrayList<String> titleLines = subsMap.get(keyIter.next());
String trimmedLine0 = titleLines.get(0).replace("\n", "").replace("\r", "").replace("\"", "");
int indexLineInt = Integer.parseInt( trimmedLine0 );
public class srtManager {
public static void main( String[] args ) {
Path subsFilePath = Paths.get("/Volumes/Multimedia/Coding/experimenting_srts/Sen to Chihiro Kanji+Hir+Eng.srt");
SubsFileDAO subsFileDAO = new SubsFileDAO(subsFilePath);
List<String> fileLines = subsFileDAO.getLinesList();
JPSubsParser parser = new JPSubsParser( fileLines );
Map<String, ArrayList<String>> subsMap = parser.getSubsMap();
// TODO analyze to detect kanji lines and store.
parser.parseTitles(subsMap);
}
}
您试图将文件开头的 BOM 解释为数字。
您可以在解析之前手动删除它:
String BOM = new String(new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf});
int indexLineInt = Integer.parseInt( trimmedLine0.replaceAll(BOM, ""));
或者,BOMInputStream 等解决方案将从提供的 InputStream
中透明地删除 BOM