Skip to content

Instantly share code, notes, and snippets.

@Arinerron
Created March 5, 2016 05:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arinerron/dd6017b58519551c2e7c to your computer and use it in GitHub Desktop.
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.
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