Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- SimpleList<String> list = new SimpleList<String>(null);
- MyQueue queue = new MyQueue();
- System.out.println(args.length);
- if (args.length != 3) {
- System.out.println("No input file provided. Expected Usage: java <executable> words5.txt");
- System.exit(1);
- }
- else {
- File string_file = new File(args[0]);
- String start = new String(args[1]);
- String end = new String(args[2]);
- try {
- Scanner inStrings = new Scanner(string_file);
- while(inStrings.hasNext()) {
- String nextStr = inStrings.next();
- if(nextStr.equals(start)){
- do{
- System.out.println(nextStr);
- list.pushBack(nextStr);//------------push words into inputStack
- nextStr = inStrings.next();
- }while(!nextStr.equals(end));
- }
- }
- inStrings.close();
- } catch (FileNotFoundException e) {
- System.out.println(e.getMessage());
- }
- }
- //***********************add in main code
- int i=0;
- while(list.getAt(i)!=null){//--------------------------------compares first word to all in list
- if(wordCompare(list.getAt(0),list.getAt(i))){
- MyStack<String> temp = new MyStack<String>();
- temp.push(list.getAt(0));
- temp.push(list.getAt(i));
- queue.push(temp);
- list.remove(i);
- i--;
- }
- i++;
- }
- list.remove(0);
- int j=0;
- while(queue.getAt(j)!=null){
- String first = ((MyStack<String>) queue.front()).top();
- int k=0;
- while(list.getAt(k)!=null){
- if(wordCompare(list.getAt(0),list.getAt(i))){
- MyStack<String> temp = new MyStack<String>();
- temp.push(list.getAt(0));
- temp.push(list.getAt(i));
- queue.push(temp);
- list.remove(i);
- k--;
- }
- k++;
- }
- if(queue.getAt(j+1)!=null)
- queue.pop();
- j++;
- }
- System.exit(0);
- }
- public static boolean wordCompare(String first, String second){
- Character[] farray,sarray;
- farray = toCharArray(first);
- sarray = toCharArray(second);
- int count = 0;
- for(int i=0; i<farray.length; i++){
- if(farray[i].equals(sarray[i]))
- count++;
- }
- if(count==1)
- return true;
- else
- return false;
- }
- public static Character[] toCharArray(String s){
- if(s==null)
- return null;
- int length = s.length();
- Character[] carray = new Character[length];
- for(int i=0; i<length; i++){
- carray[i] = new Character(s.charAt(i));
- }
- return carray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment