Java Stdin and Stdout II

Sort by

recency

|

1220 Discussions

|

  • + 0 comments

    How this test case getting passed. Im confused can someone explain this.

    Compiler Message Success

    Input (stdin)

    2147483647 235345345345.234534 fsdfsdf sdf

    Expected Output

    String: fsdfsdf sdf Double: 2.3534534534523453E11 Int: 2147483647

  • + 0 comments

    This was my original solution:

    public class Solution {
    
        public static void main(String[] args) {
            try{
                Scanner scan = new Scanner(System.in);
                int i = scan.nextInt();
                double d =scan.nextDouble();
                scan.useDelimiter("\n");
                String s = scan.next();
                scan.close();
    
                System.out.println("String: " + s);
                System.out.println("Double: " + d);
                System.out.println("Int: " + i);            
            }catch(Error e){
                System.out.println("something went wrong:" +e);
            }
    
        }
    }
    

    I'm not really sure why this doesnt satisfy the test cases, although it prints as expected

    
    
  • + 0 comments

    Unlike the scanner.nextLine() method, the scanner.nextInt() method only consumes the integer part and leaves the enter or newline character in the input buffer.

    When the third scanner.nextLine() is called, it finds the enter or newline character still existing in the input buffer, mistakes it as the input from the user, and returns immediately.

    There are two ways to solve this problem. You can either consume the newline character after the scanner.nextInt() call takes place, or you can take all the inputs as strings and parse them to the correct data type later on.

  • + 0 comments

    Java is such a versatile and powerful language—it's amazing how it's still going strong after all these years. betguru247.net

  • + 1 comment
    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();
            
            scan.nextLine();
            String s = scan.nextLine();
    
            System.out.println("String: " + s);
            System.out.println("Double: " + d);
            System.out.println("Int: " + i);
            
            scan.close();
        }
    }