Java Stdin and Stdout II

Sort by

recency

|

1178 Discussions

|

  • + 0 comments

    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.next();
            String s = scan.nextLine();
    
            // Write your code here.
    
            System.out.println("String: "+ s);
            System.out.println("Double: " + d);
            System.out.println("Int: " + i);
    }
    

    }

    The compiler can't read the string variable s in the code

    `

    
    
  • + 0 comments

    The compiler can't read the String variable "s" in this code:

    public class Solution {

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

    }

  • + 0 comments

    I think having to read the nextLine() twice is a bug?

  • + 0 comments

    The String cant read the input immediately after reading the nextDouble()-Because: the input field didn't move from the double input so we cant able to read string. If we have to move the cursor to read string we have to intialize a scan.nextLine()__" between Double and String" -- __ so that the code follows as below 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(); // Write your code here.

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
    

    }

  • + 0 comments

    it is the actual code cause
    afterusing the scan.nextInt(); or scan.nextDouble(); it looks that it is in nextline but its not so ....

    import java.util.Scanner;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
    
    
    
    
         int i = scan.nextInt();
    
        // Write your code here.
        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);
    
    }
    

    }