Random Poet Exquisite Corpse help
Question:
I'm trying to create a program that reads in words from a file and places the
words into seperate arrays based on category(noun,adjective,verb..etc)
The file only reads in the first word of the file. I think I need a loop in readInput(); but I'm not sure how. If anyone familar with the java scanner class could help that would be great. I'm also unsure about the definition of addW() in the WordArray class.
Files:
//Verse.java needs to read in a series of lines from a text file called part-of-speech.txt
// part-of-speech.txt is a file that has a word seperated by a letter representing the words category. Each word and category is seperated by a tab
/* lay out of part-of-speech is as follows:
aaerially v
aaerialness N
aalii N
aardvark N
aardwolf N
aardwolves p
aarogramme h
aaron's rod |h
aarp |N
aas |N
ab N
ab aeterno h
ab extra h
*/
import java.util.*;
public class Verse
{
private WordArray noun;
private WordArray adj;
private WordArray adverb;
private WordArray verb;
private WordArray determiner;
public static final int MAXSIZE = 1000;
public Verse()
{
// verb = new WordArray(MAXSIZE);
wordArrayInit();
readInput();
}
public void readInput()
{
//need loop
Scanner kbd = new Scanner(System.in);
String line;
line= kbd.nextLine();
System.out.println(line);
StringTokenizer token = new StringTokenizer(line,"\t");
String spell = token.nextToken(); // represents word spelling
String cat = token.nextToken(); // represents the category (ie. noun,verb, adj);
for(int j =0; j < cat.length(); j ++)
{
char c = cat.charAt(j);
updateCategory(c,spell);
}
line = kbd.nextLine();
System.out.println(line);
}
public void wordArrayInit()
{
verb = new WordArray(MAXSIZE);
adverb = new WordArray(MAXSIZE);
noun = new WordArray(MAXSIZE);
adj = new WordArray(MAXSIZE);
}
public void updateCategory(char c, String spell)
{
Word wrd = new Word(spell,c);
int i =0;
switch(c) // compares character representing a category
{
case 'V':
verb.addW(wrd);
break;
case 'A':
adj.addW(wrd);
System.out.println(adj.getWord(0));
break;
case 'N':
noun.addW(wrd); // Adds a word to the noun list
System.out.println(noun.getWord(0));
break;
case 'v':
adverb.addW(wrd);
System.out.println(adverb.getWord(1));
break;
default:
System.out.println("Error1");
break;
}
// }
}
/* public void stringLoop()
{
}*/
/* public void wordArrayInit()
{
noun = new WordArray(MAXSIZE);
adj = new WordArray(MAXSIZE);
// verb = new WordArray(MAXSIZE);
adverb = new WordArray(MAXSIZE);
}*/
/* public String random()
{
}*/
/** Adds words to a specified WordArray based on there category (ie. noun, verb, etc.)
*/
public static void main(String[] args)
{
Verse vs = new Verse();
}
}
import java.util.*;
public class WordArray{
private int num; // used array positions
private Word [] wordH; // An array that holds words
private Random randgenerator;
public WordArray(int size){
wordH = new Word[size]; // size is the maximum positions within the WordArray
num = 0;
randgenerator = new Random();
}
/* public Word randomWord()
{
randgenerator.nextInt(num);
}*/
// }
public boolean addW(Word wrd){
if(num >= wordH.length)
{
System.out.println("Error num is greater than length");
return false;
}
else
{
wordH[num] = wrd;
System.out.println(wrd);
num ++;
return true;
}
}
public Word getWord(int n)
{
if(n <= num)
{
//System.out.println(wordH[num-1]);
return wordH[num-1];
}
else
{
System.out.println("Unable to get word");
return null;
}
}
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement