0

I would really like to make a Method with a parameter of type Scanner which checks if the end of a line is reached. I already tried the following but is doesn't work at all. As parameter for in.hasNext() I already tried \r\n and \n as show below.

boolean eoln(Scanner in) {
    if (in.hasNext("\n")) {
        return true;
    }
    return false;
}

If it really isn't possible, I'll have to create a method boolean eoln(String src) with String src = in.nextLine(). Is there a substitute for nextLine() which does return the next line as a String, but does not let the Scanner advance past the returned line?

pabk
  • 3
  • 3
  • 2
    Since this sounds like xy-problem: please tell us why you think you need such a method and where you like to use it. – Tom Oct 11 '15 at 17:07
  • I know a different solution for the initial problem, but it is way more complex than the one described above. The question in short is: is there a token of the end of a line which can be detected by the Scanner as a Pattern, String, char, int etc.? And if not, is there a way to return a string without advancing (for example, creating `Scanner in2 = new Scanner(in)`)? – pabk Oct 11 '15 at 17:34
  • This desired approach sounds like a great way into a code smell, so please try to avoid it. So can you explain why you try to look for an eol, instead of reading a whole line and parsing that? – Tom Oct 11 '15 at 17:43

0 Answers0