如何在Java中输入StringBuffer的array []

时间:2018-07-27 06:16:36

标签: java arrays stringbuilder

我想为Java中的用户输入related_id的数组,但是不能正常工作:

  Schema::create('related_posts', function (Blueprint $table) {
      $table->increments('id');
      $table->unsignedInteger('post_id');
      $table->unsignedInteger('related_id');
      $table->timestamps();

      $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
      $table->foreign('related_id')->references('id')->('posts')->onDelete('cascade');
  });

2 个答案:

答案 0 :(得分:1)

您需要为每个sb [i]位置初始化StringBuffer对象。

sb[i]=new StringBuffer(sc.nextLine());

Array initialization引用javadoc

答案 1 :(得分:-2)

尝试一下。

  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());

}