Advertisement
markgrenader

Algortihms.java

Aug 5th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7.  
  8. public class Algorithms {
  9.  
  10.  
  11.     public int simpleCoolAlg(String str){
  12.         String s = "Likes:";
  13.         int i = s.length();
  14.         int popularity = 0;
  15.         int posts = 0;
  16.         while(str.lastIndexOf(s) != -1){
  17.             int k = str.indexOf(s);
  18.  
  19.             String string = str.substring(k + i);
  20.             string = string.substring(1);
  21.             String temp = (string.substring(0, string.indexOf(" ")));          
  22.             popularity += Integer.parseInt(temp);
  23.             str = str.substring(str.indexOf(temp) + temp.length() + 1);
  24.             posts++;
  25.         }
  26.         popularity /= posts;
  27.  
  28.         return popularity;
  29.     }
  30.  
  31.  
  32.     public static ArrayList<ArrayList<String>> breakFile(ArrayList<String> str){
  33.         ArrayList<ArrayList<String>> obj = new ArrayList<ArrayList<String>>();
  34.         char ch = '"';
  35.         int i = 1;
  36.         for (String s: str){
  37.             int temp = s.indexOf(ch);
  38.             ArrayList<String> stringTemp = new ArrayList<String>();
  39.             int temp2 = s.substring(temp + 1).indexOf(ch);
  40.             temp2 += temp + 1;
  41.             String string = s.substring(temp, temp2 + 1);
  42.             stringTemp.add(string);
  43.  
  44.             s = s.substring(temp2 + 1);
  45.             temp = s.indexOf(" ");
  46.             temp2 = s.substring(temp + 1).indexOf(" ");
  47.             string = s.substring(temp + 1, temp2 + 1);
  48.             stringTemp.add(string);
  49.  
  50.  
  51.             s = s.substring(temp2 + 1);
  52.             temp = s.indexOf(" ");
  53.             temp2 = s.substring(temp + 1).indexOf(" ");
  54.             string = s.substring(temp + 1, temp2 + 1);
  55.             stringTemp.add(string);
  56.  
  57.  
  58.             s = s.substring(temp2 + 1);
  59.             temp = s.indexOf(" ");
  60.             temp2 = s.substring(temp + 1).indexOf(" ");
  61.             string = s.substring(temp + 1, temp2 + 1);
  62.             stringTemp.add(string);
  63.  
  64.  
  65.             s = s.substring(temp2 + 1);
  66.             temp = s.indexOf(" ");
  67.             temp2 = s.substring(temp + 1).indexOf(" ");
  68.             string = s.substring(temp + 1, temp2 + 1);
  69.             stringTemp.add(string);
  70.  
  71.             s = s.substring(temp2 + 1);
  72.             stringTemp.add(s);
  73.  
  74.             obj.add(stringTemp);
  75.         }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.         return obj;
  82.     }
  83.  
  84.  
  85.     public ArrayList<String> readFile(String fName) throws IOException{
  86.         String stuff = " ";
  87.         File file = new File(fName);
  88.  
  89.         FileReader fr = new FileReader(file);
  90.         ArrayList<String> s = new ArrayList<String>();
  91.         @SuppressWarnings("resource")
  92.         BufferedReader read = new BufferedReader(fr);
  93.         while ((stuff = read.readLine()) != null){
  94.             s.add(stuff);
  95.  
  96.  
  97.         }
  98.  
  99.         return s;
  100.  
  101.  
  102.     }
  103.  
  104.  
  105.     public static ArrayList<String> breakDown(Person p){
  106.         ArrayList<String > broken = new ArrayList<String>();
  107.         ArrayList<String> person = p.getStatus();
  108.         for (String s: person){
  109.             while (s.indexOf(" ") != -1){
  110.                 int temp = s.indexOf(" ");
  111.                 broken.add(s.substring(0, temp));
  112.                 s = s.substring(temp + 1);
  113.  
  114.  
  115.  
  116.             }
  117.  
  118.  
  119.         }
  120.  
  121.         return broken;
  122.     }
  123.  
  124.  
  125.     public static Person algorithms(Person p, ArrayList<ArrayList<String>> strList){
  126.  
  127.         for (int i = 0; i < p.getStatus().size(); i++){
  128.             //System.out.println(p.getStatus().get(i));
  129.             for (int j = 0; j < strList.size(); j++){
  130.                 //if status word == keyword
  131.                 //System.out.println(p.getStatus().get(i));
  132.                 //System.out.println(strList.get(j).get(0));
  133.                 if (p.getStatus().get(i).equals(strList.get(j).get(0))){
  134.                     ArrayList<Double> dList = new ArrayList<Double>();
  135.                     for (int a = 1; a <= 5; a++){
  136.                         //add the doubles into list
  137.                         dList.add((Double)Double.parseDouble(strList.get(j).get(a)));
  138.                     }
  139.                     //modify person
  140.                     p.addExtrav(dList.get(0));
  141.                     p.addNeuro(dList.get(1));
  142.                     p.addAgree(dList.get(2));
  143.                     p.addConsc(dList.get(3));
  144.                     p.addOpen(dList.get(4));
  145.                     for (int b = 0; b < 5; b++){
  146.                         System.out.println(dList.get(b));
  147.                     }
  148.                 }  
  149.             }
  150.         }
  151.  
  152.         return p;
  153.     }
  154.    
  155.     public static ArrayList<ArrayList<String>> removeQuotesAndToLowerCase(ArrayList<ArrayList<String>> str){
  156.         for(ArrayList<String> string: str){
  157.             for (String s: string){
  158.                 if (s.indexOf('"') != -1){
  159.                     s = s.substring(1, s.length() - 1);
  160.                     s = s.toLowerCase();
  161.                    
  162.                 }
  163.                
  164.                
  165.             }
  166.            
  167.            
  168.         }
  169.        
  170.         return str;
  171.        
  172.     }
  173.    
  174.     public static Person toLowerCase(Person p){
  175.         ArrayList<String> str = p.getStatus();
  176.         for (String s: str)
  177.             s = s.toLowerCase();
  178.         p.setStatus(str);
  179.         return p;
  180.        
  181.     }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement