0

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();
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • So you want to see the escapes in the `Scanner`'s contents (*i*.*e*., printing the `String` shows the escaped `\n`)? – Chai T. Rex Sep 26 '18 at 20:41
  • @ChaiT.Rex Yes, and then keep the content that escapes in the new string that is being returned. – Jacob Harris Sep 26 '18 at 20:44
  • This really sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you explain what you plan on using this for? For example, if you want to stick the resulting string in a JSON request then this is definitely not the approach you want to use. – that other guy Sep 26 '18 at 20:59

1 Answers1

0

From searching, here's a combination of Olathe's String inspection method converted to use a StringBuilder and lucasvc's Scanner contents retriever:

import java.util.Scanner;

public class InspectScanner {
  public static String inspect(final String str) {
    StringBuilder result = new StringBuilder("\"");
    for (int i = 0; i < str.length(); i++) {
      final char ch = str.charAt(i);
      if      (ch >= 128) result.append(String.format("\\u%04x", (int) ch));
      else if (ch == '"') result.append("\\\"");
      else if (ch >=  32) result.append(ch);
      else switch (ch) {
        case '\b':        result.append("\\b"); break;
        case '\f':        result.append("\\f"); break;
        case '\n':        result.append("\\n"); break;
        case '\r':        result.append("\\r"); break;
        case '\t':        result.append("\\t"); break;
        default:        final int iNext = i + 1;
                        if (iNext == str.length())
                          result.append(String.format("\\%o", (int) ch));
                        else {
                          final char nextCh = str.charAt(iNext);
                          result.append(String.format((nextCh >= '0' && nextCh <= '9') ? "\\%03o" : "\\%o", (int) ch));
                        }
      }
    }
    result.append("\"");
    return result.toString();
  }

  public static String getRemainingScannerContents(final Scanner scanner) {
    scanner.useDelimiter("\\A");
    return scanner.hasNext() ? scanner.next() : "";
  }

  public static final void main(final String[] args) {
    Scanner scanner = new Scanner("test\ntest\n12345");
    System.out.println(inspect(getRemainingScannerContents(scanner)));
  }
}

This produces on stdout:

"test\ntest\n12345"
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33