java - How to compare a number from different lines of input? -
i had question trying according metric, have determine best line, no. of spaces divided no. of characters in line. line best such ratio gets printed out. can that, if line lower such ratio comes out, not printed. how compare ratios?? see here:
import java.util.scanner; public class bestline { public static void main(string[] args) { scanner s = new scanner(system.in); system.out.println("enter line of text: "); double temp = 0.0; while (s.hasnextline()) { string sentence = s.nextline(); string chars = sentence.trim(); int whtspacs = sentence.length() - chars.length(); int totchars = chars.length(); double ratio = (double) whtspacs/totchars; if (ratio < temp) { break; } temp = ratio; system.out.println("best line far is: " + sentence); } system.out.println("best line was: " ); } }
thanks telling me using variable, here's code again , it's not working? sorry asking simple question, don't it! again, much
check :
scanner s = new scanner(system.in); arraylist<double> ratios=new arraylist<double>(); arraylist<string> sentences=new arraylist<string>(); system.out.println("enter line of text: "); while (s.hasnextline()) { string sentence = s.nextline(); // number of white spaces int count = 0; for(int = 0; < sentence.length(); i++) { if(character.iswhitespace(sentence.charat(i))) count++; } //number of characters int totchars = sentence.length()-count; //add ratio our arraylist ratios.add((double) count / totchars); // add sentence arraylist sentences.add(sentence); if (sentence.length()==0) { break; } } //find best ratio int best=0; for(int i=0;i<ratios.size();i++) { if(ratios.get(i)<ratios.get((int)best)) best=i; } system.out.println("best line far is: " + sentences.get((int)best)); }
Comments
Post a Comment