Skip to content

Commit

Permalink
Format Kotlin code
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a committed Mar 17, 2024
1 parent 4079a64 commit 2fabcb4
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ import javafx.scene.Scene
import javafx.stage.Stage

class KotlinFXMLFileTextCounterApplication : Application() {
@Throws(IOException::class)
override fun start(stage: Stage) {
val fxmlLoader =
FXMLLoader(
KotlinFXMLFileTextCounterApplication::class.java.getResource(
"kotlin-fxml-file-text-counter.fxml"
)
) // load FXML file resource
val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320
stage.title = "Text Counter from a File" // set the title of stage
stage.scene = scene // set the stage to a scene
stage.show() // show the stage
}
@Throws(IOException::class)
override fun start(stage: Stage) {
val fxmlLoader =
FXMLLoader(
KotlinFXMLFileTextCounterApplication::class
.java
.getResource("kotlin-fxml-file-text-counter.fxml")) // load FXML file resource
val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320
stage.title = "Text Counter from a File" // set the title of stage
stage.scene = scene // set the stage to a scene
stage.show() // show the stage
}

companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(
KotlinFXMLFileTextCounterApplication::class.java
) // launch the application by running the program
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(
KotlinFXMLFileTextCounterApplication::class
.java) // launch the application by running the program
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,54 @@ import javafx.scene.control.Label
import javafx.stage.FileChooser

class KotlinFXMLFileTextCounterController {
@FXML var selectFile: Button? = null // button to select file
@FXML var textCountResult: Label? = null // text counter output result
@FXML var textContent: String = "" // initialize empty text content string
@FXML var selectFile: Button? = null // button to select file
@FXML var textCountResult: Label? = null // text counter output result
@FXML var textContent: String = "" // initialize empty text content string

fun calculateTextCount(actionEvent: ActionEvent?) {
var textContent = "" // initialize empty text content string
val fileChooser = FileChooser() // create a new file chooser
fileChooser.title = "Select a file"
fileChooser.extensionFilters.addAll(
FileChooser.ExtensionFilter("Text Files", "*.txt")
) // choose .txt text files
val fileSelected = fileChooser.showOpenDialog(null) // show file dialog
if (fileSelected != null) {
try {
BufferedReader(FileReader(fileSelected)).use { reader ->
var line: String?
while ((reader.readLine().also { line = it }) != null) {
textContent += line + "\n" // read text from file every line
}
}
} catch (e: IOException) {
throw RuntimeException(e) // throw runtime exception
}
fun calculateTextCount(actionEvent: ActionEvent?) {
var textContent = "" // initialize empty text content string
val fileChooser = FileChooser() // create a new file chooser
fileChooser.title = "Select a file"
fileChooser.extensionFilters.addAll(
FileChooser.ExtensionFilter("Text Files", "*.txt")) // choose .txt text files
val fileSelected = fileChooser.showOpenDialog(null) // show file dialog
if (fileSelected != null) {
try {
BufferedReader(FileReader(fileSelected)).use { reader ->
var line: String?
while ((reader.readLine().also { line = it }) != null) {
textContent += line + "\n" // read text from file every line
}
}
val chars: Int = textContent.length // get the length of opened text file in characters
val words: Int =
textContent
.trim { it <= ' ' }
.split("\\s+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in words
val lines: Int =
textContent
.trim { it <= ' ' }
.split("\\r?\\n".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in lines
textCountResult!!.text =
"""
} catch (e: IOException) {
throw RuntimeException(e) // throw runtime exception
}
}
val chars: Int = textContent.length // get the length of opened text file in characters
val words: Int =
textContent
.trim { it <= ' ' }
.split("\\s+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in words
val lines: Int =
textContent
.trim { it <= ' ' }
.split("\\r?\\n".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in lines
textCountResult!!.text =
"""
Characters: ${String.format("%,d", chars)}
Words: ${String.format("%,d", words)}
Lines: ${String.format("%,d", lines)}
""".trimIndent() // update text counter output
}
"""
.trimIndent() // update text counter output
}

companion object {
var reader: BufferedReader? = null // initialize reader
}
companion object {
var reader: BufferedReader? = null // initialize reader
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ import javafx.scene.Scene
import javafx.stage.Stage

class KotlinFXMLTextCounterApplication : Application() {
@Throws(IOException::class)
override fun start(stage: Stage) {
val fxmlLoader =
FXMLLoader(
KotlinFXMLTextCounterApplication::class.java.getResource(
"kotlin-fxml-text-counter.fxml"
)
) // load FXML file resource
val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320
stage.title = "Text Counter" // set the title of stage
stage.scene = scene // set the stage to a scene
stage.show() // show the stage
}
@Throws(IOException::class)
override fun start(stage: Stage) {
val fxmlLoader =
FXMLLoader(
KotlinFXMLTextCounterApplication::class
.java
.getResource("kotlin-fxml-text-counter.fxml")) // load FXML file resource
val scene = Scene(fxmlLoader.load(), 480.0, 320.0) // set application size to 480x320
stage.title = "Text Counter" // set the title of stage
stage.scene = scene // set the stage to a scene
stage.show() // show the stage
}

companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(
KotlinFXMLTextCounterApplication::class.java
) // launch the application by running the program
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(
KotlinFXMLTextCounterApplication::class
.java) // launch the application by running the program
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@ import javafx.scene.control.Label
import javafx.scene.control.TextArea

class KotlinFXMLTextCounterController {
@FXML var textContent: TextArea? = null // text box area
@FXML var updateText: Button? = null // button to update entered text from text box
@FXML var textCountResult: Label? = null // text counter output result
@FXML var textContent: TextArea? = null // text box area
@FXML var updateText: Button? = null // button to update entered text from text box
@FXML var textCountResult: Label? = null // text counter output result

fun calculateTextCount(
actionEvent: ActionEvent?
) { // update a text count result when button is clicked
val chars: Int = textContent!!.length // get the length of text area in characters
val words: Int =
textContent!!.text
.trim { it <= ' ' }
.split("\\s+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of text area in words
val lines: Int =
textContent!!.text
.split("\\r?\\n".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of text area in lines
textCountResult!!.text =
"""
fun calculateTextCount(
actionEvent: ActionEvent?
) { // update a text count result when button is clicked
val chars: Int = textContent!!.length // get the length of text area in characters
val words: Int =
textContent!!
.text
.trim { it <= ' ' }
.split("\\s+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of text area in words
val lines: Int =
textContent!!
.text
.split("\\r?\\n".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of text area in lines
textCountResult!!.text =
"""
Characters: ${String.format("%,d", chars)}
Words: ${String.format("%,d", words)}
Lines: ${String.format("%,d", lines)}
""".trimIndent() // update text counter output
}
"""
.trimIndent() // update text counter output
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,66 @@ import javafx.stage.FileChooser
import javafx.stage.Stage

class KotlinFileTextCounterJavaFX : Application() {
@Throws(FileNotFoundException::class)
override fun start(primaryStage: Stage) {
val root = FlowPane()
val scene = Scene(root, 480.0, 320.0) // set the application size to 480x320
val selectFile = Button("Select file") // select the file from the dialog box
primaryStage.scene = scene // set the scene
primaryStage.title = "Text Counter Application" // set the title of stage
val textCountResult =
Label("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output
selectFile.onAction = EventHandler { event: ActionEvent? ->
var textContent = "" // initialize empty text content string
val fileChooser = FileChooser() // create a new file chooser
fileChooser.title = "Select a file"
fileChooser.extensionFilters.addAll(
FileChooser.ExtensionFilter("Text Files", "*.txt")
) // choose .txt text files
val fileSelected = fileChooser.showOpenDialog(null) // show file dialog
if (fileSelected != null) {
try {
BufferedReader(FileReader(fileSelected)).use { reader ->
var line: String?
while ((reader.readLine().also { line = it }) != null) {
textContent += line + "\n" // read text from a file every line
}
}
} catch (e: IOException) {
throw RuntimeException(e) // throw runtime exception
}
@Throws(FileNotFoundException::class)
override fun start(primaryStage: Stage) {
val root = FlowPane()
val scene = Scene(root, 480.0, 320.0) // set the application size to 480x320
val selectFile = Button("Select file") // select the file from the dialog box
primaryStage.scene = scene // set the scene
primaryStage.title = "Text Counter Application" // set the title of stage
val textCountResult =
Label("Characters: 0\nWords: 0\nLines: 0") // set the default text counter output
selectFile.onAction = EventHandler { event: ActionEvent? ->
var textContent = "" // initialize empty text content string
val fileChooser = FileChooser() // create a new file chooser
fileChooser.title = "Select a file"
fileChooser.extensionFilters.addAll(
FileChooser.ExtensionFilter("Text Files", "*.txt")) // choose .txt text files
val fileSelected = fileChooser.showOpenDialog(null) // show file dialog
if (fileSelected != null) {
try {
BufferedReader(FileReader(fileSelected)).use { reader ->
var line: String?
while ((reader.readLine().also { line = it }) != null) {
textContent += line + "\n" // read text from a file every line
}
val chars: Int = textContent.length // get the length of opened text file in characters
val words: Int =
textContent
.trim { it <= ' ' }
.split("\\s+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in words
val lines: Int =
textContent
.trim { it <= ' ' }
.split("\\r?\\n".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in lines
textCountResult.text =
"Characters: " +
String.format("%,d", chars) +
"\nWords: " +
String.format("%,d", words) +
"\nLines: " +
String.format("%,d", lines) // update text counter output
}
} catch (e: IOException) {
throw RuntimeException(e) // throw runtime exception
}
root.children.addAll(selectFile, textCountResult) // add all components to the stage
primaryStage.show() // show the stage
}
val chars: Int = textContent.length // get the length of opened text file in characters
val words: Int =
textContent
.trim { it <= ' ' }
.split("\\s+".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in words
val lines: Int =
textContent
.trim { it <= ' ' }
.split("\\r?\\n".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()
.size // get the length of opened text file in lines
textCountResult.text =
"Characters: " +
String.format("%,d", chars) +
"\nWords: " +
String.format("%,d", words) +
"\nLines: " +
String.format("%,d", lines) // update text counter output
}
root.children.addAll(selectFile, textCountResult) // add all components to the stage
primaryStage.show() // show the stage
}

companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(
KotlinFileTextCounterJavaFX::class.java
) // launch the application by running the program
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(
KotlinFileTextCounterJavaFX::class.java) // launch the application by running the program
}
}
}
Loading

0 comments on commit 2fabcb4

Please sign in to comment.