From f2d859c5db34ac16d5e228dd649b77c5e4ae5562 Mon Sep 17 00:00:00 2001 From: Nwokocha wisdom maduabuchi Date: Sat, 5 Sep 2020 02:17:15 +0100 Subject: [PATCH] fix count word logic --- .idea/vcs.xml | 6 ++++ .../java/com/dev/readtime/MainActivity.kt | 9 ++++++ .../main/java/com/dev/readtime/ReadTime.kt | 28 ++++++++++------ app/src/main/res/layout/activity_main.xml | 13 ++++++-- .../src/main/java/com/dev/minread/Minread.kt | 32 ++++++++++++------- 5 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/dev/readtime/MainActivity.kt b/app/src/main/java/com/dev/readtime/MainActivity.kt index 7dcd3ca..9d6197c 100644 --- a/app/src/main/java/com/dev/readtime/MainActivity.kt +++ b/app/src/main/java/com/dev/readtime/MainActivity.kt @@ -31,8 +31,17 @@ class MainActivity : AppCompatActivity() { "To use this endpoint, simply append live to your API request URL, followed by your API access key and any of your preferred options." + "To use this endpoint, simply append live to your API request URL, followed by your API access key and any of your preferred options." + "To use this endpoint, simply append live to your API request URL, followed by your API access key and any of your preferred options." + + val you = "f your audience reads slower or faster than 200 words per minute — \n" + + " perhaps your blog is in English, which isn’t your audience’s first language, or perhaps your material is extremely\n" + + " basic and easy to skim — then you’ll want to use calculator instead. First, you’ll have to divide your total word \n" + + " count by the average words read per minute of your audience. Let’s say your 938-word article has an audience that \n" + + " reads 150 words per minute. That gives you 6.253. Enter 6.253 into the Decimal-to-Time Calculator, choose “Minutes” \n" + + " from the drop-down menu and press “Calculate.”\n" + + " Immediately, you’ll get a minute and seconds estimate. In this case, it’s 6 minutes and 15 seconds." // Read Time Logic // text.text = ReadTime.readtime(ty) + wordct.text = ReadTime.countWords(ty).toString() minread.text = ReadTime.readtime(ty) diff --git a/app/src/main/java/com/dev/readtime/ReadTime.kt b/app/src/main/java/com/dev/readtime/ReadTime.kt index 324d33e..9120eeb 100644 --- a/app/src/main/java/com/dev/readtime/ReadTime.kt +++ b/app/src/main/java/com/dev/readtime/ReadTime.kt @@ -3,9 +3,9 @@ package com.dev.readtime object ReadTime { fun readtime(string: String): String { - val word_count = string.length + val word_count = countWords(string) val total = word_count / 200 - if (total == 0) + if (total < 1) return "0.5 min read" else return "$total min read" @@ -13,21 +13,29 @@ object ReadTime { fun custome_readtime(string: String, message: String): String { - val word_count = string.length + val word_count = countWords(string) val total = word_count / 200 - if (total == 0) + if (total < 1) return "0.5 $message" else return "$total $message" } - fun wordcount(string: String): String { - - val word_count = string.length - val total = word_count / 200 - return word_count.toString() - + fun countWords(inputString: String): Int { + //Split String by Space + val strArray = inputString.split(" ".toRegex()).toTypedArray() // Spilt String by Space + val sb = StringBuilder() + var count = 0 + + //iterate String array + for (s in strArray) { + if (s != "") { + //Increase Word Counter + count++ + } + } + return count } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 46d5e51..6abc5e7 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,20 +8,27 @@ + + + app:layout_constraintTop_toBottomOf="@id/wordct" /> \ No newline at end of file diff --git a/minread/src/main/java/com/dev/minread/Minread.kt b/minread/src/main/java/com/dev/minread/Minread.kt index b7817f5..bd5b075 100644 --- a/minread/src/main/java/com/dev/minread/Minread.kt +++ b/minread/src/main/java/com/dev/minread/Minread.kt @@ -2,32 +2,40 @@ package com.dev.minread object Minread { - fun readtime(string: String): String { + fun minread(string: String): String { - val word_count = string.length + val word_count = countWords(string) val total = word_count / 200 - if (total == 0) + if (total < 1) return "0.5 min read" else return "$total min read" } - fun custome_readtime(string: String, message: String): String { + fun custome_minread(string: String, message: String): String { - val word_count = string.length + val word_count = countWords(string) val total = word_count / 200 - if (total == 0) + if (total < 1) return "0.5 $message" else return "$total $message" } - fun wordcount(string: String): String { - - val word_count = string.length - val total = word_count / 200 - return word_count.toString() - + fun countWords(inputString: String): Int { + //Split String by Space + val strArray = inputString.split(" ".toRegex()).toTypedArray() // Spilt String by Space + val sb = StringBuilder() + var count = 0 + + //iterate String array + for (s in strArray) { + if (s != "") { + //Increase Word Counter + count++ + } + } + return count } } \ No newline at end of file