Hangman/Cypher1

4 posts / 0 new
Last post
cromiem2
Hangman/Cypher1

Hola

From the Hangman programme that John used as an example I changed around my cypher programme, but I noticed something I'm not too sure about

When running a while loop to remove a randomly picked letter and reducing the original message by that letter using substrings, why does this line of code work?

message = message.substring(0, index) + message.substring(index + 1);

The second substring doesn't have an end value, how does that work?

For my version of the programme I had included the value of the message length to give an end to the substring

message = message.substring(0, index) + message.substring(index+1, len);

len is an int value based on the messsage length

Cheers

Mark

dardiso2
Hangman/Cypher1

 

Hi Mark

If you dont give the substring method an end index, it uses the last index as a default so .....

message.substring(index + 1) will extract from (index+1) to the end of the string message.

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#substring(int)

It's not in notes or the book but the best place for all the info and options on classes and methods is the Oracle Java site.......the only problem is trawling through all the listings.

Good luck

Orla

jpendlebury
String substring

Hi Mark,

 

If you exclude the second parameter in the substring method the String class will assume you want everything up to the end of the string. Handy!

 

See: http://download.oracle.com/javase/6/docs/api/java/lang/String.html

 

You will notice that there are two different substring methods, one taking two parameters and one taking one. This document explains the difference between the operation of the two.

Regards,

John

cromiem2
Re: String substring

Thanks John/Orla

 

Had an inkling it was that alright but wanted to be sure

 

I find the oracle method descriptions hard to follow sometimes

 

Cheers

 

Mark

Login to post comments