+ 2

Java : string input (line) and string concatenation

A String variable is already defined, take another line as input from user, then print predefined string + line input by user

12th Oct 2019, 1:31 AM
Tanaya Seth
Tanaya Seth - avatar
9 Réponses
+ 4
It will work if you add this line at the top of the code: import java.util.Scanner;
12th Oct 2019, 1:57 AM
voja
voja - avatar
+ 2
Tanaya Seth Can you post the code you tried? I typed the code you posted and mine works just fine.
12th Oct 2019, 3:05 AM
Odyel
Odyel - avatar
+ 1
Try the code in this way package start import java.util.Scanner; public class Hello{ public static void main(String[]args){ String s =,,"Hello"; Scanner sc = new Scanner (System.in); String s1 = sc.next(); System.out.println(s+s1) It succeeded for me
12th Oct 2019, 2:52 AM
Mohamed Izad
Mohamed Izad - avatar
+ 1
Alright it's running here. Weird. Not running on Eclipse. Thanks Mohamed Izad ! Thanks Kilowac! https://code.sololearn.com/c6w12mSIa230/?ref=app
12th Oct 2019, 3:12 AM
Tanaya Seth
Tanaya Seth - avatar
0
I tried this but not working: String s = "Hello"; Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); System.out.println(s+s1);
12th Oct 2019, 1:33 AM
Tanaya Seth
Tanaya Seth - avatar
0
Oh that I did. Still not working :(
12th Oct 2019, 2:11 AM
Tanaya Seth
Tanaya Seth - avatar
0
sc.next() takes the line input. But prints only the first word in the line after String s
12th Oct 2019, 2:54 AM
Tanaya Seth
Tanaya Seth - avatar
0
Dunno then
12th Oct 2019, 3:13 AM
Odyel
Odyel - avatar
0
I too use eclipse it runs better in neon version.
12th Oct 2019, 3:15 AM
Mohamed Izad
Mohamed Izad - avatar