Created
March 5, 2016 05:47
-
-
Save Arinerron/dd6017b58519551c2e7c to your computer and use it in GitHub Desktop.
A really simple program in 30 lines of code to decide the grade level that a book/paragraph/sentence should be offered to.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
public class Reader { | |
public Reader() { | |
String text = JOptionPane.showInputDialog("Please enter a few sentences to parse."); | |
int level = (int) read(text); | |
JOptionPane.showMessageDialog(null, "The text is " + (int) Math.ceil(level) + (level == 1 ? "st" : (level == 2 ? "nd" : (level == 3 ? "rd" : "th"))) + " grader level."); | |
} | |
public static void main(String[] args) { | |
new Reader(); | |
} | |
public float read(String text) { | |
String[] t = text.split(" "); | |
float avg = 0; | |
for(String word : t) { | |
float length = word.length(); | |
if(avg == 0) | |
avg = length; | |
avg = (avg + length) / 2; | |
} | |
return avg; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment