intermediate/Smarter

From ggc

Jump to: navigation, search

01 package intermediate;
02 
03 
04 import java.io.*;
05 import java.util.*;
06 
07 /**this class gets average word lengths*/
08 public class Smarter
09 {
10   public boolean areTooSimilar(String passage1, String passage2)
11   {
12     Scanner one=new Scanner(passage1);
13     Scanner two=new Scanner(passage2);
14     ArrayList<String> oneWord=new ArrayList<String>();
15     ArrayList<String> twoWord=new ArrayList<String>();
16     while(one.hasNext())
17     {
18       String single=one.next().toLowerCase();
19       if(!oneWord.contains(single)) oneWord.add(single);
20     }
21     while(two.hasNext())
22     {
23       String single=two.next().toLowerCase();
24       if(!twoWord.contains(single)) twoWord.add(single);
25     }
26     double same=0;
27     for(String single: oneWord)
28     {
29       if(twoWord.contains(single))
30         same++;
31     }
32     if(oneWord.size()==|| twoWord.size()==0return false;
33     if(same/oneWord.size()>0.9 || same/twoWord.size()>0.9)
34       return true;
35     return false;
36     //fill in here
37   }
38 
39   public static void main(String[] args)
40   {
41     String filename=null;
42     Scanner console=new Scanner(System.in);
43     while(filename==null)
44     {
45       System.out.println("Enter in a file name.");
46       filename=console.next();
47       try
48       {
49         File file=new File(filename);
50         Scanner fromFile=new Scanner(file);
51         double totalChars=0;
52         int totalWords=0;
53         while(fromFile.hasNext())
54         {
55           String oneWord=fromFile.next();
56           totalWords++;
57           totalChars+=oneWord.length();
58         }
59         if(((double)totalChars)/totalWords>=6.75)
60         {
61           System.out.println("The author is smarter than a fifth grader");
62         }
63         else
64         {
65           System.out.println("Written by an elementary student.");
66         }
67       }
68       catch(Exception e)
69       {
70         System.out.println("Sorry, "+filename+" not found, please try again.");
71         filename=null;
72       }
73     }
74 
75   }
76 }


Download/View intermediate/Smarter.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter