0
What is wrong in this code?
I am unable to read the input string https://code.sololearn.com/ca8A13A86A15/?ref=app
17 Answers
+ 1
Yes it works but, you should not comment
scan.nextLine()
You can also write it as
String s1 = scan.next()+scan.nextLine()
+ 1
What doesn't it do?
If I input two numbers and an enter, and then hit submit, it seems to work.
+ 1
Codes are working..
Pls add what is the problem you are getting with code? Do you getting wrong output? How you want perform this code?
+ 1
For 1st:
Input sample :
3 12 15 77
This works , here you can give input in separate line also..
For:4
int num1 = scan.nextInt();
double num2 = scan.nextDouble();
//scan.nextLine();
String s1 = scan.nextLine();
For this give, sample :
3 5.7 abcd efg
This works..
If there is no nextLine() after taking double value, then next input within same line is input to String.
If you put, nextLine(); after double input, you have input double and string values in separate lines like
3 5.7
abcdf egh
( you can give all 3 in separate lines..)..
Hope it helps...
+ 1
Its your choice to comment or not... but dealings are different(in those are 2 ways above I said)..
May be you are taking about specific to your problem but I said for general problems..
String s1 = scan.nextLine()+scan.nextLine();
Also works...๐
0
nextDouble` doesn't discard the '\n' character read after the double value. The '\n' is stuck in input stream and prevents reading of string <s1> value correctly.
double num2 = scan.nextDouble();
scan.nextLine(); // <-- add this
String s1 = scan.nextLine();
0
Please correct the two codes
0
It is not taking string input
0
I have given nextLine() but it is not taking input
0
It's because of SoloLearn: sololearn supports one user-input only. Try to run it in another compailer, then it'll work.
I've just tried with jvdroid and had no problems.
0
I have tried it in my laptop also
0
In this what do you want to say I did not understood
nextLine() is not for coming in the next line.
nextLine() method is used for reading the string input in java
For 1st:
Input sample :
3 12 15 77
This works , here you can give input in separate line also..
For:4
int num1 = scan.nextInt();
double num2 = scan.nextDouble();
//scan.nextLine();
String s1 = scan.nextLine();
For this give, sample :
3 5.7 abcd efg
This works..
If there is no nextLine() after taking double value, then next input within same line is input to String.
If you put, nextLine(); after double input, you have input double and string values in separate lines like
3 5.7
abcdf egh
( you can give all 3 in separate lines..)..
Hope it helps...
0
I have written the correct syntax even though it is not taking the string input why?
This is my doubt
0
What inputs you are giving and how you are giving those..(i think you know that here it need all inputs at once, . In laptop, you can give interactively..)
Elaborate it with sample..
My sample : 2 3.5 abc is working fine.
If you uncomment scan.nextLine(); (from my above sample code) then
2
3.5
Abc de
Works fine..
if you comment it, you must input double value and string in same line..
may you giving it in next line so that it reading \n char, and completes nextLine() method. hence it just empty string..
๐ถ๐Sravs๐๐ฅ
nextLine() reads entire current line from current position and input pointer goes to next line..
0
Ok fine if you comment scan.nextLine() ok it will take input in single line is your explanation
Did you know the scanner methods for integer input nextInt(),for double nextDouble()
First learn those methods those are not for same line,next line those are for taking inputs.
What inputs you are giving and how you are giving those..(i think you know that here it need all inputs at once, . In laptop, you can give interactively..)
Elaborate it with sample..
My sample : 2 3.5 abc is working fine.
If you uncomment scan.nextLine(); (from my above sample code) then
2
3.5
Abc de
Works fine..
if you comment it, you must input double value and string in same line..
may you giving it in next line so that it reading \n char, and completes nextLine() method. hence it just empty string..
๐ถ๐Sravs๐๐ฅ
nextLine() reads entire current line from current position and input pointer goes to next line..
0
I know about all those..
There i mean input which is in current line, and input which is in next line..(just not about nextline).
I clearly added with an example..
Again for this code:
int num1 = scan.nextInt();
double num2 = scan.nextDouble();
String s1 = scan.nextLine();
Sample input :
2 3.5 abcd efg
2 will be read into num1
3.5 will be read into num2
abcd efg will be stored in s1.
if sample is :
2
3.5
abcd efg
By this ,s1 not store 'abcd efg'. s1 contains ="" (empty)
but num1,num2 works fine.
int num1 = scan.nextInt();
double num2 = scan.nextDouble();
scan.nextline();
String s1 = scan.nextLine();
By this code, 2nd sample works fine..
" Did you know the scanner methods for integer input nextInt(),for double nextDouble()
First learn those methods those are not for same line,next line those are for taking inputs. "
thanks for the advise . i will learn again... Happy coding..