What's the difference between next() and nextLine() methods from Scanner class?

后端 未结 14 2048
你的背包
你的背包 2020-11-21 05:32

What is the main difference between next() and nextLine()?
My main goal is to read the all text using a Scanner which may be \"con

14条回答
  •  猫巷女王i
    2020-11-21 06:32

    • Just for another example of Scanner.next() and nextLine() is that like below : nextLine() does not let user type while next() makes Scanner wait and read the input.

       Scanner sc = new Scanner(System.in);
      
       do {
          System.out.println("The values on dice are :");
          for(int i = 0; i < n; i++) {
              System.out.println(ran.nextInt(6) + 1);
          }
          System.out.println("Continue : yes or no");
       } while(sc.next().equals("yes"));
      // while(sc.nextLine().equals("yes"));
      

提交回复
热议问题