Structuralize Text Data & Value Replacement
【Question】
I use “for loop” to read in data from a text file using Scanner class.
But I'm currently stuck on a programming assignment which requires me to read in data from a text file and then process it. The file looks like this:
CS1 2012 Group 1
8
5,5,5,6,5,8,9,5,6,8, good, very good, excellent, good
7,7,8,7,6,7,8,8,9,7,very good, Good, excellent, very good
8,7,6,7,8,7,5,6,8,7 ,GOOD, VERY GOOD, GOOD, AVERAGE
9,9,9,8,9,7,9,8,9,9 ,Excellent, very good, very good, excellent
7,8,8,7,8,7,8,9,6,8 ,very good, good, excellent, excellent
6,5,6,4,5,6,5,6,6,6 ,good, average, good, good
7,8,7,7,6,8,7,8,6,6 ,good, very good, good, very good
5,7,6,7,6,7,6,7,7,7 ,excellent, very good, very good, very good
The first 2 lines are read in and then assigned to fields. The remaining lines are the ones I have to process. I've been told to use .useDelimiter("[]*(,)[]*") but so far I haven't really been able to put it to good use.
Once the data has been read in I have to convert the strings into integers using a switch statement and work out an average feedback score, e.g excellent = 5 very good = 4 good =3. Maybe I'm just not thinking clear but I've already spent about 5 hours trying to figure this out to no avail so any guidance would be appreciated. Thanks!
Here’s one response:
write the code in simple steps.
First make a loop and in the loop read a line and print it
continue until all the lines have been read and printed.
When that works, then worry about how to parse the contents of a line and use the data it contains.
【Answer】
This is a simple structured data computation. It’s complicated to do this in Java. You can use SPL (Structured Process Language) to processes structured data for Java and then integrate the SPL script with the Java application (See How to Call an SPL Script in Java). For your problem (guess certain data in uppercase should be in lowercase, so I made some adjustment), the SPL script is as below:
A |
|
1 |
=file("E:\\s.txt").read@n() |
2 |
=A1.to(3,) |
3 |
=A2.concat("\n") |
4 |
=A3.import@c() |
5 |
>A4.run(_11=case(_11,"average":2,"good":3,"very good":4,"excellent":5),_12=case(_12,"average":2,"good":3,"very good":4,"excellent":5),_13=case(_13,"average":2,"good":3,"very good":4,"excellent":5),_14=case(_14,"average":2,"good":3,"very good":4,"excellent":5)) |
6 |
=A4.fno().(A4.field(~).avg()) |
7 |
=A4.record(A6) |
A1: Read in data from s.txt and return it as a sequence of strings, where each line of the file is a member.
A2: Read in A1’s data from the 3rd line to the end.
A3: Split each member of A2’s sequence into a string separated by “\n”.
A4: Make each string member a record to return a table sequence.
A5: Modify values in the last 4 columns according to “excellent = 5, very good = 4, good =3, average=2”.
A6: Calculate average over each column and return results as a sequence.
A7: Append a row to A4 and populate A6’s members to the row.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL