-3

I'm trying to create a hangman program, where the words are already set at the start, and the user has to try and guess them.

But I'm having serious trouble trying to figure out how to even split the words apart so that I can get the program to pick a random one of them.

I have tried declaring the following words as a string, how do I pick one instead of them all, or is this even correct?

Also I have only started java so I can only use if statements etc, seen a few people using arrays, but I can't use them as this is for a project and I'm not supposed to know how.

String words = "cat:dog:hat:sat:mat:rat";
Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
Jack
  • 37
  • 1
  • 6
  • You can 'split' the words, by using the 'split' method, with : as separator. – Stultuske Nov 25 '15 at 12:04
  • `seen a few people using arrays but I can't use them as this is for a project ` Are you serious ? – Suresh Atta Nov 25 '15 at 12:04
  • Technically, it isn't, but it's easier to make a mistake in writing: {"hello","how","are","you"}; then in "hello:how:are:you"; – Stultuske Nov 25 '15 at 12:07
  • Yeah haha i've no idea why but we're not supposed to have learnt them yet so if we use them she takes marks off lol.. – Jack Nov 25 '15 at 12:07
  • @Jack Ok hence you cannot use split as well since it returns an array. You need to do that manually :) – Suresh Atta Nov 25 '15 at 12:08
  • so just use what you've said above? :) – Jack Nov 25 '15 at 12:09
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: I see no single reason why he shouldn't have arrays in his code. the reason "I'm not supposed to know how" is rubbish. Nobody impresses a teacher by not trying to find a good way to work on your own. – Stultuske Nov 25 '15 at 12:12
  • @Stultuske May be the aim of the assignment is to learn about characters ,String's, for loops etc :) He can't just copy a single line of code from internet and submit. – Suresh Atta Nov 25 '15 at 12:15
  • split is part of String, teaching about String and leaving that out is nonsense (or just a teacher still working on an outdated version thinking he's da bomb) – Stultuske Nov 25 '15 at 12:16

3 Answers3

1

As you're not allowed to use arrays you could first create a random number and then depending on the outcome select one of the strings.

Random random = new Random();
int outcome = random.nextInt(6); // 6 being the number of different words
String word = "";
if (outcome == 0)
    word = "cat";
else if (outcome == 1)
    word = "dog";
...

After this word will hold the word java selected at random for you.


But as already stated by others this is only applicable if you can't use arrays. In general you would either use the approach with split and select one of the elements or you would have a separate file with words as lines and then select one of the lines in the document at random.

The second approach has the advantage that you can add new words without changing your program.

Additionally reading a specific line of a document to get its contents is even possible without using arrays. You could use the answer here and just call readLine an random number of times and then taking the string it returned as word.

Community
  • 1
  • 1
gostefan
  • 11
  • 3
0

There is a split function which takes a pattern as an input and return an array of Strings. See the documentation.

Here is an example

String words = "cat:dog:hat:sat:mat:rat";
String[] array = words.split(":");
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
0

The split function is suitable for that :

String[] str_array = "name:score:abc".split(":");

You will use the variables and assign them the result strings indexes.to get a particular word.

    String stringa = str_array[0]; 
    String stringb = str_array[1];
    String stringc = str_array[2];
Haseeb Anser
  • 494
  • 1
  • 5
  • 19
  • is there another way that doesn't involve arrays? As i'm not allowed to use them. Thanks – Jack Nov 25 '15 at 12:11
  • @Jack yes, you can use substring, but that is messy, and creates terrible code, not to mention it 'll make your code a lot less efficiënt and more difficul to write. – Stultuske Nov 25 '15 at 12:13
  • I don't think so there would be any other good approach to do this without using arrays because you can't predict the number of words occurring in your string.So using an array would be a good approach to get dynamic number of words. – Haseeb Anser Nov 25 '15 at 12:18