java add new line to string code example

Example 1: java string next line

// its \r\n
String a = "Hello, "
String b = "there!"
System.out.println(a+b);//returns Hello, there
System.out.println(a+"\r\n"+b);//returns Hello, 
//there! //it will be on the next line!

Example 2: add one character to string java

char ch='A';
String str="pple";
str= ch+str;   // str will change to "Apple"

Example 3: how to convert line into string java

File file = new File(fileName);
Scanner input = new Scanner(file);
List<String> list = new ArrayList<String>();

while (input.hasNextLine()) {
    list.add(input.nextLine());
}

//adds each line of a text file into an ArrayList

Tags:

Java Example