The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Arrays in my French program

Posted by Matt Gerrans on January 21, 2002 at 11:00 PM

> In a French learning program I'm writing I've got a bit of code like this:

> String[][] words = {{"Hello", "Bonjour"},
> {"Goodbye", "Au revoir"},
> {"Yes", "Oui"},
> {"No", "Non"}};
> int numberOfWords = 4;

> Should I want to add more words, I also have to change the value of numberOfWords (there is a part later in the program where it needs to know how many words there are). Is there any way of automatically finding out how many there are, without me having to manually change it?

In addition to using the length property of the array mentioned by Kishori, you might want to design your system so that it reads the text from a separate file, so you don't have to edit and recompile your java code to add new words. You could use a Properties object for this.

The ResourceBundle object is the mechanism for localization in Java, so maybe you could use that, but I'm not sure how you'd read strings for two (or more) different languages. Usually it is used to read the strings for the language/locale that is currently set. Maybe you could dynamically switch locales and call getString() twice in a row with the same ID, but different locales. There may be a more elegant solution than this (You'll have to do the legwork), especially since changing the local on some operating systems is a big mess.

  • Text files Chloe January 22, 2002 at 6:01 AM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us