java.lang.NumberFormatException:对于输入字符串:“23”[复制
发布时间:2020-12-15 05:12:34 所属栏目:Java 来源:网络整理
导读:参见英文答案 What is a NumberFormatException and how can I fix it? ????????????????????????????????????9个 我给了23(看起来像一个正确的字符串)作为输入,但stil得到NumberFormatException.请指出我哪里出错了. PS我试图在codechef上解决“厨师和字符
参见英文答案 >
What is a NumberFormatException and how can I fix it? ????????????????????????????????????9个
我给了23(看起来像一个正确的字符串)作为输入,但stil得到NumberFormatException.请指出我哪里出错了. PS我试图在codechef上解决“厨师和字符串问题” 相关代码: Scanner cin=new Scanner(System.in); cin.useDelimiter("n"); String data=cin.next(); System.out.println(data); /* * @param Q no. of chef's requests */ String tempStr=cin.next(); System.out.println(tempStr);; int Q = Integer.parseInt(tempStr); 输出: sdfgsdg sdfgsdg 23 23 Exception in thread "main" java.lang.NumberFormatException: For input string: "23 " at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Chef.RegexTestHarness.main(RegexTestHarness.java:24) 完整计划: package Chef; // //TODO codechef constraints import java.util.Scanner; import java.lang.*; import java.io.*; //TODO RETURN TYPE class RegexTestHarness { public static void main(String args[]) { Scanner cin=new Scanner(System.in); cin.useDelimiter("n"); String data=cin.next(); System.out.println(data); /* * @param Q no. of chef's requests */ String tempStr=cin.next(); System.out.println(tempStr);; int Q = Integer.parseInt(tempStr); for(int i=0; i<Q; i++) { /* * @param s chef's request */ String s= cin.next();//request //void getParam() (returning multiple parameters problem) //{a b L R //where a:start letter //b: end lettert //L: minStartIndex //L<=S[i]<=E[i]<=R //R is the maxEndIndex //TODO transfer to main char a=s.charAt(0); char b=s.charAt(3); int L=0,R=0; /* * @param indexOfR in the request string s,we separate R (which is maxEndIndex of chef's * good string inside data string) * . To do that,we first need the index of R itself in request string s */ int indexOfR= s.indexOf(" ",5) +1; System.out.println("indexOfR is:" + s.indexOf(" ",5)); L= Integer.parseInt( s.substring(5,indexOfR - 2) ); //TODO check if R,L<10^6 R=Integer.parseInt( s.substring(indexOfR) ); //} ( end getparam() ) //----------------------------------- //now we have a b L R //String good=""; //TODO add other constraints (like L<si.....) here if(a !=b) { int startInd=data.indexOf(a,L),endInd=data.lastIndexOf(b,R); int output=0,temp; while((startInd<endInd)&&(startInd != (-1) ) && ( endInd != (-1) )) { temp = endInd; while((startInd<endInd)) { //good= good+ s.substring(startInd,endInd); output++; endInd=data.lastIndexOf(b,endInd); } startInd=data.indexOf(a,startInd); //TODO if i comment the line below,eclipse says tat the variable temp //(declared at line 68) is not used. Whereas it is used at 68 //(and 83,the line below) endInd=temp; } System.out.println(output); } }//end for cin.close(); } } 解决方法
您的String具有尾随空格.
int Q = Integer.parseInt(tempStr.trim()); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- spring security国际化及UserCache的配置和使用
- 如何将IEEE-11073 16位SFLOAT转换为Java中的简单
- java – 搜索WindowBuilder文档/ API
- java – 使用RESTEasy进行错误的特殊字符编码
- Selenium Webdriver实现截图功能的示例
- Java中byte、byte数组与int、long的转换详解
- java – hibernate没有自动更新@ElementCollecti
- java – mockito是否具有与jMock的国家相同的成语
- java – 如何将一个Map Entry添加到ArrayList?
- 初始化包含对象数组的Java对象实例
热点阅读