Java stop reading after empty line

前端 未结 3 1891
你的背包
你的背包 2020-12-11 06:58

I\'m doing an school exercise and I can\'t figure how to do one thing. For what I\'ve read, Scanner is not the best way but since the teacher only uses Scanner this must be

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 07:44

    Here's the typical readline idiom, applied to your code:

    String[] text = new String[11]
    Scanner sc = new Scanner(System.in);
    int i = 0;
    String line;
    System.out.println("Please insert text:");
    while (!(line = sc.nextLine()).equals("")){
        text[i] = line;
        i++;        
    }
    

提交回复
热议问题