Question

Please help me with this question. It is in java and if possible please provide details....

Please help me with this question. It is in java and if possible please provide details. Thank you so much. With this question there is no file provide, just make one up.

Write a program that reads words from a text file and displays all the words(duplicates allowed) in ascending alphabetical order. The words must start with a letter. The text file is passed as a command-line argument.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

SortWords.java

package fileIO;

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

public class SortWords {

   public static int readFile(String filename,String array[]) {
       Scanner sc = new Scanner(System.in);
       File file = new File(filename); //reading data from this file
       Scanner reader;
       int index=0;
       try {
           //read file using scanner
           reader = new Scanner(file);
           while(reader.hasNext()){
               array[index] = reader.next();
               index++;
           }
       } catch (FileNotFoundException e) {
           System.out.println("file not found");
       }
       sc.close();
       return index;
   }
   public static void main(String[] args) {
       //assume max words are 1000
       String words[] = new String[1000];
       int size;
       String filename;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter file name : ");
       filename = sc.nextLine();
       size=readFile(filename, words);

      
       //sorting
       for(int i=0;i<size;i++){
           for(int j=i+1;j<size;j++){
               if(words[i].compareTo(words[j])>0){
                   String temp = words[i];
                   words[i] = words[j];
                   words[j]= temp;
               }
           }   
       }
      
       //printing after sorting
       for(int i=0;i<size;i++){
           System.out.println(words[i]);
       }
   }
}

input.txt

this line contains some loooooooooooooong wordsssssssssssssss.
hello!
short line.
this is the last line.

output:

java input.txt 3 line contains some loooooooooooo <terminated>SortWords [Java Application] CAProgram Console Enter file name

Add a comment
Know the answer?
Add Answer to:
Please help me with this question. It is in java and if possible please provide details....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs...

    out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs to be handled and is specifically the part I am having trouble with https://www.dropbox.com/s/ume3slnphyhi6wt/out1.txt?dl=0 - link for the out1.txt file for testing yourself Please include comments as I am in this for learning and would really appreciate the help! Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • Class: Java Book: Introduction To Java Programming 10th Edition, Comprehensive by Daniel Liang Problem: ****** Parse...

    Class: Java Book: Introduction To Java Programming 10th Edition, Comprehensive by Daniel Liang Problem: ****** Parse the text file by splitting on "[ \n\t\r.,;:!?(){}<>\"]", that is call the split method with this parameter: For example, call contents.split("[ \n\t\r.,;:!?(){}<>\"]"), where contents is a String object containing the contents of the text file. Also, explain (in the comment block at the beginning of the source file) two other approaches that could have been taken for the problem (use of other data structures,...

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • please help me with, Write a program in java that generates 10 random doubles, all between...

    please help me with, Write a program in java that generates 10 random doubles, all between 1 and 11, and writes them to a text file, one number per line. Then write another program that reads the text file and displays all the doubles and their sum accurate to two decimal places. SAMPLE OUTPUT 10.6269119604172 2.737790338909455 5.427925738865128 1.3742058065472509 1.1858700262498836 4.180391276485228 4.910969998930675 5.710858234343958 7.790857007373052 3.1806714736219543 The total is 47.13

  • This is a java question A palindrome is a word or phrase that reads the same...

    This is a java question A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: warts n straw radar Able was I ere I saw Elba tacocat Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is...

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • Please make a method in Java that will allow me to find a String in a...

    Please make a method in Java that will allow me to find a String in a file using recursion. The details are the following: Search the input file line by line for a given string. The output must contain the line number, followed by the contents of the line that contains the search argument. For instance given the following the search string: Java, the program would search the file line by line generating a result such as the following: 50:...

  • Please do in java as simply as possible with code available for copy and comments. Provide...

    Please do in java as simply as possible with code available for copy and comments. Provide screenshots of code and how your incorporating the .txt file with the code. Write a program that reads in temperatures from the text file temps.txt (file given in the assignment). Write the minimum temperature, maximum temperature and the new array (temperatures plus 10) to a new file. The temperatures in file are: 83 87 81 88 90 92 95

  • Please do in java as simply as possible with code available for copy and comments Write...

    Please do in java as simply as possible with code available for copy and comments Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Throw an exception if the file does not exist. An error message should result. For example if file input is: This is a test File output should be 1. This is a test I...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT