What is wrong in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is wrong in this code?

I am unable to read the input string https://code.sololearn.com/ca8A13A86A15/?ref=app

5th Apr 2021, 2:49 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
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()
10th Apr 2021, 3:57 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
+ 1
What doesn't it do? If I input two numbers and an enter, and then hit submit, it seems to work.
5th Apr 2021, 2:59 PM
Paul
Paul - avatar
+ 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?
5th Apr 2021, 7:28 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 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...
6th Apr 2021, 12:18 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 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...๐Ÿ˜‡
11th Apr 2021, 4:21 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
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();
5th Apr 2021, 3:34 PM
Ipang
5th Apr 2021, 4:01 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
0
Please correct the two codes
5th Apr 2021, 4:01 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
0
It is not taking string input
6th Apr 2021, 11:41 AM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
0
I have given nextLine() but it is not taking input
6th Apr 2021, 11:42 AM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
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.
7th Apr 2021, 8:23 AM
Sebastian Ahlborn
Sebastian Ahlborn - avatar
0
I have tried it in my laptop also
7th Apr 2021, 3:17 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
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...
7th Apr 2021, 3:19 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
0
I have written the correct syntax even though it is not taking the string input why? This is my doubt
7th Apr 2021, 3:20 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
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..
7th Apr 2021, 9:45 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
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..
8th Apr 2021, 12:59 AM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
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..
9th Apr 2021, 7:55 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ