Question

In: Computer Science

Make a program to read a file, filled with words separated by new lines, from command...

Make a program to read a file, filled with words separated by new lines, from command line arguments (args[0]), print the file, and then add each word into an arraylist.

After making the arraylist, iterate through it and print its contents.

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class PrintFileWords {

    public static void main(String[] args) {
        if (args.length > 0) {
            Scanner in = new Scanner(System.in);
            File file = new File(args[0]);
            try {
                Scanner fin = new Scanner(file);
                System.out.println("sample file: " + args[0]);
                ArrayList<String> list = new ArrayList<>();
                while (fin.hasNext()) {
                    list.add(fin.nextLine());
                }
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i));
                }
                fin.close();
            } catch (FileNotFoundException e) {
                System.out.println(file.getAbsolutePath() + " is not found!");
            }
            in.close();
        } else {
            System.out.println("Please provide file name as command line argument");
        }
    }
}

Related Solutions

assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
Make a modification of the program below, that will read in the string as a “command-line...
Make a modification of the program below, that will read in the string as a “command-line argument” to your program, instead of having the user type it while your program is running. Your program should print out the inverted string to the screen. #include <iostream> #include <cstring> using namespace std; int Reverse(char * destination, const char * source, int num); int main() // this is the test/driver code, for your function { const int STRINGSIZE = 10; char oldCString[] =...
Write a program to reverse the lines of a file and to reverse the words plus...
Write a program to reverse the lines of a file and to reverse the words plus the letter's of each word within each line using ArrayList. A file name mobydick.txt Example: Original file contains the following MOBY DICK; OR THE WHALE by Herman Melville CHAPTER 1 Loomings. Out put should be .sgnimooL 1 RETPAHC ellivleM namreH yb ELAHW EHT RO ;KCID YBOM its for java eclipse
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Write a program Median.java to read each file whose name is specified in the command-line arguments....
Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier for...
Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
For Linux: Write a command to append the top 20 lines of the file seq.fasta to...
For Linux: Write a command to append the top 20 lines of the file seq.fasta to a second file named combined.seq.
IN PYTHON: Write a program that displays the lines from the total.txt file in the following...
IN PYTHON: Write a program that displays the lines from the total.txt file in the following output. Use a try catch phrase to check for errors. Use only one function for this portion [main()]. Remember to use Python. Sample output below: 19 16, 29 3, 30 4, 34
QUESTION : Read from a file that contains a paragraph of words. Put all the words...
QUESTION : Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT