public static int parseInt(String s)
throws NumberFormatExceptionParses
the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method. Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.楼主您注意这句话:
The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-''\u002D') to indicate a negative value. 比如说:
String str1 = "ab";
String str2 = "12";
if(userOption=Integer.parseInt(str1.trim())){
不能运行通过
}
if(userOption=Integer.parseInt(str2.trim())){
运行成功
}
throws NumberFormatExceptionParses
the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method. Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.楼主您注意这句话:
The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-''\u002D') to indicate a negative value. 比如说:
String str1 = "ab";
String str2 = "12";
if(userOption=Integer.parseInt(str1.trim())){
不能运行通过
}
if(userOption=Integer.parseInt(str2.trim())){
运行成功
}
对于输入的不是数字的Case,可捕捉异常然后再处理。
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.NullPointerException
楼主只声明throws IOException,但NullPointerException不是IOException的子类
所以这个异常没有相应的处理方法,所以编译不过
要在throws里加上NullPointerException就没有问题了
要等有输入后才执行完吗?
将BufferedReader串在网络连接的一个Socket上
用一个线程不断循环readLine()
结果对方断开后,readLine()就疯狂地读进null
您有空看我上面的帖子
String实例化对象,初始化是什么?
例如:
String str1;
jvm初始化不会是 str1 = “0”;吧所以运行时会报错啦
可楼主说到的是NullPointerException,而不是NumberFormatExceptionParses哦~
我觉得问题是:
开始运行程序执行到这一行:s=br.readLine();
还没输入任何东西就到了下一行:userOption=Integer.parseInt(s.trim());
而这里由readLine()读到s里面的就是null我觉得要讨论的主要是BufferedReader.readLine()的工作问题……当然这个问题产生的麻烦可以处理异常来解决……
public Main() {
}
public static void main(String[] args) throws IOException{
int userOption;
String s;
//OperationMenu menu = new OperationMenu();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
try{
//menu.ShowMainMenu();
s=br.readLine();
userOption=Integer.parseInt(s.trim());
switch(userOption)
{
case 0 :
System.out.print("0");
break;
case 1 :
System.out.print("1");
break;
case 2 :
System.out.print("2");
break;
default :
System.out.print("default");
break;
}
}
catch(NumberFormatException e){
System.out.println("不是数字!");
}
}
}
报错:java.lang.NullPointerException怎么JAVA的输入流这么麻烦啊?我郁闷啊!
import java.util.Scanner;
................
Scanner scan=new Scanner(System.in);
...
int n=scan.nextInt();
n即是所获得数据。
在JAVA中关于流的操作必须放在try catch语句中进行异常的处理!
import java.io.*;
public class Main
{
public static void main(String[] args)throws IOException
{
int userOption;
String s;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
s=br.readLine();
userOption=Integer.parseInt(s.trim());
switch(userOption)
{
case 0 :
System.out.print("0");
break;
case 1 :
System.out.print("1");
break;
case 2 :
System.out.print("2");
break;
default :
System.out.print("default");
break;
}
}catch(NumberFormatException e){
System.err.println("不是数字!");
}catch(IOException ex){
ex.printStackTrace(System.err);
}
}
}