how to modify program

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1814 Views - Last Post: 26 May 2010 - 05:56 AM Rate Topic: -----

#1 arsh231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 10

how to modify program

Posted 19 May 2010 - 05:41 AM

hello guys i am fairly new to programming. I am trying to learn how to modfiy my previous program and would like to know how to go about doing this.




// The "GuessTheWord" class.
import hsa.*;
public class GuessTheWord
{
    public static void main (String[] args)
    {
        char[] letter={'h','i','p','p','o','p','o','t','a','m','u','s'};
        int wordSize, count, x;
        char guess;
        boolean[] flag;
        
        count = 0;
        wordSize = letter.length;
        flag = new boolean[wordSize];
        
        for(x= 0; x< wordSize; x++)
        {
            flag[x] = false;
        }
        do
        {
        Stdout.println("Enter a letter");
        guess = Stdin.readChar();
        
        for(x=0; x<wordSize; x++)
        {
            if(flag[x] == true)
            {
            Stdout.print(letter[x]+" ");
            }
             else if(guess == letter[x])
                    {
                    Stdout.print(letter[x] + " ");
                    flag[x]= true;
                    count = count + 1;
                    }  // endif
                  else
                    {
                    Stdout.print("_ ");
                    }
                
        }  //end loop for finding matching letters
        Stdout.println();
        } // end of do loop
        while (count<wordSize);
        
    } // main method
} // GuessTheWord class



that is the program i had made earlier and i must learn to modfiy in the follwing way-. Input from Data files
Modify your “Guess the Word” program to read in the mystery word from a data file.
The data file should contain 10 mystery words. The program randomly picks one of the words to solve.
BONUS: See if you can make the program ask the user if she/he wants to play another round.

i have no idea how to do this and must learn brfore we recive the final project. so could someone please help. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: how to modify program

#2 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 294
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: how to modify program

Posted 19 May 2010 - 06:40 AM

You will need to utilize Scanners and BufferedReaders. Check out here to start.
Was This Post Helpful? 0
  • +
  • -

#3 arsh231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 10

Re: how to modify program

Posted 20 May 2010 - 05:20 AM

we can not use those methods because we are using ready to program java ide. plz i need to learn how to do this
Was This Post Helpful? 0
  • +
  • -

#4 Lucas544   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 31
  • Joined: 09-April 10

Re: how to modify program

Posted 20 May 2010 - 05:25 AM

you need to save all of the words in the data file to an array. As for the bonus, you just need to make an if statement asking if they want to play again looping it over again.
Was This Post Helpful? 0
  • +
  • -

#5 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 294
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: how to modify program

Posted 20 May 2010 - 06:35 AM

To get the words from the text file though, you must use one of three File I/O methods. The best one to use in your case is a Scanner.
Was This Post Helpful? 0
  • +
  • -

#6 sh1n3   User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: how to modify program

Posted 20 May 2010 - 06:46 AM

Well start by adding :
TextInputFile iFile = new TextInputFile("MysteryWord.txt");
String[] tempWord=new String[10];
int count=0;
do {
        tempWord[count]=iFile.readString();
}while (count<10);
iFile.close();
for (int b=0;b<10;b++) {
        char[] letters=tempWord[b].toCharArray();
        //PROGRAM GOES HERE
Stdout.print("Play another round - press 1 : ");
int play=Stdin.readInt();
if (play!=1) {
        System.exit(0);
}
}


And I wonder if there is a readChar() method in hsa package. Anyway this will give you a different word for each of the 10 times. I think getting a random word will involve creating an algorithm like :
do {
x=(x+3)-1*2;
}while(x<10);

And then use -
tempWord[x]


It is very difficult doing this without the other packages like io and util. There is actually a class in util called Random which can create random numbers.
Anyway hope this helps.

This post has been edited by sh1n3: 20 May 2010 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: how to modify program

Posted 20 May 2010 - 08:57 PM

View PostLuckless, on 20 May 2010 - 09:35 AM, said:

To get the words from the text file though, you must use one of three File I/O methods. The best one to use in your case is a Scanner.


I agree; however, the OP's school provides a library with easy to use I/O tools, as evidenced by Stdin and Stdout.
Was This Post Helpful? 0
  • +
  • -

#8 singularity   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Re: how to modify program

Posted 21 May 2010 - 05:21 AM

If you don't want to use scanner, the other and simple option is to use TextIO.

TextIO is a custom class that is used for input and output in java, For using this class you should have TextIO.java and TextIO.class in your project path.

You can get TextIO from here

TextIO

Hope this helped.

This post has been edited by singularity: 21 May 2010 - 05:26 AM

Was This Post Helpful? 0
  • +
  • -

#9 arsh231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 10

Re: how to modify program

Posted 21 May 2010 - 06:09 AM

// The "Hangman2" class.
import hsa.*;
public class Hangman2
{
    public static void main (String[] args)
    {
        TextInputFile in = new TextInputFile ("Hangman.txt");
        while (!in.eof ())
        {



        
        String[] sWord = new String [10];
        char[] cLetter;
        int iCount = 0, iWordLength = cLetter.length;
        char cGuess;
        boolean[] flag;
        flag = new boolean [iWordLength];

        for (int i = 0 ; i < 10 ; i++)
        {
            cLetter = sWord [i].toCharArray ();
            cLetter [i] = in.readChar ();
        }




        for (int i = 0 ; i < 10 ; i++)
        {
            Stdout.print (sWord [i]);
        }

        Stdout.print ("The category is a computer part");
        Stdout.println ();
        for (int i = 0 ; i < iWordLength ; i++)
        {
            flag [i] = false;
        }
        do
        {
            Stdout.println ("Please guess a letter: ");
            cGuess = Stdin.readChar ();
            for (int i = 0 ; i < iWordLength ; i++)
            {
                if (flag [i] == true)
                {
                    Stdout.print (cLetter [i]);
                }
                else if (cGuess == cLetter [i])
                {
                    Stdout.print (cLetter [i]);
                    flag [i] = true;
                    iCount = iCount + 1;
                }
                else
                {
                    Stdout.print ("_ ");
                }
            }
            Stdout.println ();
        }
        while (iCount < iWordLength);
        Stdout.println ();
        Stdout.print ("Congradulations, You Guessed the Word Correctly");
        }
        in.close ();


    } // main method
} // Hangman2 class



I keep getting error cLetter at the beginning of the program where it says iWordLength = cLetter.length; it tells me the variable "cLetter" may be accessed here before having been definitely assigned a value

Edited by macosxnerd101: Welcome to DIC! :) Please remember to post your code using code tags, like so: :code:

This post has been edited by macosxnerd101: 21 May 2010 - 06:32 AM

Was This Post Helpful? 0
  • +
  • -

#10 Lucas544   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 31
  • Joined: 09-April 10

Re: how to modify program

Posted 21 May 2010 - 06:25 AM

That just means that you need to assign a value to cLetter before it can be used.
Was This Post Helpful? 0
  • +
  • -

#11 arsh231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 10

Re: how to modify program

Posted 25 May 2010 - 05:35 AM

how would i go about doing that my friend. sorry i am not that great at the programming. plz and thank you long live india
Was This Post Helpful? 0
  • +
  • -

#12 poop213   User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 1
  • Joined: 25-May 10

Re: how to modify program

Posted 25 May 2010 - 05:48 AM

View Postarsh231, on 25 May 2010 - 04:35 AM, said:

how would i go about doing that my friend. sorry i am not that great at the programming. plz and thank you long live india

<Removed>

Edited by macosxnerd101: That is not appropriate. We are here to encourage and help members, not try to get them to stop coding. Don't post anything like that again.

This post has been edited by macosxnerd101: 25 May 2010 - 05:50 AM

Was This Post Helpful? -5
  • +
  • -

#13 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: how to modify program

Posted 25 May 2010 - 05:49 AM

Here are some examples of variable declarations vs. initializations. Happy coding! :)
int x; //declare a variable
x = 2; //initialize a variable

int y = 2; //declare and initialize in the same line
int[] z = new int[3]; //declare and initialize an array to length 3


Was This Post Helpful? 0
  • +
  • -

#14 xor-logic   User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 767
  • Joined: 04-February 10

Re: how to modify program

Posted 25 May 2010 - 05:50 AM

View Postpoop213, on 25 May 2010 - 11:48 AM, said:

<Removed>

Don't listen to this idiot.

Edited by macosxnerd101: I appreciate the sentiment, but I don't think we need that quote on this forum. :)

This post has been edited by macosxnerd101: 25 May 2010 - 05:55 AM

Was This Post Helpful? 0
  • +
  • -

#15 Tilt   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 26-May 10

Re: how to modify program

Posted 26 May 2010 - 05:49 AM

Hi there, this is my first post so i apologize if somethings wrong, but since i have the java text book, with all its glorious 1000 pages, this is what i came up with:

 
// The "GuessTheWord" class.
import hsa.*;
public class GuessTheWord
{
    public static void main (String[] args)
    {
        TextInputFile bank = new  TextInputFile ("Word.txt");        
        
        String[] word = new String [3];
        char[] letter;
        int wordSize, count, x, i = 0, play = 1;
        char guess;
        boolean[] flag;

        while (!bank.eof ())
        {
            word [i] = bank.readLine ();
            i++;
        }

        for (int p = 0 ; play == 1 ; p++)
        { //loop to play again
        
            int num = ((int) (Math.random () * 3 + 1)) - 1;

            letter = new char [word [num].length ()];

            for (int z = 0 ; z < word [num].length () ; z++)
            {
                letter [z] = word [num].charAt (z);
            }

            count = 0;
            wordSize = letter.length;
            flag = new boolean [wordSize];

            for (x = 0 ; x < wordSize ; x++)
            {
                flag [x] = false;
            }
            for (x = 0 ; x < wordSize ; x++)
            {
                Stdout.print ("_ ");
            }
            do
            {
                Stdout.println ("\nEnter a letter");
                guess = Stdin.readChar ();

                for (x = 0 ; x < wordSize ; x++)
                {
                    if (flag [x] == true)
                    {
                        Stdout.print (letter [x] + " ");
                    }
                    else if (guess == letter [x])
                    {
                        Stdout.print (letter [x] + " ");
                        flag [x] = true;
                        count = count + 1;
                    } // endif
                    else
                    {
                        Stdout.print ("_ ");
                    }

                } //end loop for finding matching letters
                Stdout.println ();
            } // end of do loop
            while (count < wordSize);

            Stdout.print ("Enter 1 to play again or 0 to stop: ");
            play = Stdin.readInt ();
        } //for
    } // main method
} // GuessTheWord class



In my program i have the word variable set to 3 because in my datafile i had 3 different words, also the random number genterator uses a 3 in it because i wanted only the numbers 0-2 inclusive so i don't pick a number that is out of bounds of the array. This is what my datafile looked like:

chicken
hippopotamus
giraffe



I put the for loop at the beginning for the bonus to play over and over again, the random number generator had to be put in after it so i choses a different (hopefully) word everytime. Hope this helps.

This post has been edited by Tilt: 26 May 2010 - 05:51 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2