I'm trying to copy a scanner into a string and return the string exactly, and then add a new line to the end of the new string. so a scanner that equals This\nis a\ntest, would return a string that equals This\nis a\ntest\n.
My code so far
public static String scannerToString (Scanner scnr)
{
String string = "";
while (scnr.hasNextLine())
{
string = scnr.nextLine();
}
return string + "\\n";
}
I solved the problem using StringBuilder instead. Here is my new code.
StringBuilder result = new StringBuilder();
while (scnr.hasNextLine())
{
String string = scnr.nextLine();
result.append(string + "\n");
}
return result.toString();