ydornberg

assn2 main

Oct 26th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         SimpleList<String> list = new SimpleList<String>(null);
  9.         MyQueue queue = new MyQueue();
  10.         System.out.println(args.length);
  11.         if (args.length != 3) {
  12.             System.out.println("No input file provided. Expected Usage: java <executable> words5.txt");
  13.             System.exit(1);
  14.         }
  15.         else {
  16.             File string_file = new File(args[0]);
  17.             String start = new String(args[1]);
  18.             String end = new String(args[2]);
  19.             try {
  20.                 Scanner inStrings = new Scanner(string_file);
  21.                 while(inStrings.hasNext()) {
  22.                     String nextStr = inStrings.next();
  23.                     if(nextStr.equals(start)){
  24.                         do{
  25.                             System.out.println(nextStr);
  26.                             list.pushBack(nextStr);//------------push words into inputStack
  27.                             nextStr = inStrings.next();
  28.                         }while(!nextStr.equals(end));
  29.                     }
  30.                 }
  31.        
  32.                 inStrings.close();
  33.             } catch (FileNotFoundException e) {
  34.                 System.out.println(e.getMessage());
  35.             }
  36.         }
  37.         //***********************add in main code
  38.         int i=0;
  39.         while(list.getAt(i)!=null){//--------------------------------compares first word to all in list
  40.             if(wordCompare(list.getAt(0),list.getAt(i))){
  41.                 MyStack<String> temp = new MyStack<String>();
  42.                 temp.push(list.getAt(0));
  43.                 temp.push(list.getAt(i));
  44.                 queue.push(temp);
  45.                 list.remove(i);
  46.                 i--;
  47.             }
  48.             i++;
  49.         }
  50.         list.remove(0);
  51.         int j=0;
  52.         while(queue.getAt(j)!=null){
  53.             String first = ((MyStack<String>) queue.front()).top();
  54.             int k=0;
  55.             while(list.getAt(k)!=null){
  56.                 if(wordCompare(list.getAt(0),list.getAt(i))){
  57.                     MyStack<String> temp = new MyStack<String>();
  58.                     temp.push(list.getAt(0));
  59.                     temp.push(list.getAt(i));
  60.                     queue.push(temp);
  61.                     list.remove(i);
  62.                     k--;
  63.                 }
  64.                 k++;
  65.             }
  66.             if(queue.getAt(j+1)!=null)
  67.                 queue.pop();
  68.             j++;
  69.         }
  70.        
  71.        
  72.     System.exit(0);
  73.     }
  74.     public static boolean wordCompare(String first, String second){
  75.         Character[] farray,sarray;
  76.         farray = toCharArray(first);
  77.         sarray = toCharArray(second);
  78.         int count = 0;
  79.         for(int i=0; i<farray.length; i++){
  80.             if(farray[i].equals(sarray[i]))
  81.                 count++;
  82.         }
  83.         if(count==1)
  84.             return true;
  85.         else
  86.             return false;
  87.     }
  88.     public static Character[] toCharArray(String s){
  89.         if(s==null)
  90.             return null;
  91.         int length = s.length();
  92.         Character[] carray = new Character[length];
  93.         for(int i=0; i<length; i++){
  94.             carray[i] = new Character(s.charAt(i));
  95.         }
  96.         return carray;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment