0

okay so I know this code isn't entirely orthodox, but regardless it compiles & runs. the problem is once i input a txt file via the command line it only converts the first line in the file into String text. (yes, i know im using the nextLine() method.that is temp until i find a better way). How can i get the entire txt file, that has line breaks, into one string? thanks in advance for any suggestions/ tips.

    import java.util.*;


    public class Concordance{

    static Scanner kb;


    public static void main(String arg[]){
        //input text file, create array, create List, and call other methods

        kb = new Scanner(System.in);

        String text = kb.nextLine();
        String[] words = text.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
        List<String> list = Arrays.asList(text.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"));
        System.out.println("number of words in text (including duplicates) is: " + words.length);

        System.out.println("");
        alphaPrint(list);
        System.out.println("");
        uniqueWord(list);
    }//end main


    //prints text in alphabetical order and counts unique words 
    public static void alphaPrint(List<String> list){
        int count = 0;
        TreeSet<String> uniqueWord = new TreeSet<String>(list);
        System.out.println("text in alphabetical order: ");
        Collections.sort(list);
        for (String word : uniqueWord) {
            System.out.println(word);
            count++;
        }
        System.out.println("");
        System.out.println("unique word count is: " + count);
    }//end alphaprint


    //method will find and print frequency counts
    public static void uniqueWord(List<String> list){

        System.out.println("text with word frequencies: ");

        TreeSet<String> uniqueWord = new TreeSet<String>(list);
        for (String word : uniqueWord) {
            System.out.println(word + ": " + Collections.frequency(list, word));

        }
    }//end unique word

}//end class
raffian
  • 28,859
  • 25
  • 95
  • 164
overboard182
  • 150
  • 2
  • 16
  • Would it be allright to just input the text file name/path via command line and then read the contents as a string? I can provide sample code for that if you would like. – Vineet Kosaraju Dec 03 '13 at 01:32
  • @Bucco yes, if you could me a sample that would help get me on the right track that would be great. – overboard182 Dec 03 '13 at 01:40

2 Answers2

0

Maybe something like this

StringBuilder s = new StringBuilder();
while(kb.hasNextLine()){
  s.append(kb.nextLine())
}
text = s.toString();

Or maybe build the array in the while loop.. using kb.hasNext(pattern)

---- Edit You can run the the application using ./java filename < textfile.txt

jnes
  • 423
  • 2
  • 11
0

The code must iterate through the supplied file using a loop. Here is an example:

public class FileToString {

    public static void main(String[] args) {
        System.out.println("Please Enter a File:");

        Scanner scanner = new Scanner(System.in);
        String fileName = scanner.nextLine();


        Scanner fileScanner;
        try {

            File file = new File(fileName);
            fileScanner = new Scanner(file);

            String text = "";

            while (fileScanner.hasNext()) {
                text += fileScanner.nextLine();
            }
            System.out.println(text);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}
Kevin Bowersox
  • 88,138
  • 17
  • 142
  • 176