Reputation: 1
I’m having some problems with scanning and storing data with an input that looks like this:
34 56=22 67=81 75
95 34
95 33
95 32
The "=" delimiter separates 3 completely different coordinates. I have to put the last block of coordinates into an array but I can’t do it to the first 2 coordinates. Can someone help me please?
I created a while loop that looks like the one below. What I want to create out of this input is start coordinates (the first two numbers), the end coordinates (the next to numbers between “=” signs), and an array with the rest of coordinates that are parted with “\n”. My code only allows me to read the coordinates in the first line and store it in one coordinate start object. I want to part them into different objects/arrays.
Scanner in = new Scanner(System.in);
in.useDelimiter("=");
while(in.hasNext()){
Scanner coordinatesScanner = new Scanner(in.next());
int coordinateX = coordinatesScanner.nextInt();
int coordinateY = coordinatesScanner.nextInt();
start = new Coordinates(coordinateX,coordinateY);
out.printf("%d %d", start.x, start.y);
}
Upvotes: 0
Views: 636
Reputation:
You can change the delimiter with Scanner.useDelimiter
and use regex to accept = aswell as whitespaces as delimiters:
Scanner scanner = new Scanner(...);
scanner.useDelimiter("\\s|=");
Upvotes: 1