myprogram needs to ask the user for a file name. The file will contain a name on each line. Put each of the names into an ArrayList.
After you have put all of the names into the ArrayList search through the list and print out the following information;
Print out the longest name Print out the shortest name print out the total number of characters in all of the names. print out the average number of characters per name print out the whole list after the previous four pieces of information.
i have this so far. help with at least one aspect?
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Scanner;
public class project1 { public static void main(String[] args) throws IOException { String filename = ""; String longestString =""; int i; ArrayList<String> filestrings = new ArrayList<String>(); Scanner keyboard = new Scanner(System.in); System.out.print("Enter the name of the file you would like to search:"); filename = keyboard.nextLine(); File file = new File(filename); if (!file.exists()) { System.out.println("The file " +filename+ " is not found."); System.exit(0); } else { System.out.println(file); } File file1 = new File(filename); Scanner inputFile = new Scanner(file); while (inputFile.hasNext()) { String line = inputFile.nextLine(); filestrings.add(line); } System.out.println("There are " + filestrings.size() + " words in this document."); } }
Ads