Java Scanner, Delimiters, and End of Line

java scanner multiple delimiters
java scanner exceptions
java read file line by line scanner
java scanner(file)
java scanner pattern
scanner java 8
java scanner standard input
scan nextstring

I have a data file with slash-delimited ints on multiple successive lines. Here's a little piece of code that I'd like to use to read and print all those ints:

import java.util.Scanner;
import java.io.File;

class Testing {
   public static void main (String[] args) throws Exception {
      Scanner file = new Scanner(new File("data.txt"));
      file.useDelimiter("/");
      while (file.hasNextInt())
         System.out.println(file.nextInt());
   }
}

Data file one looks like this:

1/2/3
4/5/6

Output from data file one looks like this:

1
2

Data file two looks like this:

1/2/3

Output from data file two looks like this:

1
2
3

Can someone explain why I'm getting this behavior? In particular, why does the first example ignore the presence of the "3" at the end of the first line of data?

Note: I know several "better" ways to get my desired behavior: read the whole line at once and use split() and parseInt(), read the whole line at once and turn it into a separate Scanner object, etc.. That's not my question. I'm trying to understand why this particular piece of code doesn't work as intended --- in particular, why I get less data when the file has more lines.

The last hasNextInt() will read the element with the carriage return character. i.e the element will be 3\r\n. Which is obviously not int, hence it will fail the test file.hasNextInt()

Scanner error at end of line, Indeed, using just a comma for a delimiter would not work. Scanner works by reading in all incoming data until the next occurrence of the 

When it reaches end of line 1 i.e after value 3, there is 'return character' which is not an integer. Your test of hasNextInt() starts failing from there onwards. So it only prints upto 3. i.e 1 2 3

Java Scanner, In this quick tutorial, we'll illustrate how to use the Java Scanner class – to read input, find and skip patterns with different delimiters. returns it except for any line separator at the end – in this case – the new line character.

The has next to the end of the line one hasNextInt() will return a false as it return 3\r\n so that causes the issue.

Simple solution can be -

public static void main (String[] args) throws Exception {
      Scanner file = new Scanner(new File("test.txt"));
      file.useDelimiter("/");
      while (file.hasNext())
         System.out.println(file.next());
   }

Output will now look like - 123456

Java Scanner useDelimiter(String pattern) method example, This java tutorial shows how to use the useDelimiter(String pattern) method of Scanner class of java.util package. This method sets the delimiter 

Scanning text with java.util.Scanner, File; public class TextReader { private static void readFile(String fileName) { try The Scanner breaks the contents of the File into tokens using a delimiter You could, for example, test for lines that end with a newline character or that end with​ 

java.util.Scanner.match java code examples, new Scanner(System.in); InputStream src;new Scanner(src); String src;new an unknown amount of inputs java and stop checking at the end of a line private List<GherkinLineSpan> getSpans(String delimiter) { List<GherkinLineSpan> 

How to read a file line by line in Java (Example), The Scanner class presents the simplest way to read a file line by line in Java. A Scanner breaks its input into tokens using a delimiter pattern, new Scanner(​new File("examplefile.txt")); // read until end of file (EOF) 

Comments
  • The next item in your 1st file is "3\n4" which isn't an int (so I assume hasNextInt returns false)
  • The delimiter is for splitting up the inputs. By using / you are no longer splitting up the whitespace, so the first file actually has an output of 1, 2, 3\n4 etc. I would read the entirety of the lines via #nextLine and follow up the result of that with a String#split call to get all your integers