Java Stdin and Stdout II

  • + 4 comments

    sir i don understand

    • + 1 comment

      If it is placing the cursor in the same line then whay does i take only one word from the input line and why not the whole line

      • + 1 comment

        hi , I think you are using next() function ,so it receives first word only .It doesnot allow space. If you use nextLine(),you will get problem of not taking input from user(doesnot scan from keyboard).One point we should notice that initialy cursor was in the previous line from which it takes input .If you dont clear buffer memory it takes that as an input .So before using nextline() function to receive string. annonymously call nextline() function THen you will get expected output

        • + 0 comments

          here is solution of problem Java Stdin and Stdout second https://solution.programmingoneonone.com/2020/06/hackerrank-java-stdin-and-stdout-solution-second.html

    • + 15 comments

      Where do you not understand? please explain your problem to get your solution;

      Here, Input Format:

      There are three lines of input:

      The first line contains an integer. The second line contains a double. The third line contains a String.

      Here is main code:

      int i = scan.nextInt();
      double d = scan.nextDouble();
      scan.nextLine();
      String s = scan.nextLine();
      
      • + 7 comments

        why we put this (scan.nextLine();)before string ? why this didnt like int or double ? can you explain please .

        • + 1 comment

          yes exactly why??

          • + 4 comments

            Because int,double etc are primitive data types, where as String is not datatype it's a class.

            • + 0 comments

              ok,Can you elobarate??

            • + 2 comments

              Can you please give some refrence where we can read or learn more about it or else if you can tell us the reason it will be great?

              • + 1 comment

                A great explanation is contained in the book Absolute Java page 83 " Pitfall: Dealing with the line terminator,'\n'.

                Im sure by now you must understand why nextLine() is used twice, in order to get rid of the empty string after invoking nextInt(), but this comment is for anyone else who reads your comment and is also interested in finding a refrence to read about this problem more.

                • + 0 comments

                  Thank you very much about the book reference.

              • + 0 comments

                geeks for geeks where you every thing get related this.

            • + 0 comments

              thank you

            • + 0 comments

              jbj

        • + 6 comments

          Because scan.nextLine() read one line of input,while scan.nextInt () reads only one integer. Therefore, in the second line , it would still be necessary to read the line break and the carriage return (\ n \ r), that is, this additional string must be read in order to store the next line to store it in s.

          • [deleted]
            + 0 comments

            thanx :)

          • + 1 comment

            thanks a lot :)

          • + 0 comments

            thankyou you solved my query so simply

          • [deleted]
            + 0 comments

            Thanks a lot :)

          • + 0 comments

            thanks

          • + 0 comments

            i was not able to understand fromm previous comments,thanks am_castillo_men for simplyfying things ..

        • + 0 comments

          As explained before, scan.nextline(); reads from the next buffer. So as previous line was reading an 'int', it has stored the key it used to read in buffer. Now when nextLine(); execures it read it as an empty line and hence we are returned with no result.

        • + 0 comments

          to clear the buffer

        • + 0 comments

          scan.nextLine(); is used to clear the buffer.

        • + 0 comments

          because if we type int value then we press enter after typing the double, we also press enter.but if we dont type (scan.nextLine();) the last enter will be updated as the char value. so we need to type the 'scan.nextLine()' to get rid of that enter to be updated as the char value!!! I hope you can understand.

        • + 2 comments

          due to double d= scan.nextDouble(); the pointer of scanner remaines there at the end of the double so we need to shift it to the starting point of our string so we use that.

          • + 0 comments

            thank you

          • + 0 comments

            then why don't we use after scanning int

      • + 0 comments

        why do we need "scan.nextLine();" for..?

      • + 0 comments

        You are not explaining properly.......I think you might have copied it from somewhere.

      • + 0 comments

        Thx

      • + 1 comment

        this works! thankyou!!

        • + 1 comment

          I am getting a wrong answer on the double..

          • + 0 comments

            try it :- scan.nextLine(); after double so that when the next line is entered that contains string can be taken into the input buffer otherwise the double is continued for the next and it doesn't find the double value intead it's a string .It depends on editor also

      • + 1 comment

        Why their is a need to put Scan.nextLine();before getting the string value

        • + 1 comment

          nextLine() methods used for getting String inputs. ... Also, next() places the cursor in the same line after reading the input. nextLine() reads input including space between the words it reads till the end of line \n.

          • + 1 comment

            As you said nextLine() methods used for getting String inputs. ...right but similarly to get **char* input what we should use ???specially to be used at array //arr[i]=nextChar(); ===> this command not working please suggess any command to get char as input from user.....

            • + 0 comments

              Scanner sc=new Scanner(System.in); char c=sc.next().CharAt(0);

      • [deleted]
        + 0 comments

        why are we writing scan.nextLine(); after double d = scan.nextDouble();

      • + 0 comments

        i cant understand why we are using scan.nextLine(); before declaration of the String. Although String is the class but then to i cant understand the reason.

      • + 0 comments

        why you use scan.nextLine(); before String s = .......... line

      • + 1 comment

        techsajib bro why are you using scan.nextLine(); before String s=scan.nextLIne();

        • + 1 comment

          Because you cant do the input of a string after getting an integer.so when you try the input of an integer then you hit the 'Enter' then this enter will move to the variable as a string input. And you will not be able to get the input of the Actual String input. thats why there is " scan.nextLine(); ". Hope you understand! All the very best and enjoy programming.

          • + 0 comments

            Thankyou, was able to submit it because of your help.

      • + 0 comments

        thanks

      • + 0 comments

        scan.nextLine(); sir why did you use this

      • + 0 comments

        thankyou

      • + 0 comments

        not working

    • + 0 comments

      ok ok

    • + 0 comments

      basically the function of nextline() is to place the cursor at next line explicitly(forcefully) otherwise the enter key press by us would be assigned in string ,ie string would remain blank.