We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
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.
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.
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.
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.
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.
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
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.
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.....
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.
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.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Java Stdin and Stdout II
You are viewing a single comment's thread. Return to all comments →
sir i don understand
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
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
here is solution of problem Java Stdin and Stdout second https://solution.programmingoneonone.com/2020/06/hackerrank-java-stdin-and-stdout-solution-second.html
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:
why we put this (scan.nextLine();)before string ? why this didnt like int or double ? can you explain please .
yes exactly why??
Because int,double etc are primitive data types, where as String is not datatype it's a class.
ok,Can you elobarate??
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?
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.
Thank you very much about the book reference.
geeks for geeks where you every thing get related this.
thank you
jbj
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.
thanx :)
thanks a lot :)
thankyou you solved my query so simply
Thanks a lot :)
thanks
i was not able to understand fromm previous comments,thanks am_castillo_men for simplyfying things ..
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.
to clear the buffer
scan.nextLine(); is used to clear the buffer.
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.
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.
thank you
then why don't we use after scanning int
why do we need "scan.nextLine();" for..?
You are not explaining properly.......I think you might have copied it from somewhere.
Thx
this works! thankyou!!
I am getting a wrong answer on the double..
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
Why their is a need to put Scan.nextLine();before getting the string value
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.
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.....
Scanner sc=new Scanner(System.in); char c=sc.next().CharAt(0);
why are we writing scan.nextLine(); after double d = scan.nextDouble();
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.
why you use scan.nextLine(); before String s = .......... line
techsajib bro why are you using scan.nextLine(); before String s=scan.nextLIne();
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.
Thankyou, was able to submit it because of your help.
thanks
scan.nextLine(); sir why did you use this
thankyou
not working
ok ok
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.