java 如何使用bufferedreader在java中接受字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16869622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to accept string array in java using bufferedreader
提问by Yash Srivastava
public static void accept_name( String[] name, int[] r)
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader ab = new BufferedReader(isr);
r = new int[40];
name = new String[40];
for(int i=0;i<40;i++)
{
System.out.println("Enter the name of students");
name[i] = ab.readLine();
}
}
i am having a problem in name[i] = ab.readLine();
我在 name[i] = ab.readLine() 中遇到问题;
i don't understand what the problem is.
我不明白问题是什么。
采纳答案by Khaled.K
When using Input\Output stream, an IOException
is expected, define that it might throw that kind of exception:
使用 Input\Output 流时,需要IOException
定义它可能会抛出那种异常:
public static void accept_name (String[] name, int[] r) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader ab = new BufferedReader(isr);
r = new int[40];
name = new String[40];
for(int i=0;i<40;i++)
{
System.out.println("Enter the name of students");
name[i] = ab.readLine();
}
}
回答by Suresh Atta
Actually,If you mouseover on the error line ,the message there tells everything.
实际上,如果您将鼠标悬停在错误行上,则那里的消息会说明一切。
It's a compile time error.Asking for exception
handling.While executing that line there are chances to get the IOException
.
这是一个编译时错误。要求exception
处理。执行该行时,有机会获得IOException
.
So you have to handle
it by throwing in the method
signature or by catching it there itself.
因此,您必须handle
通过输入method
签名或将其捕获在那里来实现它。
Change your method to
将您的方法更改为
public static void accept_name( String[] name, int[] r)
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader ab = new BufferedReader(isr);
r = new int[40];
name = new String[40];
for(int i=0;i<40;i++)
{
System.out.println("Enter the name of students");
try {
name[i] = ab.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
回答by Aniket Thakur
You are getting name array in the function argument why are you initializing it again? Below is not required
您在函数参数中获取名称数组,为什么要再次初始化它?以下不需要
name = new String[40];
r = new int[40];
You code must be
你的代码必须是
public static void accept_name( String[] name, int[] r)
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader ab = new BufferedReader(isr);
for(int i=0;i<40;i++)
{
System.out.println("Enter the name of students");
try {
name[i] = ab.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and then you can call your function as
然后你可以调用你的函数
String[] name = new String[40];
//populate your name array
int[] r = new int[40];
//populate your r array
ClassName.accept_name(name,r);//Static function
also I don't see where you are using r.
我也没有看到你在哪里使用 r。
回答by Carlo Bertuccini
Readline throws an IOException so you should catch it or rethrow.
Readline 会抛出一个 IOException,因此您应该捕获它或重新抛出。
public static void accept_name(String[] name, int[] r) throws IOException {
[...]
}
Carlo
卡罗
回答by NULL
You could try this...
你可以试试这个...
public static void accept_name( String[] name, int[] r)
{
name = new String[40];
for(int i=0;i<40;i++)
{
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader ab = new BufferedReader(isr);
System.out.println("Enter the name of students");
name[i] = ab.readLine();
ab.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}