Well my code is supposed to ask for an input file and then (ex: input.txt), read the input file and create an output.txt file with the anagram for the words in the file. Also it should be displayed on the screen. However my code doesn't display the anagram on screen or the output file! Please show me whats wrong. Thanks i need a part of the code thats fixed if possible!
Heres is the code:
import java.io.*; import java.lang.*; import java.util.*; /* This program will read a file given by the user, read the words within the * file and determine anagrams of the given words. If the file that the user * inputs is empty, then the program will output "The input file is empty." * The program will read the file line by line, counting the total number of * words read. If there are more than 50 words, "There are more than 50 words." * will be printed, and the program will terminate. After each line is read, the * words in the line will be separated,punctuation characters will be removed, * and upper case characters will be switched to lower case. If any word is * larger than 12 characters, that word will not be considered in the total * amount of words in the file and it will not be sorted. After each word is * read, the letters will be sorted and stored into an array containing each * word's 'signature'. After all the words have been read, words will be printed * to the output file on the same line based upon their signature. */ public class Anagram { //Creating constants for maximum words in file and maximum chars in word public static final int MAX_CHARS = 12; public static final int MAX_WORDS = 50; public static void main(String[] args) { int numWords = 0, numChars = 0; Vector signatures= new Vector(MAX_WORDS); String tempWord, fileName; Scanner inputStr = new Scanner(System.in); Scanner readFile = null; System.out.println("Enter the name of the input file:"); //Reading user input filename and checking if it exists. fileName = inputStr.next(); File inFile = new File(fileName); try{ readFile = new Scanner(inFile); //Making first pass through file to see if the file contains more than //50 words. while(readFile.hasNext()){ if(readFile.next().length()>MAX_CHARS){ continue; } else numWords++; if(numWords>MAX_WORDS){ System.out.println("There are more than" + "50 words in the input file."); System.exit(1); } } } catch(FileNotFoundException e){ System.out.println(e); System.exit(1); } finally{ readFile.reset(); } numWords = 0; /*Creating char array to store words from file until the signature * is created after which the signature will be moved into a vector. */ String temp = new String(); //Char array that will contain no punctuation characters char []currentWordSignature = new char[MAX_CHARS]; char []previousWordSignature = new char[MAX_CHARS]; previousWordSignature[0]='\0'; //Reading each word in file, converting upper-case->lower-case, //removing punctuation characters, and storing signatures. /*Writing into output file and onscreen on same line until signatures * do not match, then a newline character will be printed. */ PrintWriter output = null; try{ readFile = new Scanner(inFile); output = new PrintWriter(new FileWriter("C:\\output.txt")); while(readFile.hasNext()){ temp = readFile.next(); if(temp.length()>MAX_CHARS){ continue; } /*if temp is less than 13 characters in length, print temp onscreen * and into the output file. */ currentWordSignature=temp.replaceAll("[^a-zA-Z]", "").toLowerCase().toCharArray(); //Calling Arrays.sort() function to sort char array alphabetically Arrays.sort(currentWordSignature); if(Arrays.equals(currentWordSignature, previousWordSignature) || previousWordSignature[0]=='\u0000'){ System.out.print(temp + " "); output.print(temp + " "); } else if(currentWordSignature != previousWordSignature){ System.out.print("\n" + temp + " "); output.println(); output.print(temp + " "); } previousWordSignature = currentWordSignature; } }catch(FileNotFoundException e){ System.out.println(e); System.exit(1); }catch(IOException e){ System.out.println(e); System.exit(1); } finally{ readFile.close(); output.close(); } for(int i=0;i<signatures.size();i++){ System.out.println(signatures.get(i)); } } }
Ads