How do I count the number of lines in a string file? (Java) There\'s 2 input fil
ID: 3848790 • Letter: H
Question
How do I count the number of lines in a string file? (Java)
There's 2 input files in this program. The program runs just fine aside from one looping issue. It's supposed count the amount of words (one word per line) in a text file named "Dictionary.txt" file.
Boggledata.txt includes:
D R L X E I
C P O H S A
N H N L Z R
W T O O T A
I O S S E T
N W E G H E
B O O J A B
U I E N E S
P S A F K F
I U N H M Qu
Y R D V E L
V E H W H R
I O T M U C
T Y E L T R
S T I T Y D
A G A E E N
dictionary.txt includes a list of 61,406 words
The output should look like
Die 0: D R L X E I
looped all the way up to
Die 15: A G A E E N
as well as saying: "There are (number of words) entries in the dictionary"
My ouput however, continues to come out at 0. I would appreciate any help. Thank you
Note: The method of reading the data file was already given and that is what I'm expected to use.
Boggle Class is where my print statement is supposed to be as well.
import core.Board;
import inputOutput.ReadDataFile;
import java.util.ArrayList;
public class Boggle {
private static ArrayList boggleData = new ArrayList();
private static String dataFileName = "BoggleData.txt";
private static ArrayList dictionaryData = new ArrayList();
private static String dictionaryFileName = "Dictionary.txt";
public static void main(String[] args) {
ReadDataFile data = new ReadDataFile(dataFileName);
data.populateData();
ReadDataFile dictionary = new ReadDataFile(dictionaryFileName);
dictionary.populateData();
Board board = new Board(data.getData(), dictionary.getData());
board.populateDice();
int size = dictionaryData.size();
System.out.println("There are " + size + " entries in the dictionary");
}
}
import java.util.ArrayList;
public class Board implements IBoard{
private static ArrayList<String> boggleData;
private static ArrayList<String> dictionaryData;
private static ArrayList<Die> boggleDice;
public Board(ArrayList<String> diceData, ArrayList<String> dictionary){
boggleData = diceData;
dictionaryData = dictionary;
boggleDice = new ArrayList<Die>();
}
@Override
public ArrayList shakeDice() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void populateDice() {
Die die;
int counter = 0;
for(int dice = 0; dice < NUMBER_OF_DICE; dice++){
die = new Die();
String[] data = boggleData.get(dice).split(" ");
for(int side = 0; side < die.NUMBER_OF_SIDES; side++){
die.addLetter(data[side]);
counter++;
}
System.out.print("Die "+ dice + ": ");
die.displayLetters();
System.out.println();
counter++;
boggleDice.add(die);
}
}
}
import java.util.*;
public interface IBoard {
public static final int NUMBER_OF_DICE = 16;
public static final int GRID = 4;
public void populateDice();
public ArrayList shakeDice();
}
import java.util.ArrayList;
public class Die implements IDie {
private ArrayList<String> sides = new ArrayList<String>();
@Override
public String rollDie() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void addLetter(String letter) {
sides.add(letter);
}
@Override
public void displayLetters() {
for(String value : sides){
System.out.print(" " + value + "");
}
}
}
public interface IDie {
static final int NUMBER_OF_SIDES = 6;
public String rollDie();
public void addLetter(String letter);
public void displayLetters();
}
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadDataFile implements IReadDataFile {
private Scanner inputFile;
private String fileName;
private ArrayList<String> data;
public ReadDataFile(String fileName) {
this.fileName = fileName;
data = new ArrayList<String>();
}
public ArrayList<String> getData() {
return data;
}
@Override
public void populateData() {
try {
URL url = getClass().getResource("/data/" + fileName);
File file = new File(url.toURI());
inputFile = new Scanner(file);
while(inputFile.hasNextLine()){
String line = inputFile.nextLine();
data.add(line);
}
inputFile.close();
}
catch (IOException | URISyntaxException e){
System.out.print(e.toString());
}
finally {
if (inputFile != null)
inputFile.close();
}
}
}
public interface IReadDataFile {
public void populateData();
}
Explanation / Answer
Hi, you have done a simple mistake while coding Boggle class and Board class.
In the Boggle class you have just simply created and initialized an arraylist with name dataFileName. Since it is not assigned with any value its size will be zero. That's why you are getting "There are 0 entries in the Dictinoary".
And also, populateDice method must have a return type of arraylist.
The modified code is placed below.
Boggle.java :
import java.util.ArrayList;
public class Boggle {
private static ArrayList boggleData = new ArrayList();
private static String dataFileName = "BoggleData.txt";
private static ArrayList dictionaryData = new ArrayList();
private static String dictionaryFileName = "Dictionary.txt";
public static void main(String[] args) {
ReadDataFile data = new ReadDataFile(dataFileName);
data.populateData();
ReadDataFile dictionary = new ReadDataFile(dictionaryFileName);
dictionary.populateData();
Board board = new Board(data.getData(), dictionary.getData());
dictionaryData = (ArrayList) board.populateDice();
int size = dictionaryData.size();
System.out.println("There are " + size + " entries in the dictionary");
}
}
Board.java :
import java.util.ArrayList;
import java.util.List;
public class Board implements IBoard{
private static ArrayList<String> boggleData;
private static ArrayList<String> dictionaryData;
private static ArrayList<Die> boggleDice;
public Board(ArrayList<String> diceData, ArrayList<String> dictionary){
boggleData = diceData;
dictionaryData = dictionary;
boggleDice = new ArrayList<Die>();
}
@Override
public ArrayList shakeDice() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public ArrayList populateDice() {
Die die;
int counter = 0;
for(int dice = 0; dice < NUMBER_OF_DICE; dice++){
die = new Die();
String[] data = boggleData.get(dice).split(" ");
for(int side = 0; side < die.NUMBER_OF_SIDES; side++){
die.addLetter(data[side]);
counter++;
}
System.out.print("Die "+ dice + ": ");
die.displayLetters();
System.out.println();
counter++;
boggleDice.add(die);
}
return boggleDice;
}
}
You need to modify only these two classes inorder to get your desired output. Remaining classes are working fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.