Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added formatting to CSVReader #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/scala/CSVReader/CSVReader.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package csvreader

/** CSVReader
*
*
*/
object CSVreader extends App{
println("Reading CSV file...")

val file = io.Source.fromFile("people.csv")

val headers = Array("Name", "Age", "State")

//Formatting to divide headers for each item in the CSV into a padded row.
println(f"${headers(0)}%-10s ${headers(1)}%-10s ${headers(2)}%-10s")

for (line <- file.getLines) {

var values = line.split(",")
// line.split() will split a string into an array based on a delimiter.
// E.G line.split("+") would split the string: 'Tim+40+Texas' into this array: [Tim, 40, Texas]

println(f"${values(0)}%-10s ${values(1)}%-10s ${values(2)}%-10s")
// Divides each CSV column into a padded row.
// Same thing as this:
// println(values(0) + ", " + values(1) + ", " + values(2))
// ^You can uncomment that line and it'll print the same thing without formatting(and with commas)
}
}
13 changes: 0 additions & 13 deletions src/main/scala/csv/CSVReader.scala

This file was deleted.