Question

You will be writing some methods that will give you some practice working with Lists. Create a new project and create a class
• List. • @param wordsFile The name of a file in the proper format containing the word list * @return a list of words read fr
---); System.out.println(--- displayList(list); System.out.println(); System.out.println(Wordlist 2); System.out.println(
C e est and paste une following line into it. over the lazy dog Save both of these files in the Project Folder you created fo
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ListPractice.java

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

public class ListPractice {
  
public static List<String> getList(String wordsFile)
{
Scanner fileReader;
List<String> list = new ArrayList<>();
try
{
fileReader = new Scanner(new File(wordsFile));
while(fileReader.hasNextLine())
{
list.add(fileReader.nextLine().trim());
}
}catch(FileNotFoundException fnfe){
System.out.println("ERROR! File wordsFile not found!");
}
return new ArrayList<>(list);
}
  
public static void displayList(List<String> myList)
{
int counter = 0;
for(String word : myList)
{
System.out.println((counter++) + ": " + word);
}
}
  
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a filename: ");
String fname = keyboard.nextLine();
List list1 = getList(fname);
System.out.print("Enter another: ");
fname = keyboard.nextLine();
List list2 = getList(fname);
  
System.out.println("Wordlist 1");
System.out.println("----------");
displayList(list1);
  
System.out.println();
System.out.println("Wordlist 2");
System.out.println("----------");
displayList(list2);
  
keyboard.close();
}
}

********************************************************************** SCREENSHOT *****************************************************

DD DD run: Enter a filename: test1. txt Enter another: test2.txt Wordlist 1 0: the 1: quick 2: brown 3: fox 4 jumps Wordlist

Add a comment
Know the answer?
Add Answer to:
You will be writing some methods that will give you some practice working with Lists. Create...
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
  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File...

    Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File Explorer onto the BlueJ project window.) Otherwise, use the instructor's App class: Create a new class using the "New Class..." button and name it App. Open the App class to edit the source code. Select and delete all the source code so that the file is...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • FIRST: Write code that prompts the user for the name of a text file, opens that...

    FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each line to the screen. You must implement the file read in a try/catch block rather than throwing the exception on the main method. NEXT: Modify your code so that the program takes the name of two files from the command line, opens the first file if it exists for...

  • Java Description This project is designed to help you practice for the second midterm examination. First...

    Java Description This project is designed to help you practice for the second midterm examination. First you will hand write the code for the project, just as you would do on the examination. Then you will take your hand written code and enter it into eclipse and debug/fix it. This should help you identify challenges that you have in hand writing code that could cause problems on the midterm on Wednesday Write a program that generates platitudes, by writing each...

  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...

  • This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class...

    This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class Write a class named Player that stores a player’s name and the player’s high score. A player is described by:  player’s name  player’s high score In your class, include:  instance data variables  two constructors  getters and setters  include appropriate value checks when applicable  a toString method Part 2: PlayersList Class Write a class that manages a list...

  • Hey I really need some help asap!!!! I have to take the node class that is...

    Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...

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