Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2XB3FinalProject
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Schankula
2XB3FinalProject
Commits
de557659
Commit
de557659
authored
7 years ago
by
Winnie
Browse files
Options
Downloads
Patches
Plain Diff
Add FileProcessor Class.
- Reads file - Parses file to string variables.
parent
2ac85593
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/FileProcessor.java
+118
-0
118 additions, 0 deletions
src/FileProcessor.java
with
118 additions
and
0 deletions
src/FileProcessor.java
0 → 100644
+
118
−
0
View file @
de557659
import
java.io.BufferedReader
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.util.Scanner
;
// Testing ONLY
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* @author
* This class reads and parses files in the format of occurences.csv
* It provides methods to get chunks of data.
*
* Notes:
* - Processes one row at a time
* - Press enter for the next row.
* - Parsed data is in string format
* - Data will be converted to the appropriate type in a toADT() function
*/
public
class
FileProcessor
{
private
static
String
path
=
"src/occurrence.csv"
;
public
String
getPath
()
{
return
path
;
}
public
void
setPath
(
String
newPath
)
{
path
=
newPath
;
}
// Calls Parse automatically
private
static
void
read
()
{
FileReader
fr
;
BufferedReader
br
;
try
{
fr
=
new
FileReader
(
path
);
br
=
new
BufferedReader
(
fr
);
Scanner
s
=
new
Scanner
(
System
.
in
);
//Testing ONLY
String
currentLine
;
br
.
readLine
();
// Reads Past Field Names
while
((
currentLine
=
br
.
readLine
())
!=
null
)
{
System
.
out
.
println
(
currentLine
);
parse
(
currentLine
);
s
.
nextLine
();
//Testing ONLY for checking one line at a time
}
s
.
close
();
//Testing ONLY
br
.
close
();
fr
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
//"scientificName" in raw data heading = species name
private
static
void
parse
(
String
currentLine
)
{
String
[]
splitLine
=
currentLine
.
split
(
","
);
String
eventId
=
null
,
taxId
=
null
,
occurrenceId
,
latitude
,
longitude
,
month
=
null
,
year
=
null
,
day
=
null
,
individualCount
;
occurrenceId
=
splitLine
[
3
];
individualCount
=
splitLine
[
4
];
Pattern
patternEventId
=
Pattern
.
compile
(
"OP_ID (\\d+)"
);
Matcher
matchEventId
=
patternEventId
.
matcher
(
splitLine
[
7
]);
if
(
matchEventId
.
find
())
{
eventId
=
matchEventId
.
group
(
1
);
}
else
{
// TODO: Throw Exception?
System
.
out
.
println
(
"Could not parse eventId. String may be unique or missing."
);
}
Pattern
patternDate
=
Pattern
.
compile
(
"(\\d+)-(\\d+)-(\\d+)"
);
Matcher
matchDate
=
patternDate
.
matcher
(
splitLine
[
8
]);
if
(
matchDate
.
find
())
{
year
=
matchDate
.
group
(
1
);
month
=
matchDate
.
group
(
2
);
day
=
matchDate
.
group
(
3
);
}
else
{
// TODO: Throw Exception?
System
.
out
.
println
(
"Could not parse eventId. String may be unique or missing."
);
}
latitude
=
splitLine
[
9
];
longitude
=
splitLine
[
10
];
Pattern
patternTaxId
=
Pattern
.
compile
(
":(\\d+)"
);
Matcher
matchTaxId
=
patternTaxId
.
matcher
(
splitLine
[
11
]);
if
(
matchTaxId
.
find
())
{
taxId
=
matchTaxId
.
group
(
1
);
}
else
{
// TODO: Throw Exception?
System
.
out
.
println
(
"Could not parse TaxId. String may be unique or missing."
);
}
//Taxonomy Info [12-18] - Using a species ADT instead?
// At this point, adt constructor will be called to create the object.
// Testing ONLY Print lines
System
.
out
.
println
(
"Occurence Id:"
+
occurrenceId
);
System
.
out
.
println
(
"Ind. Count:"
+
individualCount
);
System
.
out
.
println
(
"event Id:"
+
eventId
);
System
.
out
.
println
(
"Year:"
+
year
);
System
.
out
.
println
(
"Month:"
+
month
);
System
.
out
.
println
(
"Day:"
+
day
);
System
.
out
.
println
(
"lat:"
+
latitude
);
System
.
out
.
println
(
"long:"
+
longitude
);
System
.
out
.
println
(
"tax Id:"
+
taxId
);
}
public
static
void
main
(
String
[]
args
)
{
read
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment