User input String in Java !!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

User input String in Java !!!

Can't I get string input using this method..? //scan is my Scanner name String sentence = scan.nextLine (); I tried it but my program didn't asked me to give any input... What is problem with this one.. ? Can anyone tell me in details please :(

15th Jun 2017, 3:44 PM
Mazharul Islam Suzon
Mazharul Islam Suzon - avatar
27 Answers
+ 9
1. Did you define your scan variable like this? Scanner scan = new Scanner (System.in); 2. Does your code throw an error? If so, what is the error message?
15th Jun 2017, 3:57 PM
Tashi N
Tashi N - avatar
+ 9
Could you publish your code at the playground and add a link to it here?
15th Jun 2017, 4:01 PM
Tashi N
Tashi N - avatar
+ 2
It's not reserved I see, that's my fault. The Sololearn IDE usually indicates reserved keywords in blue but I see now it only made it blue because it started with a capital letter. My apologies, I'll subtract my comment
15th Jun 2017, 4:55 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
@Mazharual here's a workable version of your code. When you run it and it requests input then separate your inputs with a space to enter multiple inputs (e.g 1.0 2.0 Hello) https://code.sololearn.com/c09ITW62EVgm/?ref=app
15th Jun 2017, 5:05 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
I just made your variables lowercase and commented out your package at the top of your code.
15th Jun 2017, 5:26 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
It was confusing for me too @Mazharul but I see now that nothing was really wrong with your variable names. The only problem your code had was that package you included. Why I got confused was because the variable names were in blue.
15th Jun 2017, 6:07 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
No, you use the package keyword to make a package of the code you're creating and then save it as a java file. Then in your main code you can import that package with the import keyword. Example (Java): import java.util.* //imports all classes of the util package. //Show when you must not use the package keyword with your main code
15th Jun 2017, 7:06 PM
Ghauth Christians
Ghauth Christians - avatar
+ 1
I read that you shouldn't use the same scanner for both Strings and numbers. I don't know that that's the problem, but I would try a second scanner for strings
15th Jun 2017, 4:31 PM
LordHill
LordHill - avatar
+ 1
Ohh I see.. Thanks a lot brother for your kind information :)
15th Jun 2017, 4:33 PM
Mazharul Islam Suzon
Mazharul Islam Suzon - avatar
15th Jun 2017, 4:38 PM
Nanda Balakrishnan
+ 1
thanks bro@ gavin
15th Jun 2017, 4:44 PM
Nanda Balakrishnan
+ 1
Hi Mazaharul, both are used to read string alone. if u want to read int then use nextInt() ,nextFloat() for float and nextDouble() for double.
15th Jun 2017, 5:04 PM
Nanda Balakrishnan
15th Jun 2017, 5:13 PM
Nanda Balakrishnan
+ 1
then use nextLine() bro
15th Jun 2017, 5:16 PM
Nanda Balakrishnan
+ 1
oh ok
15th Jun 2017, 5:17 PM
Nanda Balakrishnan
0
Yes.. Defined like that but there is no error message.. while I run it just execute the program
15th Jun 2017, 4:00 PM
Mazharul Islam Suzon
Mazharul Islam Suzon - avatar
0
@Tashi N I'm publishing then will add the link here.. Thanks for your interest :)
15th Jun 2017, 4:19 PM
Mazharul Islam Suzon
Mazharul Islam Suzon - avatar
0
Joker yes I did.. my scanner taking int,double,float but not string in sequenc
15th Jun 2017, 4:21 PM
Mazharul Islam Suzon
Mazharul Islam Suzon - avatar
15th Jun 2017, 4:29 PM
Mazharul Islam Suzon
Mazharul Islam Suzon - avatar
0
Brother Gavin, I tried it in my PC with NetBeans IDE :(
15th Jun 2017, 4:46 PM
Mazharul Islam Suzon
Mazharul Islam Suzon - avatar