Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A really simple program in 30 lines of code to decide the grade level that a book/paragraph/sentence should be offered to.
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
You can’t perform that action at this time.