由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 刚刚开始学习java,麻烦帮我看一下我哪里错了行吗?谢谢
相关主题
anyone saw this on code?Help with Tomcat for Eclipse v1.03
问一个 java generic问题ant javac error in eclipse 3.0?
what is your opinion in this case?Problem on ANT,JAVA,JSP,JSPPrecompiler
Re: Problem on ANT,JAVA,JSP,JSPPrecompil关于char和int的问题
very weird problemHow big is the penalty for compile with debugging mode on?
java compilation question也问一个Eclipse的问题
help on this scope questionJAVA文本文件读写问题
怎么从键盘输入整数或float?QT is LGPL now
相关话题的讨论汇总
话题: string话题: strin话题: new
进入Java版参与讨论
1 (共1页)
s*******9
发帖数: 1
1
public class GetInputFromKeyboard1 {
public static void main(String[] args){
BufferedReader dataIn = new BufferedReader(new InputStreamReader(
System.in));
BufferedReader is = new BufferedReader(new InputStreamReader(System.
in));
int number ;
System.out.println("How Many Names You Wish To Enter:");
String name = "";
String strIn = "";
try{
strIn = is.readLine();
}catch(IOException e){
System.out.println(
Z****e
发帖数: 2999
2
when you define "number", no initial value is assigned
then you wrap the assignment to "number" by parseInt in a try block, but in
the catch block, you just printed an error message without ending the
program
so when a NumberFormatException happens, the control flow will still
continue to the for loop, but at this point, no value has been assigned to "
number" yet
the compiler sees this potential problem, would not let it pass compilation
solution: assign initial value 0, or return at the end of

【在 s*******9 的大作中提到】
: public class GetInputFromKeyboard1 {
: public static void main(String[] args){
: BufferedReader dataIn = new BufferedReader(new InputStreamReader(
: System.in));
: BufferedReader is = new BufferedReader(new InputStreamReader(System.
: in));
: int number ;
: System.out.println("How Many Names You Wish To Enter:");
: String name = "";
: String strIn = "";

c****7
发帖数: 13
3
In java, for local variable, you need to initialize it first before you use
it. Since you initialize it in the try{} block, it may not be initialized
because of exception is thrown out. Compiler will complain it.
m******t
发帖数: 2416
4
I have always hated it when the compiler does this. The JLS specifically
says
a variable is always assigned a default value when it's created. Well, if
it's
assigned a default value, then it's in a known state, and you can't say it's
not initialized, right?
I think it should've been a lint warning instead of a hard error.

in
"
compilation
,

【在 Z****e 的大作中提到】
: when you define "number", no initial value is assigned
: then you wrap the assignment to "number" by parseInt in a try block, but in
: the catch block, you just printed an error message without ending the
: program
: so when a NumberFormatException happens, the control flow will still
: continue to the for loop, but at this point, no value has been assigned to "
: number" yet
: the compiler sees this potential problem, would not let it pass compilation
: solution: assign initial value 0, or return at the end of

c*****t
发帖数: 1879
5
It doesn't matter that much in most cases though, since good code should
have 0 warning and 0 errors anyways.

's

【在 m******t 的大作中提到】
: I have always hated it when the compiler does this. The JLS specifically
: says
: a variable is always assigned a default value when it's created. Well, if
: it's
: assigned a default value, then it's in a known state, and you can't say it's
: not initialized, right?
: I think it should've been a lint warning instead of a hard error.
:
: in
: "

m******t
发帖数: 2416
6

Well just because a good driver should have no tickets on record
doesn't mean a cop can put me in jail for a night just for not wearing
the safety belt! ;-)

【在 c*****t 的大作中提到】
: It doesn't matter that much in most cases though, since good code should
: have 0 warning and 0 errors anyways.
:
: 's

s******e
发帖数: 493
7
Last time(a couple of years ago) I checked, no local primitive variable
should be assigned a default value by compiler. Did this change since then?
1 (共1页)
进入Java版参与讨论
相关主题
QT is LGPL nowvery weird problem
从文件读入数据得到的是bytesjava compilation question
其实JSP也不错help on this scope question
怎样截取网页
中多个

之间的内容?
怎么从键盘输入整数或float?
anyone saw this on code?Help with Tomcat for Eclipse v1.03
问一个 java generic问题ant javac error in eclipse 3.0?
what is your opinion in this case?Problem on ANT,JAVA,JSP,JSPPrecompiler
Re: Problem on ANT,JAVA,JSP,JSPPrecompil关于char和int的问题
相关话题的讨论汇总
话题: string话题: strin话题: new