0

I am writing a Java hangman game where the user enters a letter guess and then guesses in which spaces the letter is in. For easy mode, the user can guess 4 spaces. The user must guess 4 spaces with a space between the numbers.

My question is, I have String guesses = keyboard.next(); but the issue I am having issue with is, how can I get the number that's at index 0, 2, 4, and 6? I am not allowed to use int space1 = keyboard.nextInt(); and so on. I have to use a string. So how can I extract the numbers at indices 0, 2, 4, and 6 from the string guesses?

P.S. I am not allowed to use arrays or the string builder class.

5
  • 1
    Have a look at String.substring docs.oracle.com/javase/7/docs/api/java/lang/… Commented Feb 26, 2015 at 22:13
  • Did you try anything? You wil have a much better chance of getting useful answers if you can provide some code that you have tried, together with a few comments on what isn't working. Commented Feb 26, 2015 at 22:13
  • Also, the code you gave would make more sense if you included some context. Commented Feb 26, 2015 at 22:15
  • HI Keppil, I would post some code but I am not allowed to post my code online per my class academic honesty policy. I have done quite a bit of research, but what I've found uses stuff I have not yet learned. Also, please don't post if it's not helpful. Thank you. Commented Feb 26, 2015 at 22:16
  • 2
    You're not allowed to share the code, but you can ask strangers to do the work for you? Commented Feb 26, 2015 at 22:17

1 Answer 1

2

Assuming for example

String s = "xx3x";

int j = s.charAt(2) - '0';

This gives j == 3.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.