Reputation: 1
I am working a java project and have hit a wall. I am trying to read in a file and create some objects with that file. A line will create an object of type TransportLine, followed by n lines that will create stations to populate the TransportLine data structure, before adding the TransportLine to the TransportSystem, n times. Here is a sample input:
TransportLine RD
Shady Grove;2100
Rockville;7800
Farragut North;1600
Metro Center;1200
Gallery Place;2400
Union Station;5300
Fort Totten;2050
Takoma;4800
Glenmont;0
TransportLine OR
Vienna;1100
East Falls Church;3100
Rosslyn;2730
Foggy Bottom;1600
Metro Center;3300
Smithsonian;2400
LEnfant Plaza;2000
Potomac Ave;5200
Stadium Armony;7200
New Carlton;0
TransportLine GR
GreenBelt;1100;
College Park;4100
Fort Tottem;3400
Columbia Heights;6200
Gallery Place;2700
LEnfant Plaza;5500
Anacostia;6240
Branch Ave;0
And here is my code:
public static void main(String[] args) throws FileNotFoundException{
TransportSystem ts = new TransportSystem();
String LINE = "TransportLine";
Scanner read = new Scanner(new File("WashingtonDCMetro.txt"));
String str;
String lineID, name;
int distance;
while(read.hasNext()){
str = read.nextLine();
List<String> items = Arrays.asList(str.split("\\s"));
TransportLine temp = new TransportLine(items.get(1));
System.out.println(items.toString());
while(read.hasNext("\\p{ASCII}*;\\p{ASCII}*")){
str = read.nextLine();
items = Arrays.asList(str.split(";"));
System.out.println(items.toString());
temp.addStation(new Station(items.get(0), temp.getLineID(), Integer.parseInt(items.get(1))));
}
ts.addLine(temp);
}
System.out.println(ts.toString());
}
The problem seems to lie in the nested while loop:
while(read.hasNext("\\p{ASCII}*;\\p{ASCII}*")){
I want to check the next line for any number of characters or whitespace followed by ";" followed by any number of digits, except it's not picking up on the second line of the input due to the space:
Shady Grove;2100
The first time this loop is stepped into is the third line of input. Any ideas how to modify the regex to catch the lines that aren't preceeded by TransportLine? ( I think i may have answered my own question here, off to try something, but help still very much appreciated... I am scratching my head )
Upvotes: 0
Views: 41
Reputation: 985
You can use this regex,
\w+( \w+)*;\d+
It will capture all lines except lines containing the string TransportLine
Upvotes: 1