2013年9月28日星期六

An array of cross-border issues

Error code is as follows:
Scanner in = new Scanner (System.in);
System.out.println (" Please enter the first sequence of length : " ) ;
int m = in.nextInt ();
char [] array = new char [m];
System.out.println (" Please enter the first sequence : " )
for (int i = 0; i array [i] = in.nextLine (). charAt (0);


---------------------------------------------- ----------------------------------
The result:
Enter the length of the first sequence :
5
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt (Unknown Source)


I novice , seeking god Weapon
------ Solution ----------------------------- ---------------

import java.util.*;

public class Test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入第一个序列的长度:");
int m=in.nextInt();
char []array=new char[m];
        System.out.println("请输入第一个序列:");
in.nextLine(); // 加入这句话 就可以了
for(int i=0;i<m;i++)
array[i]=in.nextLine().charAt(0);
}
}

You can look this article
http://blog.163.com/hellojavaj_v_m/blog/static/22553207720138811407968/
------ Solution -------------------------------------- ------
nextInt () Return will be entered after the first nextLine () capture
------ For reference only ------------ ---------------------------
length of the array is fixed and must be initialized
--- --- For reference only ---------------------------------------
in. nextLine () is an empty string , right

------ For reference only ---------------------------------- -----
in.nextLine (). charAt (0) there are problems
------ For reference only ------------------ ---------------------

+1
------ For reference only -------- -------------------------------



ah , thank
------ For reference only ------------------------------- --------


So that

没有评论:

发表评论