I'm trying to create a program that identifies the subject and verb of a sentence. I use almost the exact same method to determine words that are nouns and words that are verbs, but when I call this method back-to-back it wont work the second time around. For example if I do this:

try {
	    	SA.VerbIdentifier(SA.TokenToArray(INPUT_words));
		} catch (Exception e) {
			System.out.println("Error reading Verb Text file");
			e.printStackTrace();
		}
	    try {
	    	SA.SubjectIdentifier(SA.TokenToArray(INPUT_words));
		} catch (Exception e) {
			System.out.println("Error reading Subject Text file");
			e.printStackTrace();
		}

Then the program wont find the subject. If I call the subject method first and the verb method second it wont find the verb. Can someone tell me why?

void SubjectIdentifier(String[] INPUT) throws Exception{
		int position = -1;
		String[] SubjectFile = {"src/Pronouns.txt","src/Nouns.txt"};
		String[] SubjectArray = null;
		for(int a = 0; SubjectFile.length > a; a++){
			SubjectArray = FileToArray(SubjectFile[a]);
			for(int i = 0; INPUT.length > i; i++){
				for(int j = 0; SubjectArray.length > j; j++){
					if(SubjectArray[j].trim().equalsIgnoreCase((INPUT[i]))){					
						position = i;
						break;	
					}
				}
			}
		}

Here's the FiletoArray() Method

String[] FileToArray(String file) throws Exception {
		String[] words = null;
		words = new String[NumberOfWords(file)];
		
		BufferedReader FILE = new BufferedReader(new FileReader(file));
		try {
			for(int i = 0; words.length > i; i++){
				words[i] = FILE.readLine();
			}
			
		} catch (Exception done) {
			done.printStackTrace();
		}
		FILE.close();
		return words;
	}

Recommended Answers

All 3 Replies

This is what I suggest to you:

1) You will take your FileToArray() function and you will test it with all the possible limit cases (even if they are stupid). If there's no problem, then you will remove it from the post.

2) Do you know JUnit? I strongly suggest you to use it. It is a software testing environment. You will write your tests and use the limit cases for testing. Then, you will see your bug.

This is a common practice in software development to test EVERYTHING before doing something else. And yes, you can do it everytime.

I'm not sure what you mean by limit cases, but I will download JUnit ASAP. The thing that confuses me most about this is that the method works, but if I run it twice the second output from the method is NULL. The method I'm having problems with is the SubjectIdentifier() Method. I included the FileToArray() Method because SubjectIdentifier() depends on it.

You don't need JUnit at this point, you just need to place some debugging println statements throughout your methods so you can see what is going on inside them (if you don't have or know how to use an IDE debugger).

Trying to learn how to use JUnit is not going to speed up solving this problem at all - it's just going to add a lot of unnecessary confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.