split的用法看一下jdk文档.它还有举例子的. 它里面的参数是个正则表达式. public String[] split(String regex)The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result : { "boo", "and", "foo" } o { "b", "", ":and:f" }
多谢believefym(feng),你改后的程序没问题,但是我写了一个跟它很相似的方法,却有错,可不可以帮我查一下? 我想指定一个i,从一行int中找出第i个值返回: public static Integer[] readAInt(int i) { try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); String[] tmp = input.split(" +"); int[] a = new int[tmp.length]; a[i] = Integer.parseInt(tmp[i]); }catch(IOException e) { System.out.println("read a integer has some problem!"); } return a; }
public static int[] readAInt() { try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String input = br.readLine(); String[] tmp = input.split(" +"); int[] a = new int[tmp.length]; for (int i = 0; i < a.length; i++) a[i] = Integer.parseInt(tmp[i]); return a; } catch (IOException e) { System.out.println("read a integer has some problem!"); return null; } // return a; }
你的a是int[],但函数返回的是Integer[] 一个是基本类型,一个是类
你要返回一个值输出就不要返回一个数组了,直接输出arrayName[index]就行了 public static int readAInt(int j) { try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String input = br.readLine(); String[] tmp = input.trim().split(" +"); int[] a = new int[tmp.length]; for (int i = 0; i < a.length; i++) a[i] = Integer.parseInt(tmp[i]); return a[j]; } catch (IOException ioe) { System.out.println("read a integer has some problem!"); return -1; }catch(Exception e){ e.printStackTrace(); return -1; } // return a; }
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
String input = br.readLine();
String tmp = input.split(" +");
i = new int[tmp.length];
for(int j=0; j<i.length; j++)
i[j] = Integer.parseInt(tmp[i]);
}catch...直接一行输入不就行了,以空格隔开,或者你要一个一个输的话来个for循环一下,个数到了退出继续执行程序不就行了
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
String s,s1;
s=in.readLine();
s1=in2.readLine();
System.out.println(s);
System.out.println(s1);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
split就是把字符串分割放到数组里,参数是分隔符,这里的" +"是一个正则表达式,表示1个或多个空格(+表示一个或多个,前面有个空格就表示一个或多个空格了)
改成
i[j] = Integer.parseInt(tmp[j]);
它里面的参数是个正则表达式.
public String[] split(String regex)The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
我想指定一个i,从一行int中找出第i个值返回:
public static Integer[] readAInt(int i)
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
String[] tmp = input.split(" +");
int[] a = new int[tmp.length];
a[i] = Integer.parseInt(tmp[i]);
}catch(IOException e)
{
System.out.println("read a integer has some problem!");
}
return a;
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String input = br.readLine();
String[] tmp = input.split(" +");
int[] a = new int[tmp.length];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tmp[i]);
return a;
} catch (IOException e) {
System.out.println("read a integer has some problem!");
return null;
}
// return a;
}
一个是基本类型,一个是类
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String input = br.readLine();
String[] tmp = input.trim().split(" +");
int[] a = new int[tmp.length];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tmp[i]);
return a[j];
} catch (IOException ioe) {
System.out.println("read a integer has some problem!");
return -1;
}catch(Exception e){
e.printStackTrace();
return -1;
}
// return a;
}