4 Replies Latest reply on Oct 25, 2012 4:24 PM by 970521

    How do I let my program accept a text file as a input? (redirecting)

      I am trying to create a program that does the following.
      Typing without the quotes into command prompt: "java Crooswords ezplzyze < wordlist.txt" and executing would execute this program, "Crosswords", make it look inside wordlist.txt for a word that is 8 characters long (ezplzyze has 8 characters) and the 1st character is "e", 3rd "p", 4th "l", 6th "y" and 8th "e". The z characters are unknown so if wordlist.txt had the following words:


      The program would print out employer and employee. Why? Because both words are 8 characters long and in both the 1st character is "e", 3rd "p", 4th "l", 6th "y" and 8th "e".

      The main problem I am having is not knowing how to make java: count the characters of a particular word, read a particular line in a text document and handling the wordlist.txt that I redirect to the program upon execution by doing "< wordlist.txt" after the "java Crosswords ezplzyze" where ezplzyze is the word I am looking for in wordlist.txt without the z characters.
      How do I do those commands?

      This is what i have written so far:
      import StdIn.*; // I'm using commands from this class to read wordlist.txt so I need to declare this line
      public class Crosswords {
        public static void main(String[] args) {
          for (String inputWord: args) { // I think this loop catches the first attribute in the execution. So if I'd do "java Crosswords ezplzyze" when executing the program, the string inputWord = "ezplzyze"
            int b = 0; // This is the number of the line we are currently checking in the text file wordlist.txt
            int c = 0; // This is the lenght of the word we are looking for in wordlist.txt. If the word was "ezplzyze" c should be c = 8
            while ( StdIn.isEmpty() == false ) { // While there is text in the current line of wordlist we are checking, go on with the while loop. StdIn.isEmpty() = true when we reach line 6 since wordlist.txt only contains 5 words (the sixth line doesnt exist -> doesnt contain any word)
                c = StdIn.readLine(b); // Assign the length of the word we are now checking in line number b to c.
                if ( c == Stdin.lenght(inputWord) ) { // Is the lenght of the word we are currently checking in wordlist.txt in line b equally long as the inputWord?
                  char[] array_1 = new char[a]; // Create a character array to keep track of which characters are where in the inputWord. Make the array as big as the amount of characters in the inputWord.
                  char[] array_2 = new char[a] ;// Create a character array to keep track of which characters are where in the inputWord
                  boolean[] array_3 = new boolean[a]; // Keeps track of which characters of the inputWord and which of the word we are currently checking in wordlist.txt match.
                  // For example, if inputWord = "kamel" and the word we are currently checking was "knife", mengi_3[0] would be true because k = k but mengi_3[2] would be false because m != i
                  boolean keepGoing = true;
                  for ( int d = 1; d <= c && keepGoing; d++ ) { // As long as d is less or equal to the length of inputWord, keep going. (we are looping thorugh the characters in inputWord, comparing them to the characters in the word we are currently checking in line b of wordlist.txt
                    if ( array_1[d] == z || array_1[d] == array_2[d] ) { // f.x. when d = 0, it checks the first character of f.x. inputWord = "znife". Okay, it is z so let mengi_3[0] = true because z equals to any character in our comparison word since we dont know what z is.
                      array_3[d] = true; } // also, if inputWord = "knifz" and our comparing word would be "knife", also let mengi_3[0] = true because k = k.
                    else // However, if there is neither z in that particular spot in the inputWord nor does the first character of inputWord equal the first character of the word we are comparing to, the words are obviously not the same. Stop the loop, let b++ (check the next line of wordlist.txt and compare that next word to inputWord)
                      haldaAfram = false; }
                  counting = 0;
                  for ( int k = 0; k < c; k++ ) { // counts how many of array_3 are true
                    if ( array_3[k] == true ) {
                      talning++; }
                  if ( counting = c ) { // if all of them are true = all the characters match -> the words are obviously the same.
                  // System.out.println(our word of comparison);
                  else { // else, if counting != c, the words did not match, so do b++ which makes you check the next line using the while loop.
                    // the b++ is below
      Edited by: 967518 on Oct 24, 2012 7:00 PM