Java Scanner String input

前端 未结 5 1297
遇见更好的自我
遇见更好的自我 2020-12-05 11:02

I\'m writing a program that uses an Event class, which has in it an instance of a calendar, and a description of type String. The method to create an event uses a Scanner t

5条回答
  •  执笔经年
    2020-12-05 11:15

    If you use the nextLine() method immediately following the nextInt() method, nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty). So we read can read the empty space to another string might work. Check below code.

    import java.util.Scanner;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
    
            int i = scan.nextInt();
            Double d = scan.nextDouble();
            String f = scan.nextLine();
            String s = scan.nextLine();
    
    
            // Write your code here.
    
            System.out.println("String: " + s);
            System.out.println("Double: " + d);
             System.out.println("Int: " + i);
        }
    }
    

提交回复
热议问题