I want to get a little help with a school assignment. I have a method that isn't acting the way I thought it would. I'm pretty sure the problem is in nlpClassify(), but I'm not entirely sure. Currently, clpClassify() will only print out 0s for the variables. Any suggestions are welcome.
public class TweetHandler implements TweetHandlerInterface {
private Scanner inFile = null;
private String tweet = null;
private String[] temp = null;
private int counter = 0;
List<AbstractTweet> tweetList = new ArrayList<AbstractTweet>();
List<AbstractTweet> dataList = new ArrayList<AbstractTweet>();
List<String[]> dataBaseList = new ArrayList<String[]>();
#Override
public List<AbstractTweet> loadTweetsFromText(String filePath) {
MainApp main = null;
try {
inFile = new Scanner ( new FileReader(filePath));
}
catch( FileNotFoundException er ){
System.err.println(er);
main.printMenu();
}
String line = null;
//String[] data = null;
//int counter = 0;
while ( inFile.hasNextLine() ) {
line = inFile.nextLine();
parseTweetLine(line);
counter++;
}
System.out.println("Reading tweets from file..."
+ "\n" + counter + " tweets read.");
inFile.close();
return tweetList;
}
#Override
public AbstractTweet parseTweetLine(String tweetLine) {
temp = tweetLine.split("\",\"");
tweet = temp[5];
if( temp.length > 6 ){
for( int i = 0; i < temp.length; i++ ){
tweet += " " + temp[i];
}
}
temp[0] = temp[0].replaceAll("\"","");
tweet = tweet.replaceAll("\\p{Punct}", "");
temp[5] = tweet;
int target = Integer.parseInt(temp[0]);
int id = Integer.parseInt(temp[1]);
SimpleDateFormat format = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy");
Date date = new Date();
try{
date = format.parse(temp[2]);
}
catch( ParseException e ){
System.err.println("Error with date parsing. " + e);
}
String flag = temp[3];
String user = temp[4];
String text = temp[5];
Tweet tweets = new Tweet(target,id,date,flag,user,text);
return tweets;
}
/**
* calls classifyTweet
*/
public void nlpClassify(){ //prints out accuracy
System.out.println("Classifying tweets...");
counter = 0;
int correct = 0,
wrong = 0;
float accuracy = 0.0f;
AbstractTweet tweets;
for( int i = 0; i < tweetList.size(); i++ ){
tweets = tweetList.get(i);
tweets.setPredictedPolarity(classifyTweet(tweets));
int target = tweets.getTarget();
int polarity = tweets.getPredictedPolarity();
System.out.println("Target: " + target );
System.out.println("Prediction: " + polarity );
for( int j = 0; j < 75; j++ ){
System.out.print("=");
}
System.out.println();
if( target == polarity ){
correct++;
}
else{
wrong++;
}
counter++;
accuracy = ( correct / counter ) * 100.0f;
}
System.out.println( "Classified " + counter + " tweets." );
System.out.println( "Correct tweets: " + correct );
System.out.println( "Wrong tweets: " + wrong );
System.out.println( "Accuracy: " + accuracy );
}
#Override
public int classifyTweet(AbstractTweet tweet) {
int calcPolarity = SentimentAnalyzer.getParagraphSentiment( tweet.getText() );
tweet.setPredictedPolarity(calcPolarity);
return calcPolarity;
}
#Override
public void addTweetsToDB(List<AbstractTweet> tweets) {
System.out.println("Adding files to database... ");
int i = 0;
while( i < tweets.size()){
dataList.add(tweets.get(i));
i++;
}
}
#Override
public void deleteTweet(int id) {
int i = 0,
temp = 0;
Tweet obj = null;
while( i < dataList.size() ){
temp = obj.getId();
if( id == temp ){
dataList.remove(i);
--counter;
}
++i;
}
}
#Override
public void saveSerialDB() {
try{
FileOutputStream fileOut = new FileOutputStream("DB.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(dataList);
out.close();
fileOut.close();
}
catch( IOException e ){
e.printStackTrace();
}
}
#Override
public void loadSerialDB() {
try{
FileInputStream fileIn = new FileInputStream("DB.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
dataList = (List<AbstractTweet>)in.readObject();
in.close();
fileIn.close();
}
catch( Exception e ){
e.printStackTrace();
}
}
#Override
public List<AbstractTweet> searchByUser(String user) {
int i = 0;
String temp = null;
Tweet obj = null;
while( i < dataList.size() ){
temp = obj.getUser();
if( user.equals(temp) )
dataList.add(obj);
++i;
}
return dataList;
}
Ignore searchByUser(String user), I know it's wrong and haven't gotten around to fixing it. If you have suggestions for it, that's cool too.
Here is the interface.
public interface TweetHandlerInterface {
/**
* Loads tweets from a CSV text file.
* #param filePath The path to the CSV file.
* #return A list of tweets as objects.
*/
List<AbstractTweet> loadTweetsFromText(String filePath);
/**
* Parses a single line from the CSV file and returns a tweet as an object.
* #param tweetLine A string containing the contents of a single line in the CSV file.
* #return A tweet as an object.
*/
AbstractTweet parseTweetLine(String tweetLine);
/**
* Classifies a tweet as negative, neutral, or positive by using the text of the tweet.
* #param tweet A tweet object.
* #return 0 = negative, 2 = neutral, 4 = positive.
*/
int classifyTweet(AbstractTweet tweet);
/**
* Adds a list of new tweets to the existing database.
* #param tweets A list of tweet objects.
*/
void addTweetsToDB(List<AbstractTweet> tweets);
/**
* It deletes ad tweet from the database, given its id.
* #param id The id value of the tweet.
*/
void deleteTweet(int id);
/**
* Saves the database in the working directory as a serialized object.
*/
void saveSerialDB();
/**
* Loads tweet database.
*/
void loadSerialDB();
/**
* Searches the tweet database by user name. It returns a list of all tweets
* matching the given user name.
* #param user The user name to search for.
* #return A list of tweet objects.
*/
List<AbstractTweet> searchByUser(String user);
/**
* Searches the tweet database for tweets posted on a given date.
* #param date The date to search for.
* #return A list of tweet objects.
*/
List<AbstractTweet> searchByDate(Date date);
/**
* Searches the tweet database for tweets matching a given flag.
* #param flag The flag to search for.
* #return A list of tweet objects.
*/
List<AbstractTweet> searchByFlag(String flag);
/**
* Searches the tweet database for tweets matching a given substring.
* #param substring The substring to search for.
* #return A list of tweet objects.
*/
List<AbstractTweet> searchBySubstring(String substring);
}
Here is my Main().
public class MainApp {
public static void main (String[] args){
TweetHandler handler = new TweetHandler();
MainApp obj = new MainApp();
Tweet tweetObj = null;
String choice;
do{
obj.printMenu();
System.out.print("Enter a choice from above: ");
Scanner inputConsole = new Scanner( System.in );
choice = inputConsole.next();
String filePath = null;
switch( choice ){
case "0":
System.exit(0);
break;
case "1":
Scanner file = new Scanner ( System.in );
System.out.print("Enter the input file path: ");
filePath = file.nextLine();
handler.loadTweetsFromText(filePath);
break;
case "2":
handler.classifyTweet(handler.parseTweetLine(filePath));
handler.nlpClassify();
break;
case "3":
System.out.println("Enter ID of the tweet you wish to change: ");
break;
case "4":
System.out.println("Adding tweets to database...");
handler.addTweetsToDB(handler.loadTweetsFromText(filePath));
break;
case "5":
Scanner removeChoice = new Scanner( System.in );
System.out.println("Enter ID of tweet you wish to delete: ");
int remove = removeChoice.nextInt();
handler.deleteTweet(remove);
break;
case "6":
Scanner searching = new Scanner( System.in );
System.out.println("Search by: 1 User, 2 Date, 3 Flag, 4 Substring.");
int search = searching.nextInt();
switch(search){
case 1:
case 2:
case 3:
case 4:
}
break;
}
} while ( !"0".equals(choice) );
}
void printMenu(){
System.out.println("0. Exit program.\n"
+ "1. Load new tweet text file.\n"
+ "2. Classify tweets using NLP library and report accuracy.\n"
+ "3. Manually change tweet class label.\n"
+ "4. Add new tweets to database.\n"
+ "5. Delete tweet from database (given its id).\n"
+ "6. Search tweets by user, date, flag, or a matching substring.\n");
}
}
You loop for with tweetList.size(). You can print out tweetList.size() to see the value. I guess it's 0. I didn't see any tweetList.add(obj) in your code. So tweetList is empty all the time.
Related
I have a requirement to write Java program where I should read daily inventory feeds from suppliers, extract the data and add to the database. The feeds are different for each supplier and only in csv, txt or excel formats. Some suppliers may have extra columns in the feed, but they all guarantee to provide one column containing the product ID and one column containing the quantity (whose column indices can vary from supplier to supplier). Each product ID appears only once in a file. Part of the problem statement is dealing with data, sometimes placed in different positions within the feed for various suppliers.
I wrote below code assuming the columns are always named “product” and “quantity”. But any suggestions on how to handle this part of problem in a better way so that it is flexible to handle continued increase in the number of suppliers and their feeds in the future?
package com.file.handling;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class Inventory {
private ArrayList<String> fileNames = new ArrayList<>();
public Inventory(ArrayList<String> fileNames) {
this.fileNames = fileNames;
}
/**
* This is the main method that initiates file read to get data
* #return Nothing.
* #exception FileNotFoundException On file not found.
* #see FileNotFoundException
*/
public void addItems(){
for (String filename: fileNames){
try{
URL url = getClass().getResource(filename);
File myObj = new File(url.getPath());
Scanner myReader = new Scanner(myObj);
ArrayList<String> lines = new ArrayList<>();
while (myReader.hasNextLine()){
lines.add(myReader.nextLine());
}
extractData(lines, myObj.getName());
myReader.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
/**
* This is a private method which extracts productID, suppleirID and Quantity values from data passed
* #param lines arraylist of string data
* #param filename
* #return Nothing.
*/
private void extractData(ArrayList<String> lines, String filename) {
// Get fieldNames
String fieldNameString = lines.remove(0);
String[] fieldNames;
// Get supplierID and file type
String supplierID = filename.split("\\.")[0];
String fileType = filename.split("\\.")[1];
// Get index of fieldName product and quantity/inventory based on file type
int productIdx = -1;
int quantityIdx = -1;
if (fileType.equals("csv") || filename.equals("xlsb") || fileType.equals("xls") || fileType.equals("xlsx") || fileType.equals("xlsm")){
fieldNames = fieldNameString.split(",");
for (int i=0; i<fieldNames.length; i++){
String fieldName = fieldNames[i];
if (fieldName.toLowerCase().equals("product"))
productIdx = i;
else if (fieldName.toLowerCase().equals("inventory") || fieldName.toLowerCase().equals("quantity"))
quantityIdx = i;
}
// loop through remaining data to get productIDs and their quantities
for (String line: lines){
String productID = line.split(",")[productIdx];
int quantity = Integer.valueOf(line.split(",")[quantityIdx]);
System.out.println("ProductID: " + productID + " quantity: " + quantity + " SupplierID: " + supplierID);
// create SupplierProduct Object
SupplierProduct supplierProduct = new SupplierProduct(supplierID, productID, quantity);
// WriteToDB - use supplierProduct to write to database
}
} else if (fileType.equals("tsv") || fileType.equals("tab")) {
fieldNames = fieldNameString.split("\\s+");
for (int i=0; i<fieldNames.length; i++){
String fieldName = fieldNames[i];
if (fieldName.toLowerCase().equals("product"))
productIdx = i;
else if (fieldName.toLowerCase().equals("inventory") || fieldName.toLowerCase().equals("quantity"))
quantityIdx = i;
}
// loop through remaining data to get productIDs and their quantities
for (String line: lines){
String productID = line.split("\\s+")[productIdx];
int quantity = Integer.valueOf(line.split("\\s+")[quantityIdx]);
System.out.println("ProductID: " + productID + " Quantity: " + quantity + " SupplierID: " + supplierID);
// create SupplierProduct Object
SupplierProduct supplierProduct = new SupplierProduct(supplierID, productID, quantity);
// WriteToDB - use supplierProduct to write to database
}
}
}
}
class SupplierProduct {
private String supplierID;
private String productID;
private int quantity;
public SupplierProduct(String supplierID, String productID, int quantity) {
this.supplierID = supplierID;
this.productID = productID;
this.quantity = quantity;
}
}
Good day!
I have created a code using Netbeans and it executes the processes just fine.
Now, i want my input to given and output to be displayed through a user interface. I have then created a 2 Jframes, 1 to collect the user's input and the other to display the results after execution by the code.
But, i am unable to link the interface to the main class(called NgramBetaE) as i am not aware of how i can do so.
I highly welcome suggestions.
The main class in its entirety is;
package ngrambetae;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
*
* #author 201102144
*/
public class NgramBetaE {
static LinkedList<String> allWords = new LinkedList<String>();
static LinkedList<String> distinctWords = new LinkedList<String>();
static String[] hashmapWord = null;
static int wordCount;
public static HashMap<String,HashMap<String, Integer>> hashmap = new HashMap<>();
public static HashMap<String,HashMap<String, Integer>> bigramMap = new HashMap<>();
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
//prompt user input
Scanner input = new Scanner(System.in);
//read words from collected corpus; a number of .txt files
File directory = new File("Corpus");
File[] listOfFiles = directory.listFiles();//To read from all listed iles in the "directory"
int lineNumber = 0;
String line;
String files;
String delimiters = "[()?!:;,.\\s]+";
//reading from a list of text files
for (File file : listOfFiles) {
if (file.isFile()) {
files = file.getName();
try {
if (files.endsWith(".txt") || files.endsWith(".TXT")) { //ensures a file being read is a text file
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
line = line.toLowerCase();
hashmapWord = line.split(delimiters);
//CALCULATING UNIGRAMS
for(int s = 0; s < hashmapWord.length; s++){
String read = hashmapWord[s];
allWords.add(read);
//count the total number of words in all the text files combined
//TEST
wordCount = 0;
for (int i = 0; i < allWords.size(); i++){
wordCount ++;
}
}
//CALCULATING BIGRAM FREQUENCIES
for(int s = 0; s < hashmapWord.length -1; s++){
String read = hashmapWord[s];
final String read1 = hashmapWord[s + 1];
HashMap<String, Integer> counter = bigramMap.get(read);
if (null == counter) {
counter = new HashMap<String, Integer>();
bigramMap.put(read, counter);
}
Integer count = counter.get(read1);
counter.put(read1, count == null ? 1 : count + 1);
}
//CALCULATING TRIGRAM FREQUENCIES
for(int s = 0; s < hashmapWord.length - 2; s++){
String read = hashmapWord[s];
String read1 = hashmapWord[s + 1];
final String read2 = hashmapWord[s + 2];
String readTrigrams = read + " " + read1;
HashMap<String, Integer> counter = hashmap.get(readTrigrams);
if (null == counter) {
counter = new HashMap<String, Integer>();
hashmap.put(readTrigrams, counter);
}
Integer count = counter.get(read2);
counter.put(read2, count == null ? 1 : count + 1);
}
}
br.close();
}
} catch (NullPointerException | IOException e) {
e.printStackTrace();
System.out.println("Unable to read files: " + e);
}
}
}
//COMPUTING THE TOTAL NUMBER OF WORDS FROM ALL THE TEXT FILES COMBINED
System.out.println("THE TOTAL NUMBER OF WORDS IN COLLECTED CORPUS IS : \t" + wordCount + "\n");
for(int i = 0, size = allWords.size(); i < size; i++){
String distinctWord = allWords.get(i);
//adding a word into the 'distinctWords' list if it doesn't already occur
if(!distinctWords.contains(distinctWord)){
distinctWords.add(distinctWord);
}
}
//PRINTING THE DISTINCT WORDS
System.out.println("THE DISTINCT WORDS IN TOTAL ARE :\t " + distinctWords.size() + "\n");
System.out.println("PRINTING CONTENTS OF THE BIGRAMS HASHMAP... ");
System.out.println(bigramMap);
System.out.println("================================================================================================================================================================================================================================================================================================================\n");
System.out.println("PRINTING CONTENTS OF THE TRIGRAMS HASHMAP... ");
System.out.println(hashmap);
System.out.println("================================================================================================================================================================================================================================================================================================================\n");
//QUITTING APPLICATION
String userInput = null;
while(true) {
System.out.println("\n**********************************************************************************************************************************************************************************************************************************");
System.out.println("\n\n\t\tPLEASE ENTER A WORD OR PHRASE YOU WOULD LIKE A PREDICTION OF THE NEXT WORD FROM:");
System.out.println("\t\t\t\t(OR TYPE IN 'Q' OR 'q' TO QUIT)");
userInput = input.nextLine();
if (userInput.equalsIgnoreCase("Q")) break;
//FORMAT USER INPUT
String[] users = userInput.toLowerCase().split("[?!,.\\s]+");
if (users.length < 2) {
userInput = users[0];
//System.out.println("\nENTRY '" + userInput + "' IS TOO SHORT TO PREDICT NEXT WORD. PLEASE ENTER 2 OR MORE WORDS");
//CALCULATING BIGRAM PROBABILITY
int sum = 0;
try {
for(String s : bigramMap.get(userInput).keySet()) {
sum += bigramMap.get(userInput).get(s);
}
String stringHolder = null;
double numHolder = 0.0;
for(String s : bigramMap.get(userInput).keySet()) {
//System.out.println("TWO");
double x = Math.round(bigramMap.get(userInput).put(s, bigramMap.get(userInput).get(s))/ (double)sum *100 );
if(s != null){
if(numHolder < x ){
stringHolder = s;
numHolder = x;
}
}
}
System.out.println("\nNEXT WORD PREDICTED IS '" + stringHolder + "'");
System.out.println("ITS PROBABILITY OF OCCURRENCE IS " + numHolder + "%");
} catch (Exception NullPointerException) {
System.out.println("\nSORRY. MATCH NOT FOUND.");
}
} else {
userInput = users[users.length - 2] + " " + users[users.length - 1];
// System.out.println("FROM USER WE GET....");
// System.out.println(bigrams.get(userInput).keySet());
/* CALCULATING TRIGRAM PROBABILITY*/
int sum = 0;
try {
for(String s : hashmap.get(userInput).keySet()) {
sum += hashmap.get(userInput).get(s);
}
String stringHolder = null;
double numHolder = 0.0;
for(String s : hashmap.get(userInput).keySet()) {
//System.out.println("TWO");
double x = Math.round(hashmap.get(userInput).put(s, hashmap.get(userInput).get(s))/ (double)sum *100 );
if(s != null){
if(numHolder < x ){
stringHolder = s;
numHolder = x;
}
}
}
System.out.println("\nNEXT WORD PREDICTED IS '" + stringHolder + "'");
System.out.println("ITS PROBABILITY OF OCCURRENCE IS " + numHolder + "%");
} catch (Exception NullPointerException) {
System.out.println("\nSORRY. MATCH NOT FOUND.");
}
}
}
input.close();
}
}
My first Jframe which i would like to appear upon running the project has got a single textbox and a single button;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String usersInput = jTextField1.getText();
Interface1 s = new Interface1();
s.setVisible(true);
dispose();
}
i would like for the user to enter data in the textbox and when they click on the button 'predict next word' then the output from the code execution is displayed on the second jframe which has got 3 labels and relative text areas.
NOTE; i couldn't paste the screenshots but if you run the NgramBetaE class you will get an idea of how the interfaces will be as i tried to explain them.
Thank you
Don't even try to link your GUI code to your NgramBetaE code as you've more work to do since the NgramBetaE is little more than one huge static main method that gets user input from the console with a Scanner and outputs to the console via printlns. Melding these two is like trying to put a square peg into a round hole.
Instead re-write the whole thing with an eye towards object-oriented coding, including creation of an OOP-compliant model class with instance fields and methods, and a single GUI that gets the input and displays it, that holds an instance of the model class and that calls instance methods on this instance.
Consider creating non-GUI classes and methods for --
Reading in data from your text files
Analyzing and hashing the data held in the text files including calculating word frequencies etc...
Returning needed data after analysis in whatever data form it may be needed.
A method for allowing input of a String/phrase for testing, with return its predicted probability
Then create GUI code for:
Getting selected text file from the user. A JFileChooser and supporting code works well here.
Button to start analysis
JTextField to allow entering of phrase
JTextArea or perhaps JTable to display results of analysis
Note that you should avoid having more than one JFrame in your GUI. For more on this, please have a look at The Use of Multiple JFrames, Good/Bad Practice?
this may be a simple fix and a silly question but I am still new to learning java. I am currently working on a code for class that bases on a text game. The code below is what was given to us out of our text book to work on the game. I changed the actual text in the text file mainly and did not do much to the code but I am receiving the following error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at BradySkuza58.getRoom(BradySkuza58.java:143)
at BradySkuza58.loadRoomsFromFile(BradySkuza58.java:90)
at BradySkuza58.main(BradySkuza58.java:39)
This is the code I used for the text game.
import java.util.Scanner;
class Room
{
int roomNumber;
String roomName;
String description;
int numExits;
String[] exits = new String[20];
int[] destinations = new int[20];
}
public class BradySkuza58
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
// initialize rooms from file
Room[] rooms = loadRoomsFromFile("textadventurerooms.txt");
//showAllRooms(rooms); // for debugging
// Okay, so let's play the game!
int currentRoom = 0;
String ans;
while ( currentRoom >= 0 )
{
Room cur = rooms[currentRoom];
System.out.print( cur.description );
System.out.print("> ");
ans = keyboard.nextLine();
// See if what they typed matches any of our exit names
boolean found = false;
for ( int i=0; i<cur.numExits; i++ )
{
if ( cur.exits[i].equals(ans) )
{
found = true;
// if so, change our next room to that exit's room number
currentRoom = cur.destinations[i];
}
}
if ( ! found )
System.out.println("Sorry, I don't understand.");
}
}
public static Room[] loadRoomsFromFile( String filename )
{
Scanner file = null;
try
{
file = new Scanner(new java.io.File(filename));
}
catch ( java.io.IOException e )
{
System.err.println("Sorry, I can't read from the file '" +filename + "'.");
System.exit(1);
}
int numRooms = file.nextInt();
Room[] rooms = new Room[numRooms];
// initialize rooms from file
int roomNum = 0;
while ( file.hasNext() )
{
Room r = getRoom(file);
if ( r.roomNumber != roomNum )
{
System.err.println("Reading room # " + r.roomNumber + ", but" + roomNum + " was expected.");
System.exit(2);
}
rooms[roomNum] = r;
roomNum++;
}
file.close();
return rooms;
}
public static void showAllRooms( Room[] rooms )
{
for ( Room r : rooms )
{
String exitString = "";
for ( int i=0; i<r.numExits; i++ )
exitString += "\t" + r.exits[i] + " (" + r.destinations[i] +")";
System.out.println( r.roomNumber + ") " + r.roomName + "\n" +exitString );
}
}
public static Room getRoom( Scanner f )
{
// any rooms left in the file?
if ( ! f.hasNextInt() )
return null;
Room r = new Room();
String line;
// read in the room # for errorchecking later
r.roomNumber = f.nextInt();
f.nextLine(); // skip "\n" after room #
r.roomName = f.nextLine();
// read in the room's description r.description = "";
while ( true )
{
line = f.nextLine();
if ( line.equals("%%") )
break;
r.description += line + "\n";
}
// finally, we'll read in the exits
int i = 0;
while ( true )
{
line = f.nextLine();
if ( line.equals("%%") )
break;
String[] parts = line.split(":");
r.exits[i] = parts[0];
r.destinations[i] = Integer.parseInt(parts[1]);
i++;
}
r.numExits = i;
// should be done; return the Room
return r;
}
}
line 39
Room[] rooms = loadRoomsFromFile("textadventurerooms.txt");
line 90
Room r = getRoom(file);
line 143
line = f.nextLine();
I did not attach the text file itself but I can if it is needed to find the error.
I'm guessing that your text file does not have as many lines are your program expects (you are trying to call nextLine() after already reaching EOF. Double check that your text file matches what you want. Not having the text file it is tough to give more detail. Based on where it is make sure that you are breaking out of that while loop. You can also check to see if there is another line by calling hasNextLine().
Add a line "%%" to your room definition file. Your parser requires that.
I'm doing a Phone Directory project and we have to read from a directory file telnos.txt
I'm using a Scanner to load the data from the file telnos.txt, using a loadData method from a previous question I asked here on StackOverflow.
I noticed attempts to find a user always returned Not Found, so I added a few System.out.printlns in the methods to help me see what was going on. It looks like the scanner isn't reading anything from the file. Weirdly, it is printing the name of the file as what should be the first line read, which makes me think I've missed something very very simple here.
Console
run:
telnos.txt
null
loadData tested successfully
Please enter a name to look up: John
-1
Not found
BUILD SUCCESSFUL (total time: 6 seconds)
ArrayPhoneDirectory.java
import java.util.*;
import java.io.*;
public class ArrayPhoneDirectory implements PhoneDirectory {
private static final int INIT_CAPACITY = 100;
private int capacity = INIT_CAPACITY;
// holds telno of directory entries
private int size = 0;
// Array to contain directory entries
private DirectoryEntry[] theDirectory = new DirectoryEntry[capacity];
// Holds name of data file
private final String sourceName = "telnos.txt";
File telnos = new File(sourceName);
// Flag to indicate whether directory was modified since it was last loaded or saved
private boolean modified = false;
// add method stubs as specified in interface to compile
public void loadData(String sourceName) {
Scanner read = new Scanner("telnos.txt").useDelimiter("\\Z");
int i = 1;
String name = null;
String telno = null;
while (read.hasNextLine()) {
if (i % 2 != 0)
name = read.nextLine();
else
telno = read.nextLine();
add(name, telno);
i++;
}
}
public String lookUpEntry(String name) {
int i = find(name);
String a = null;
if (i >= 0) {
a = name + (" is at position " + i + " in the directory");
} else {
a = ("Not found");
}
return a;
}
public String addChangeEntry(String name, String telno) {
for (DirectoryEntry i : theDirectory) {
if (i.getName().equals(name)) {
i.setNumber(telno);
} else {
add(name, telno);
}
}
return null;
}
public String removeEntry(String name) {
for (DirectoryEntry i : theDirectory) {
if (i.getName().equals(name)) {
i.setName(null);
i.setNumber(null);
}
}
return null;
}
public void save() {
PrintWriter writer = null;
// writer = new PrintWriter(FileWriter(sourceName));
}
public String format() {
String a;
a = null;
for (DirectoryEntry i : theDirectory) {
String b;
b = i.getName() + "/n";
String c;
c = i.getNumber() + "/n";
a = a + b + c;
}
return a;
}
// add private methods
// Adds a new entry with the given name and telno to the array of
// directory entries
private void add(String name, String telno) {
System.out.println(name);
System.out.println(telno);
theDirectory[size] = new DirectoryEntry(name, telno);
size = size + 1;
}
// Searches the array of directory entries for a specific name
private int find(String name) {
int result = -1;
for (int count = 0; count < size; count++) {
if (theDirectory[count].getName().equals(name)) {
result = count;
}
System.out.println(result);
}
return result;
}
// Creates a new array of directory entries with twice the capacity
// of the previous one
private void reallocate() {
capacity = capacity * 2;
DirectoryEntry[] newDirectory = new DirectoryEntry[capacity];
System.arraycopy(theDirectory, 0, newDirectory,
0, theDirectory.length);
theDirectory = newDirectory;
}
}
ArrayPhoneDirectoryTester.java
import java.util.Scanner;
public class ArrayPhoneDirectoryTester {
public static void main(String[] args) {
//create a new ArrayPhoneDirectory
PhoneDirectory newTest = new ArrayPhoneDirectory();
newTest.loadData("telnos.txt");
System.out.println("loadData tested successfully");
System.out.print("Please enter a name to look up: ");
Scanner in = new Scanner(System.in);
String name = in.next();
String entryNo = newTest.lookUpEntry(name);
System.out.println(entryNo);
}
}
telnos.txt
John
123
Bill
23
Hello
23455
Frank
12345
Dkddd
31231
In your code:
Scanner read = new Scanner("telnos.txt");
Is not going to load file 'telnos.txt'. It is instead going to create a Scanner object that scans the String "telnos.txt".
To make the Scanner understand that it has to scan a file you have to either:
Scanner read = new Scanner(new File("telnos.txt"));
or create a File object and pass its path to the Scanner constructor.
In case you are getting "File not found" errors you need to check the current working directory. You could run the following lines and see if you are indeed in the right directory in which the file is:
String workingDir = System.getProperty("user.dir");
System.out.println("Current working directory : " + workingDir);
You need to also catch the FileNotFoundException in the function as follows:
public void loadData(String sourceName) {
try {
Scanner read = new Scanner(new File("telnos.txt")).useDelimiter("\\Z");
int i = 1;
String name = null;
String telno = null;
while (read.hasNextLine()) {
if (i % 2 != 0)
name = read.nextLine();
else {
telno = read.nextLine();
add(name, telno);
}
i++;
}
}catch(FileNotFoundException ex) {
System.out.println("File not found:"+ex.getMessage);
}
}
You are actually parsing the filename not the actual file contents.
Instead of:
new Scanner("telnos.txt")
you need
new Scanner( new File( "telnos.txt" ) )
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
I have written a Java program that analyze .soft file of data on gene expressions and write it to txt
package il.ac.tau.cs.sw1.bioinformatics;
import org.apache.commons.math3.stat.inference.TestUtils;
import java.io.*;
import java.util.Arrays;
/**
*
* Gene Expression Analyzer
*
* Command line arguments:
* args[0] - GeoDatasetName: Gene expression dataset name (expects a corresponding
input file in SOFT format to exist in the local directory).
* args[1] - Label1: Label of the first sample subset
* args[2] - Label2: Label of the second sample subset
* args[3] - Alpha: T-test confidence level : only genes with pValue below this
threshold will be printed to output file
*
* Execution example: GeneExpressionAnalyzer GDS4085 "estrogen receptor-negative" "estrogen receptor-positive" 0.01
*
* #author software1-2014
*
*/
public class GeneExpressionAnalyzer {
public static void main(String args[]) throws IOException {
// Reads the dataset from a SOFT input file
String inputSoftFileName = args[0] + ".soft";
GeneExpressionDataset geneExpressionDataset = parseGeneExpressionFile (inputSoftFileName);
System.out.printf ("Gene expression dataset loaded from file %s. %n",inputSoftFileName);
System.out.printf("Dataset contains %d samples and %d gene probes.%n%n",geneExpressionDataset.samplesNumber, geneExpressionDataset.genesNumber);
// Writes the dataset to a tabular format
String tabularFileName = args[0] + "-Tabular.txt";
writeDatasetToTabularFile(geneExpressionDataset,tabularFileName);
System.out.printf ("Dataset saved to tabular file - %s.%n%n",tabularFileName);
// Identifies differentially expressed genes between two sample groups and writes the results to a text file
String label1 = args[1];
String label2 = args[2];
double alpha = Double.parseDouble(args[3]);
String diffGenesFileName = args[0] + "-DiffGenes.txt";
int numOfDiffGenes = writeTopDifferentiallyExpressedGenesToFile(diffGenesFileName,geneExpressionDataset, alpha, label1, label2);
System.out.printf ("%d differentially expressed genes identified using alpha of %f when comparing the two sample groups [%s] and [%s].%n",numOfDiffGenes, alpha, label1, label2);
System.out.printf ("Results saved to file %s.%n",diffGenesFileName);
}
private static float[] StringtoFloat(String[] temp) {
float[] array = new float[temp.length];
for (int i = 0; i < temp.length; i++){
array[i]= Float.parseFloat(temp[i]);
}
return array;
}
private static double[] CutToCounter(double[] array, int counter) {
if (array.length == counter){
return array;
}
double[] args = new double[counter+1];
for (int i = 0; i < args.length; i++){
args[i] = array[i];
}
return args;
}
private static int min(double[] pValues) {
double val = 2;
int index = -1;
for (int i = 0; i < pValues.length; i++){
if (pValues[i] < val && pValues[i] != 3.0){
val = pValues[i];
index = i;
}
}
return index;
}
private static String changeformat(float[] array) {
String[] args = new String[array.length];
for (int i = 0; i < array.length; i++){
args[i] = String.format("%.2f", array[i]);
}
return Arrays.toString(args);
}
/**
*
* parseGeneExpressionFile - parses the given SOFT file
*
*
* #param filename A gene expression file in SOFT format
* #return a GeneExpressionDataset object storing all data parsed from the input file
* #throws IOException
*/
public static GeneExpressionDataset parseGeneExpressionFile (String filename) throws IOException {
GeneExpressionDataset dataset = new GeneExpressionDataset();
BufferedReader buf = new BufferedReader(new FileReader(filename));
String line = buf.readLine();
String[] geneids = null;
String[] genesymbols = null;
float[][] datamatrix = null;
String[][] subsetinfo = new String[10][2];
String[][] subsetsample = new String[10][];
int i = 0;
int j = 0;
boolean bol = false;
while (line != null){
if (line.startsWith("!dataset_sample_count")){
dataset.samplesNumber = Integer.parseInt(line.substring(24));
}
else if (line.startsWith("!dataset_sample_count")){
dataset.genesNumber = Integer.parseInt(line.substring(25));
geneids = new String[dataset.genesNumber];
genesymbols = new String[dataset.genesNumber];
}
else if (line.startsWith("^SUBSET")){
subsetinfo[i][0] = line.substring(10);
i++;
}
else if (line.startsWith("!subset_sample_description")){
subsetinfo[i][1] = line.substring(22);
}
else if (line.startsWith("!subset_sample_id")){
subsetsample[i-1] = line.substring(20).split(",");
}
else if (line.startsWith("!dataset_table_begin")){
datamatrix = new float[dataset.genesNumber][dataset.samplesNumber];
}
else if (line.startsWith("ID_REF")){
String[] array1 = line.split("\t");
dataset.sampleIds = (String[]) Arrays.copyOfRange(array1, 2, array1.length);
bol = true;
}
else if (bol && !line.startsWith("!dataset_table_end")){
String[] array2 = line.split("\t");
geneids[j] = array2[0];
genesymbols[j] = array2[1];
String[] temp = (String[]) Arrays.copyOfRange(array2, 2, array2.length);
datamatrix[j] = StringtoFloat(temp);
j++;
}
}
buf.close();
dataset.geneIds = geneids;
dataset.geneSymbols = genesymbols;
dataset.dataMatrix = datamatrix;
String[] lables = new String[dataset.samplesNumber];
int k = 0;
for (String sample : dataset.sampleIds) {
for (int m = 0; m < subsetsample.length; m++) {
if (Arrays.binarySearch(subsetsample[m], sample) != -1) {
lables[k] = subsetsample[m][1];
k += 1;
} else {
continue;
}
}
}
dataset.labels = lables;
return dataset;
}
/**
* writeDatasetToTabularFile
* writes the dataset to a tabular text file
*
* #param geneExpressionDataset
* #param outputFilename
* #throws IOException
*/
public static void writeDatasetToTabularFile(GeneExpressionDataset geneExpressionDataset, String outputFilename) throws IOException {
File NewFile = new File(outputFilename);
BufferedWriter buf = new BufferedWriter(new FileWriter(NewFile));
String Lables = "\t" + "\t" + "\t" + "\t" + Arrays.toString(geneExpressionDataset.labels);
String Samples = "\t" + "\t" + "\t" + "\t" + Arrays.toString(geneExpressionDataset.sampleIds);
buf.write(Lables + "\r\n" + Samples + "\r\n");
for (int i = 0; i < geneExpressionDataset.genesNumber; i++){
buf.write(geneExpressionDataset.geneIds[i] + "\t"+ geneExpressionDataset.geneSymbols[i] + "\t" +
changeformat(geneExpressionDataset.dataMatrix[i]) + "\r\n");
}
buf.close();
}
/**
*
* writeTopDifferentiallyExpressedGenesToFile
*
* #param outputFilename
* #param geneExpressionDataset
* #param alpha
* #param label1
* #param label2
* #return numOfDiffGenes The number of differentially expressed genes detected, having p-value lower than alpha
* #throws IOException
*/
public static int writeTopDifferentiallyExpressedGenesToFile(String outputFilename,
GeneExpressionDataset geneExpressionDataset, double alpha,
String label1, String label2) throws IOException {
double pValues[] = new double[geneExpressionDataset.genesNumber];
int counter = 0;
for (int i = 0; i < pValues.length; i++){
double pval = calcTtest(geneExpressionDataset, i, label1, label2);
if (pval < alpha){
pValues[i] = pval;
counter++;
}
else{
continue;
}
}
File tofile = new File(outputFilename);
BufferedWriter buf = new BufferedWriter(new FileWriter(tofile));
int j = 0;
while (min(pValues) != -1){
String PVal = String.format("%.6f", pValues[min(pValues)]);
String gene_id = geneExpressionDataset.geneIds[min(pValues)];
String gene_symbol = geneExpressionDataset.geneSymbols[min(pValues)];
String line = String.valueOf(j) + "\t" + PVal + "\t" + gene_id + "\t" + gene_symbol;
buf.write(line + "\r\n");
pValues[min(pValues)] = 3.0;
j++;
}
buf.close();
return counter;
}
/**
*
* getDataEntriesForLabel
*
* Returns the entries in the 'data' array for which the corresponding entries in the 'labels' array equals 'label'
*
* #param data
* #param labels
* #param label
* #return
*/
public static double[] getDataEntriesForLabel(float[] data, String[] labels, String label) {
double[] array = new double[data.length];
int counter = 0;
for (int i = 0; i < data.length; i++){
if (labels[i].equals(label)){
array[counter] = data[i];
counter++;
}
else{
continue;
}
}return CutToCounter(array, counter);
}
/**
* calcTtest - returns a pValue for the t-Test
*
* Returns the p-value, associated with a two-sample, two-tailed t-test comparing the means of the input arrays
*
* //http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/stat/inference/TTest.html#tTest(double[], double[])
*
* #param geneExpressionDataset
* #param geneIndex
* #param label1
* #param label2
* #return
*/
private static double calcTtest(GeneExpressionDataset geneExpressionDataset, int geneIndex, String label1, String label2) {
double[] sample1 = getDataEntriesForLabel(geneExpressionDataset.dataMatrix[geneIndex], geneExpressionDataset.labels, label1);
double[] sample2 = getDataEntriesForLabel(geneExpressionDataset.dataMatrix[geneIndex], geneExpressionDataset.labels, label2);
return TestUtils.tTest(sample1, sample2);
}
/**
*
* GeneExpressionDataset
* A class representing a gene expression dataset
*
* #author software1-2014
*
*/
public static class GeneExpressionDataset {
public int samplesNumber; //number of dataset samples
public int genesNumber; // number of dataset gene probes
public String[] sampleIds; //sample ids
public String[] geneIds; //gene probe ids
public String[] geneSymbols; //gene symbols
public float[][] dataMatrix; //expression data matrix
public String[] labels; //sample labels
}
}
now, it won't compile and the error message is this:
"GeneExpressionAnalyzer.java:2: error: package org.apache.commons.math3.stat.inference does not exist
import org.apach.commons.math3.stat.interference.TestUtils;
GeneExpressionAnalyzer.java:277: error: cannot find symbol
return TestUtils.tTest;
symbol: variable TestUtils
location: class GeneExpressionAnalyzer
2 errors"
I don't get what went wrong, obviously i've added the .jar file that contains the path to TestUtils.
(here it is: http://apache.spd.co.il//commons/math/binaries/commons-math3-3.2-bin.zip)
any insights?
If you are working with eclipse,
Manually download the jar file from here
After that in eclipse open the package explorer -> right click on your project
Build Path -> Configure Build Path, A window will open.
Under the Libraries tab -> click Add External JARs. select the jar file you have downloaded Click Ok.
That's all. Now the problem might be gone
Does it work from the command line?
I've shortened your class to
import org.apache.commons.math3.stat.inference.TestUtils;
import java.io.*;
import java.util.Arrays;
public class Test {
public static void main(String args[]) throws IOException {
System.out.printf ("test...");
}
}
I copied the Test.java file and the commons-math3-3.2.jar to the same directory and here is my output from the command line:
C:\temp\test>dir
Répertoire de C:\temp\test
24/04/2014 14:41 <REP> .
24/04/2014 14:41 <REP> ..
24/04/2014 14:38 1 692 782 commons-math3-3.2.jar
24/04/2014 14:41 230 Test.java
2 fichier(s) 1 693 012 octets
2 Rép(s) 23 170 342 912 octets libres
C:\temp\test>javac Test.java
Test.java:1: package org.apache.commons.math3.stat.inference does not exist
import org.apache.commons.math3.stat.inference.TestUtils;
^
1 error
C:\temp\test>javac -cp commons-math3-3.2.jar Test.java
C:\temp\test>dir
Répertoire de C:\temp\test
24/04/2014 14:41 <REP> .
24/04/2014 14:41 <REP> ..
24/04/2014 14:38 1 692 782 commons-math3-3.2.jar
24/04/2014 14:41 500 Test.class
24/04/2014 14:41 230 Test.java