Saturday, 17 August 2013

Java Scanner delimiter doesn't split the last section

Java Scanner delimiter doesn't split the last section

/* txt file
Rolling Stone#Jann Wenner#Bi-Weekly#Boston#9000
Rolling Stone#Jann Wenner#Bi-Weekly#Philadelphia#8000
Rolling Stone#Jann Wenner#Bi-Weekly#London#10000
The Economist#John Micklethwait#Weekly#New York#42000
The Economist#John Micklethwait#Weekly#Washington#29000
Nature#Philip Campbell#Weekly#Pittsburg#4000
Nature#Philip Campbell#Weekly#Berlin#6000
*/
public class Zines {
public static void main(String[] args) throws
FileNotFoundException {
Scanner input = new Scanner(new File("txt.file"));
input.useDelimiter("#");
while(input.hasNext()) {
String title = input.next();
String author = input.next();
String publisher = input.next();
String city = input.next();
String line = input.nextLine();
//int dist = Integer.valueOf(line);
System.out.println(line);
}
}
}
Output is: "#9000 "#8000 "#10000 "#42000 "#29000 "#4000 "#6000
Refrain from the quotations marks! The question here is why does the #'s
still appear after using the delimiter?

No comments:

Post a Comment