Skip to main content

Need help with a little program..

4 replies [Last post]
Rider~NBK~
Offline
Joined: 2006-02-17

Hi, I'm new to these forums and to Java all together but.. Here's my dilemma, I'm learning Java at school (1st year taking the course) and I have been asked to finish up a little program as an assignment.

Anyways.. here's the Question:

Question 1

Implement a program that will make predictions regarding the fate of the current user. Most of this program has been written and is available as A4Q1.java in the documents area. Do NOT change any of the code that has been provided to you (other than the informational comments at the beginning of the file to indicate who you are)!

Your job is to implement the method String extractWord( String wordList, int wordIndex ) used by main() to determine the predictions. The method takes wordList and finds the word at position wordIndex. The first word is considered word 1 and each word is separated by 1 or more spaces. Note: you can only use the charAt() and length() String methods.

Look at the strings that are passed to extractWord to determine all of the special cases that you need to handle.

--- And here's the code I have so far (the only code I wrote is under the extractWord() method.. It runs fine it just doesn't give the correct prediction every time if you understand me.. anyway not quite sure how the code will display in here but here goes nothin, any helps would be great!...

import javax.swing.*;

/**
class A4Q1. Generates predictions from a set of pre-defined words.

public class A4Q1
{
/**
Our application. It will prompt the user for their name and make up predictions
until done.

@param args -- an array of command line options
@return void
*/
public static void main( String args[] )
{
// the text used to build our predictions
final String firstWord = "sees wants hears desires fears loathes needs";
final String secondWord = "death love wealth poverty sickness danger travel sadness ";
final String thirdWord = "coming";
final String fourthWord = "tomorrow never soon next immediately";
final String fifthWord = ". ! ? ...";
final String sixthWord = "";
final String company = "Madame Trewlaney's Crystal Gazing Corp";

String name;
String prediction;
int response;

name = JOptionPane.showInputDialog( null, "What's you're name? (Enter Trelawney to quit)", company, JOptionPane.QUESTION_MESSAGE );
while ( !name.equals( "Trelawney" ) )
{
do
{
// build the prediction by randomly selecting words from our lists
prediction = name;
prediction += " " + extractWord( firstWord, randomIndex( 1, 7 ) );
prediction += " " + extractWord( secondWord, randomIndex( 1, 8 ) );
prediction += " " + extractWord( thirdWord, randomIndex( 1, 1 ) );
prediction += " " + extractWord( fourthWord, randomIndex( 1, 5 ) );
prediction += extractWord( fifthWord, randomIndex( 1, 4 ) );
prediction += " " + extractWord( sixthWord, randomIndex( 1, 2 ) );

JOptionPane.showMessageDialog( null, prediction, company, JOptionPane.INFORMATION_MESSAGE );

response = JOptionPane.showConfirmDialog( null, "Would you like another prediction for " + name + "?", company, JOptionPane.YES_NO_OPTION );
} while ( response == JOptionPane.YES_OPTION );

name = JOptionPane.showInputDialog( null, "What's you're name? (Enter Trelawney to quit)", company, JOptionPane.QUESTION_MESSAGE );
}

JOptionPane.showMessageDialog( null, "end of predictions", company, JOptionPane.INFORMATION_MESSAGE );
System.exit(0);
}

/**
Generates random numbers given the passed range.

@param low -- the minimum value to generate for our random number
@param high -- the maximum value to generate for our random number
@return int -- a random number between low and high
*/
static int randomIndex( int low, int high )
{
return (int)(Math.random() * (high - low + 1)) + low;
}

/**
Pulls a word from the passed list of words.

@param wordList -- the words to choose from, separated by spaces
@param wordIndex -- the word to extract, the first word is considered word 1
@return String -- the extracted word
*/
static String extractWord( String wordList, int wordIndex )
{
String theWord = "";

int wordCounter = 0;
int counter = 0;
int charCounter = 0;
boolean haveNotFoundWord = true;

while (counter <= wordList.length() - 1 && haveNotFoundWord)
{
if (wordList.charAt(counter) == ' ')
{
wordCounter++;
if (wordCounter + 1 == wordIndex)
{
while(wordList.charAt(charCounter) != ' ')
{
theWord = theWord + wordList.charAt(charCounter);
charCounter++;
}
haveNotFoundWord = false;
}
}
counter++;
}

return theWord;
}
}

Message was edited by: Rider~NBK~

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jafaucett
Offline
Joined: 2006-02-17

Learning to program means attention to details. You aren't comparing to wordIndex correctly, you aren't handling the end of the string correctly (there's no space to delimit the last word), and you aren't incrementing charCounter correctly.

A suggestion: On a sheet of paper, for each step in each loop, keep track of the two counters. You'll find the problems right away.

Rider~NBK~
Offline
Joined: 2006-02-17

I got it :)

Had to re-write the whole thing entirely lol.

brinkley
Offline
Joined: 2003-06-06

Topic doesn't belong in Forum feedback ... moving the topic here.

Binky

Rider~NBK~
Offline
Joined: 2006-02-17

sorry lol like i said first time in here.