In Java, when we try to print a Hardcoded String like, “abcnabc”, we observe a line change after the first abc but when we take same input via sc.next() or sc.nextLine() it prints out ‘n’ as the part of string itself without any line change.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = "abcnabc";
String s2 = sc.nextLine();
System.out.println(s1);
System.out.println(s2);
}
}
Input: abcnabc
Output: abc
abc
abcnabc
Shouldn’t the input String also have a line change when printed back?
New contributor
Digvijay Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.