-1

How do I concatenate two lines dynamically using java?

String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
//Scanner scan = new Scanner(System.in);
String s2 = scan.nextLine();
//concatenates both the strings        
s = s+s2;
// s = s.concat(s2);
System.out.println(s);
scan.close();

The output should concatenate two complete lines. for ex. if
String s = "Hackerank" and String s1 = "is very good to learn coding",
The output should be like:

Hackerank is very good to learn coding

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Liswin
  • 158
  • 1
  • 9

2 Answers2

1

I've tried your code and it works perfectly may be you don't know how to use it. There is no need to concatenate with blank space first as the previous answer says. Just keep doing as you did it work, look at the screenshot: here is the result I got when I ran you code

with this code

import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        String s = "HackerRank "; //Scanner scan = new Scanner(System.in);
        try (Scanner scan = new Scanner(System.in)) {
            //Scanner scan = new Scanner(System.in);
            String s2 = scan.nextLine(); //concatenates both the strings
            s = s + s2; // s = s.concat(s2);
            System.out.println(s);
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Vivien SA'A
  • 727
  • 8
  • 16
-1

I don't see anything wrong with the code. The strings will be concatenated. But the output will be

Hackerankis very good to learn coding.

if you want the output to be as you described you should concatenate a white space like

s = s + " " + s2;