Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中获取StringBuffer的数组[]的输入_Java_Arrays_Stringbuilder - Fatal编程技术网

如何在java中获取StringBuffer的数组[]的输入

如何在java中获取StringBuffer的数组[]的输入,java,arrays,stringbuilder,Java,Arrays,Stringbuilder,我想为java中的用户获取一个StringBuffer数组的输入,但它不能正常工作: public class A { public static void main(String[] args) { B obj =new B(); Scanner sc= new Scanner(System.in); int l; System.out.println("enter the length of string ");

我想为java中的用户获取一个
StringBuffer
数组的输入,但它不能正常工作:

public class A {

    public static void main(String[] args) {

        B obj =new B();
        Scanner sc= new Scanner(System.in);
        int l;
        System.out.println("enter the length of string ");
        l=sc.nextInt();

        StringBuffer sb[]=new StringBuffer[l];


         for(int i=0;i<sb.length;i++) 
         {
             System.out.println("enter a string "+(i+1) +" : ");
             sb[i]=sb[i].append(sc.nextLine()); // in this line they will give error
         }


        obj.inputstring( sb);


    }
公共A类{
公共静态void main(字符串[]args){
B obj=新的B();
扫描仪sc=新的扫描仪(System.in);
int l;
System.out.println(“输入字符串长度”);
l=sc.nextInt();
StringBuffer sb[]=新的StringBuffer[l];

对于(inti=0;i您需要为每个sb[i]位置初始化StringBuffer对象

sb[i]=new StringBuffer(sc.nextLine());
请参阅javadoc,了解尝试此操作

  public static void main(String[] args) {

    //B obj =new B();
    Scanner sc = new Scanner(System.in);
    int l;
    System.out.println("enter the length of string ");
    l = sc.nextInt();
        sc.nextLine();

    StringBuffer sb[] = new StringBuffer[l];

    System.out.println("sb length==" + sb.length);
    for (int i = 0; i < sb.length; i++) {

        System.out.println("enter a string " + (i + 1) + " : ");
        sb[i] = new StringBuffer(sc.nextLine()); // use StringBuffer(value)
    }
    for(int j=0;j<sb.length;j++){

        System.out.println("string "+j+"="+sb[j]);
    }
    //for(int j=0;j<=sb.length;j++){}
    //System.out.println("sb=="+sb.toString());

}
publicstaticvoidmain(字符串[]args){
//B obj=新的B();
扫描仪sc=新的扫描仪(System.in);
int l;
System.out.println(“输入字符串长度”);
l=sc.nextInt();
sc.nextLine();
StringBuffer sb[]=新的StringBuffer[l];
系统输出println(“sb长度=”+sb长度);
for(int i=0;i对于(int j=0;j此
sb[i]=sb[i].append(sc.nextLine());
将给您一个
NullPointerException
,因为
sb[i]
null
在分配它之前,请使用
sb[i]=new StrinBuffer(sc.nextLine())
instead另外,正如前面所说,您应该使用
StringBuilder
instead我想从java中的用户那里获得一系列StringBuffer。@SyedMossawarHussainShah另一个注意事项是,如果您使用sc.nextInt()-您会发现您创建的第一个StringBuffer将立即被获取-您需要调用sc.nextLine();忽略nextInt()之后的行的“剩余部分”。为什么不使用支持
append()的
StringBuilder()
?您真的需要l+1扫描仪吗?一台扫描仪应该足以读取所有内容。我尝试了使用单台扫描仪。这会在循环中造成一些混乱。我正在检查它。检查我对问题的评论。谢谢您先生的帮助