Skip to content

Instantly share code, notes, and snippets.

@anikseu
Created March 7, 2018 19:07
Show Gist options
  • Select an option

  • Save anikseu/d72acddbc5b346c6ab801ceb50b84ed3 to your computer and use it in GitHub Desktop.

Select an option

Save anikseu/d72acddbc5b346c6ab801ceb50b84ed3 to your computer and use it in GitHub Desktop.
/*
* File Chooser Homework
* Text Analyzer Project
* CSE2015 - Programming Language 2 Java (KMH)
* Southeast University
*/
package text.analyzer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
/**
*
* @author kmhasan
*/
public class FXMLDocumentController implements Initializable {
@FXML
private TextArea textArea;
@FXML
private TextField filenameTextField;
@FXML
private Button fileOpenButton;
@FXML
private TextField wordsTextField;
@Override
public void initialize(URL url, ResourceBundle rb) {
// String text = "Hello\nWorld\n";
// textArea.setText(text);
// textArea.appendText("Here goes another line");
// wordsTextField.setText("1");
}
@FXML
private void handleFileOpenAction(ActionEvent event) {
// Homework: try to use FileChooser
FileChooser fc = new FileChooser();
File fname = fc.showOpenDialog(null);
if(fname!=null){
filenameTextField.setText(fname.getName());
}
else{
filenameTextField.setText("Try again!");
}
String filename = filenameTextField.getText();
textArea.clear();
try {
RandomAccessFile input = new RandomAccessFile(filename, "r");
String line;
String filecontent = "";
while (true) {
line = input.readLine();
if (line == null)
break;
// The following line is ugly!
// We should use StringBuilder instead
filecontent = filecontent + " " + line;
textArea.appendText(line + "\n");
}
int words = countWords(filecontent);
wordsTextField.setText("" + words);
} catch (FileNotFoundException ex) {
System.err.println("File does not exist");
} catch (IOException ex) {
System.err.println("Some I/O exception occured");
}
}
private int countWords(String filecontent) {
// For future:
// Once you've completed CSE3025, revisit this problem
// You need to know what Regular Expressions are
int count = 0;
while (filecontent.contains(" "))
filecontent = filecontent.replaceAll(" ", " ");
filecontent = filecontent.trim();
for (int i = 0; i < filecontent.length(); i++)
if (filecontent.charAt(i) == ' ' || filecontent.charAt(i) == ',')
count++;
if (filecontent.length() != 0)
return count + 1;
else return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment