9 Replies - 429 Views - Last Post: 03 April 2013 - 08:18 AM Rate Topic: -----

#1 bobrown101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 13

help with hangman (using arrays and see if the letter is in the word)

Posted 02 April 2013 - 12:06 PM

I'm making a hangman game, and i need to be able to know if the guess is in the word multiple times here is my code
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;;

public class main{
  public static void main(String[] args){
    graphics graphics = new graphics();
    main main = new main();
    Scanner input = new Scanner(System.in);

    String word[] = {"h", "i"};

    int guesses_right = 0;
    int guesses_wrong = 0;

    int length = Array.getLength(word);

    System.out.println("Hello, this is a hangman game. Good luck!");

    while(guesses_right != length){
      System.out.println("Please enter a letter");
      String x = input.nextLine();
      try{
        if(Arrays.asList(word).contains(x)){
                 **** this is where i need help***
        }
      }
      catch(Exception e){
        System.out.println("I'm sorry, but it appears you have entered something invalid.  Please try again");

      }

    }
    System.out.println("Congraulations. You have WON!!");

  }
}


here is my flow chart so you guys can see my approach

https://www.lucidcha...-79600a000cd9/0
thanks for the help in advance

Is This A Good Question/Topic? 0
  • +

Replies To: help with hangman (using arrays and see if the letter is in the word)

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,168
  • Joined: 12-June 08

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 02 April 2013 - 12:13 PM

      try{
        if(Arrays.asList(word).contains(x)){
                 **** this is where i need help***
        }
      }


Your word (that the player is guessing) is in a string, right? A string is just an array of characters, and that means you can iterate through them with a 'for loop'... testing each one against what character the user guessed!
Was This Post Helpful? 0
  • +
  • -

#3 bobrown101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 13

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 02 April 2013 - 12:24 PM

View Postmodi123_1, on 02 April 2013 - 12:13 PM, said:

      try{
        if(Arrays.asList(word).contains(x)){
                 **** this is where i need help***
        }
      }


Your word (that the player is guessing) is in a string, right? A string is just an array of characters, and that means you can iterate through them with a 'for loop'... testing each one against what character the user guessed!


I sort of understand what you are saying, but could you give a simple example of how you would do that?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,168
  • Joined: 12-June 08

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 02 April 2013 - 12:58 PM

It's a for loop.. pretty basic. You need three things:
1. initializing loop control variables
2. termination condition
3. increment action.

for(1;2;3)
{
// stuff
}





To count to five you can use something like:

for(int lcv = 0; lcv < 6; lcv++)
{
system.out.printlin(lcv);
}



See.. not too hard.

Arrays are just an indexed collection of similarly typed objects.

value: 0, 12, 15, 19, 6
index: [0],[1], [2], [3], [4]

That means if an array's values look like {0, 12, 15, 19, 6} I can pick any one of the valid indexes (no going past the length of the array) and use it at will.

Example:

		String[] foo = {"a", "b", "z", "y"}; // my array of characters
		if (foo[1] == "a") // I picked the second indexed item and compare it against 'a'.
			System.out.println("Match!");
		else
			System.out.println("No match!");



Now go knock your socks off thinking about how to use the length of the array to help in the for loop's condition to not exceed the length of the array, but make sure you iterate through all the options.

http://docs.oracle.c...dbolts/for.html
http://docs.oracle.c...lts/arrays.html
Was This Post Helpful? 0
  • +
  • -

#5 bobrown101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 13

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 02 April 2013 - 01:44 PM

View Postmodi123_1, on 02 April 2013 - 12:58 PM, said:

It's a for loop.. pretty basic. You need three things:
1. initializing loop control variables
2. termination condition
3. increment action.

for(1;2;3)
{
// stuff
}





To count to five you can use something like:

for(int lcv = 0; lcv < 6; lcv++)
{
system.out.printlin(lcv);
}



See.. not too hard.

Arrays are just an indexed collection of similarly typed objects.

value: 0, 12, 15, 19, 6
index: [0],[1], [2], [3], [4]

That means if an array's values look like {0, 12, 15, 19, 6} I can pick any one of the valid indexes (no going past the length of the array) and use it at will.

Example:

		String[] foo = {"a", "b", "z", "y"}; // my array of characters
		if (foo[1] == "a") // I picked the second indexed item and compare it against 'a'.
			System.out.println("Match!");
		else
			System.out.println("No match!");



Now go knock your socks off thinking about how to use the length of the array to help in the for loop's condition to not exceed the length of the array, but make sure you iterate through all the options.

http://docs.oracle.c...dbolts/for.html
http://docs.oracle.c...lts/arrays.html


Thanks. This actually helped out a lot. If you want, I'll post the finished product tomorrow.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 02 April 2013 - 01:51 PM

I wouldn't do it that way at all
I would put the word in a single String
and convert it to a char[] array for manipulation

   String word = "elephant";
   char[] digit = word.toCharArray();
   char[] guess = new char[digit.length];
   for(int i = 0; i < guess.length; ++i)  // user guess filled with blank
     guess[i] = ' ';
...

   // now to find if a letter is there
   int nbFound = 0;
   for(int i = 0; i < digit.length; ++i) {
     if(charFromUser == digit[i]) {
        ++nbFound;
        guess[i] = charGromUser;
        digit[i] = ' ';    // <--- so user cannot guess twice same letter
     }
   }


Was This Post Helpful? 0
  • +
  • -

#7 bobrown101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 13

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 03 April 2013 - 07:18 AM

View Postpbl, on 02 April 2013 - 08:51 PM, said:

I wouldn't do it that way at all
I would put the word in a single String
and convert it to a char[] array for manipulation

   String word = "elephant";
   char[] digit = word.toCharArray();
   char[] guess = new char[digit.length];
   for(int i = 0; i < guess.length; ++i)  // user guess filled with blank
     guess[i] = ' ';
...

   // now to find if a letter is there
   int nbFound = 0;
   for(int i = 0; i < digit.length; ++i) {
     if(charFromUser == digit[i]) {
        ++nbFound;
        guess[i] = charGromUser;
        digit[i] = ' ';    // <--- so user cannot guess twice same letter
     }
   }



Hey guys. I have done a lot of the program. The only parts left are making sure you cant enter the same letter twice, accounting for the letter being in the word multiple times, and showing the operator where the letter is in the word (for example: _ _ _ _ a_ _). I have no time limit on doing these things since I'm just doing this for fun. If you could help me figure the rest out I would really appreciate it.

PS. I uploaded two files in this post. I'm not sure, but i think I can just make another post with the other two classes.

View Postbobrown101, on 03 April 2013 - 02:13 PM, said:

View Postpbl, on 02 April 2013 - 08:51 PM, said:

I wouldn't do it that way at all
I would put the word in a single String
and convert it to a char[] array for manipulation

   String word = "elephant";
   char[] digit = word.toCharArray();
   char[] guess = new char[digit.length];
   for(int i = 0; i < guess.length; ++i)  // user guess filled with blank
     guess[i] = ' ';
...

   // now to find if a letter is there
   int nbFound = 0;
   for(int i = 0; i < digit.length; ++i) {
     if(charFromUser == digit[i]) {
        ++nbFound;
        guess[i] = charGromUser;
        digit[i] = ' ';    // <--- so user cannot guess twice same letter
     }
   }



Hey guys. I have done a lot of the program. The only parts left are making sure you cant enter the same letter twice, accounting for the letter being in the word multiple times, and showing the operator where the letter is in the word (for example: _ _ _ _ a_ _). I have no time limit on doing these things since I'm just doing this for fun. If you could help me figure the rest out I would really appreciate it.

PS. I uploaded two files in this post. I'm not sure, but i think I can just make another post with the other two classes.



Here are the other classes

View Postbobrown101, on 03 April 2013 - 02:14 PM, said:

View Postpbl, on 02 April 2013 - 08:51 PM, said:

I wouldn't do it that way at all
I would put the word in a single String
and convert it to a char[] array for manipulation

   String word = "elephant";
   char[] digit = word.toCharArray();
   char[] guess = new char[digit.length];
   for(int i = 0; i < guess.length; ++i)  // user guess filled with blank
     guess[i] = ' ';
...

   // now to find if a letter is there
   int nbFound = 0;
   for(int i = 0; i < digit.length; ++i) {
     if(charFromUser == digit[i]) {
        ++nbFound;
        guess[i] = charGromUser;
        digit[i] = ' ';    // <--- so user cannot guess twice same letter
     }
   }



Hey guys. I have done a lot of the program. The only parts left are making sure you cant enter the same letter twice, accounting for the letter being in the word multiple times, and showing the operator where the letter is in the word (for example: _ _ _ _ a_ _). I have no time limit on doing these things since I'm just doing this for fun. If you could help me figure the rest out I would really appreciate it.

PS. I uploaded two files in this post. I'm not sure, but i think I can just make another post with the other two classes.

View Postbobrown101, on 03 April 2013 - 02:13 PM, said:

View Postpbl, on 02 April 2013 - 08:51 PM, said:

I wouldn't do it that way at all
I would put the word in a single String
and convert it to a char[] array for manipulation

   String word = "elephant";
   char[] digit = word.toCharArray();
   char[] guess = new char[digit.length];
   for(int i = 0; i < guess.length; ++i)  // user guess filled with blank
     guess[i] = ' ';
...

   // now to find if a letter is there
   int nbFound = 0;
   for(int i = 0; i < digit.length; ++i) {
     if(charFromUser == digit[i]) {
        ++nbFound;
        guess[i] = charGromUser;
        digit[i] = ' ';    // <--- so user cannot guess twice same letter
     }
   }



Hey guys. I have done a lot of the program. The only parts left are making sure you cant enter the same letter twice, accounting for the letter being in the word multiple times, and showing the operator where the letter is in the word (for example: _ _ _ _ a_ _). I have no time limit on doing these things since I'm just doing this for fun. If you could help me figure the rest out I would really appreciate it.

PS. I uploaded two files in this post. I'm not sure, but i think I can just make another post with the other two classes.



Here are the other classes


_______________________________________________________________________

Apparently the files don't show up. Here is the dropbox link: https://www.dropbox....vx25/t4sfMDjgM8
Was This Post Helpful? 0
  • +
  • -

#8 bobrown101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-April 13

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 03 April 2013 - 08:09 AM

I'm making a simple hangman application. This is the class that will see where the letter is in the word
import java.lang.String;

public class char_array {
	main main = new main();
	String word; // this will be some word the person would be trying to guess like "brady"
	String x; // this is the letter the operator guesses , so it would be like "b"
	
	public char_array(String a, String B)/>/>{
		a = this.word;
		b = this.x;
	}

	public void some_function() {
		
		char charArray[]=word.toCharArray();
		for(int h=0;h<charArray.length;h++){
		    System.out.println("Data at ["+h+"]="+charArray[h]);//this is just to test
		}
	
	}

}




this is the error I get
 
Exception in thread "main" java.lang.NullPointerException
	at char_array.some_function(char_array.java:15)
	at main.main(main.java:47)



the error doesn't really tell me much. Can you guys help?
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7650
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 03 April 2013 - 08:12 AM

It tells you everything you need to know. 'word' is not initialized, so it's pointing to null. You're trying to call a method on a null object, which you can't do. You need to give 'word' a value before you can call methods on it.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,168
  • Joined: 12-June 08

Re: help with hangman (using arrays and see if the letter is in the word)

Posted 03 April 2013 - 08:18 AM

You already have a thread on this.. merging.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1