JAVA File Chooser doesnt work for my code Can you help me fixed the bug on File
ID: 3814004 • Letter: J
Question
JAVA
File Chooser doesnt work for my code
Can you help me fixed the bug on File chooser in main function plz ( you can change anything but Entry & SimpleList classes)
------------------- Entry.java
public class Entry {
//The word to store in the Entry, this will be a key value for your hash table.
private String word;
//The count of how many times the word appears.
private int count;
/**
* Constructor that creates an {@code Entry} object given a word.
*
* @param word The word to set in the {@code Entry}.
*/
public Entry(String word) {
this.word = word;
this.count = 1;
}
/**
* Returns the word of this {@code Entry}.
*
* @return The word of this {@code Entry}.
*/
public String getWord() {
return this.word;
}
/**
* Returns the count of how many times this word appears in the document.
*
* @return the word count.
*/
public int getCount() {
return this.count;
}
/**
* Increases the count of the word in this {@code Entry} by one.
*
*/
public void incrementCount() {
this.count++;
}
@Override
public String toString() {
String result = "";
result += "Word: " + this.word + " " +
"Count: " + this.count;
return result;
}
public int hashCode(String str, int type) {
//You must implement this method!!!!
int hashValue;
if(type ==1){ // primary hash
hashValue = str.hashCode();
return hashValue %7;
}
// secondary hash function
hashValue = str.hashCode();
return 7 - (hashValue % 7);
}
}---------------------------------------------------
----------- CLASS SIMPLELIST
public class SimpleList {
//Initial size of the internal array.
private static final int INITIAL_CAPACITY = 10;
//Internal array of Entry objects.
public Entry[] entries;
//Size of the List
private int size;
/**
* Constructor creates an empty {@code SimpleList} with default initial capacity.
*
*/
public SimpleList() {
this.entries = new Entry[INITIAL_CAPACITY];
this.size = 0;
}
/**
* This method adds a new {@code Entry} to the end of the list. The list will also be resized when necessary.
*
* @param e The entry to add to the end of the list.
*/
public void add(Entry e) {
//Check to see if we need to resize the list
if (this.size == this.entries.length) {
this.resize();
}
this.entries[this.size] = e;
this.size++;
}
/**
* This function finds the {@code Entry} in the list whose word matches the given {@code String}. The function
* returns the index of where the Entry can be found, or -1 if the {@code Entry} was not found.
*
* @param word The word whose {@code Entry} you want to find.
*
* @return Returns the index of where the {@code Entry} was found, -1 otherwise.
*/
public int find(String word) {
for (int i = 0 ; i < this.size ; i++) {
Entry current = this.entries[i];
if (word.equals(current.getWord())) {
return i;
}
}
return -1;
}
/**
* This method returns the {@code Entry} at the given index.
*
* @param index The index of the {@code Entry} to return. {@code index} must be a positive value between 0 to
* size()-1 inclusive.
* @return The {@code Entry} at the given index.
*/
public Entry getEntry(int index) {
return this.entries[index];
}
/**
* This method returns the number of entries in the list.
*
* @return The number of entries in the list.
*/
public int size() {
return this.size;
}
/**
* This method will create a new list double the size of the previous, and copy all values from the old list
* to the new list.
*/
private void resize() {
Entry[] newList = new Entry[this.entries.length * 2];
for (int i = 0 ; i < this.size ; i++) {
newList[i] = this.entries[i];
}
this.entries = newList;
}
public String toString() {
String result = "";
String formatter = "%-20s%-1d";
for (int i = 0 ; i < this.size ; i++) {
Entry e = this.entries[i];
result += String.format(formatter, e.getWord(), e.getCount()) + " ";
}
return result;
}
}
------------------------------ CLASS HashTable ------------------------
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class HashTable {
//variable
ArrayList<String> numbers = new ArrayList<> ();
//constructor
public HashTable(ArrayList<String> numbers){
this.numbers = numbers;
}
public SimpleList generateSimpleList(){
SimpleList obj = new SimpleList();
for(int i = 0; i < numbers.size(); i++){
// add number
obj.add(new Entry(numbers.get(i)));
}
return obj;
}
private SimpleList generateHashTable(){
SimpleList obj = new SimpleList();
for(int i =0; i< numbers.size(); i++){
// add number
Entry entry = new Entry(numbers.get(i));
// get hash code
int hash = entry.hashCode(numbers.get(i), 1);
if(obj.getEntry(hash)!= null){ // get hash code success
obj.entries[hash] = entry;
}
else{
hash = entry.hashCode(numbers.get(i),2);
obj.entries[hash] = entry;
}
}
return obj;
}
// main function
public static void main(String args[]){
// read file
JFileChooser jfchooser = new JFileChooser();
ArrayList<String> list = new ArrayList<>();
//jfchooser.showOpenDialog(null);
int result = jfchooser.showOpenDialog(null);
if( result == JFileChooser.APPROVE_OPTION){
try{
//FileReader fr = new FileReader()
FileReader fr = null;
JOptionPane.showMessageDialog(null, "Select File");
File file = jfchooser.getSelectedFile();
fr = new FileReader(file);
int num = 1;
while (num != -1) {
num = fr.read();
list.add(String.valueOf(num));
}
}catch(FileNotFoundException ex){
}catch(IOException ex){
}
}
// creating object
HashTable obj = new HashTable(list);
obj.generateSimpleList();
obj.generateHashTable();
}
}
Explanation / Answer
public category TTTConsoleNonOO2P cipher = 2;
// Name-constants to represent the varied states of the sport
public static final int enjoying = 0;
public static final int DRAW = 1;
public static final int CROSS_WON = 2;
public static final int NOUGHT_WON = 3;
// the sport board and also the game standing
public static final int ROWS = three, COLS = 3; // range of rows and columns
public static int[][] board = new int[ROWS][COLS]; // game board in 2nd array
// containing (EMPTY, CROSS, NOUGHT)
public static int currentState; // this state of the sport
// (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
public static int currentPlayer; // this player (CROSS or NOUGHT)
public static int currntRow, currentCol; // current seed's row and column
public static Scanner in = new Scanner(System.in); // the input Scanner
/** The entry main methodology (the program starts here) */
public static void main(String[] args) {
// Initialize the game-board and current standing
initGame();
// Play the sport once
do else if (isDraw())
// Otherwise, no amendment to currentState (still PLAYING).
}
/** come back true if it's a draw (no additional empty cell) */
// TODO: Shall declare draw if no player will "possibly" win
public static Boolean isDraw() mountain pass = 0; gap < COLS; ++col) {
if (board[row][col] == EMPTY) {
come back false; // AN empty cell found, not draw, exit
}
}
}
come back true; // no empty cell, it is a draw
}
/** come back true if the player with "theSeed" has won once inserting at
(currentRow, currentCol) */
public static Boolean hasWon(int theSeed, int currentRow, int currentCol) {
come back (board[currentRow][0] == theSeed // 3-in-the-row
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == two // 3-in-the-opposite-diagonal
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
/** Print the sport board */
public static void printBoard() mountain pass = 0; gap < COLS; ++col) every of the cells
if (col != COLS - 1)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.