aspose file tools*
The moose likes Cattle Drive and the fly likes Character Strings Redefined As Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Character Strings Redefined As Arrays" Watch "Character Strings Redefined As Arrays" New topic
Author

Character Strings Redefined As Arrays

Maritza Keisman
Greenhorn

Joined: Mar 28, 2006
Posts: 7
Hi,

During my days as a mainframe programmer, I coud do the following:

aString (a15) /* this declares a string that is 15 characters long
(anArray(1:15) /* redefines aString into an array with 15 occurences)

aString = "MooseHead " /* assigns a value

anArray(1) = "M"
anArray(2) = "o"
anArray(3) = "o"
anArray(4) = "s"

Is there any way to redefine each character in a string as an occurence in an array?

Thanks,
Maritza
Maritza Keisman
Greenhorn

Joined: Mar 28, 2006
Posts: 7
Hi,

If i have code that says :

public static void main( String args[] )

My user has typed java Say 900

I know the args.length = 3

So, I know that my number string will be in the hundreds. If if could do a for next loop with

for( int x = 0 ; x < args.length ; x++ )

Split the string 900 into occurences :

x [0] = hundreds
look up hundreds method

x [1] = tens
look up tens method

x [2] = ones
loop up ones method

Based on the occurence of the number, I would like to call the appropriate method. What would be even better, is if I could write a for next loop that looked like this

for( int x = 0 ; x < args.length ; x--)

x [2] = hundreds
look up hundreds method

x [1] = tens
look up tens method

x [0] = ones
loop up ones method

I have been looking at the arrays and thinking of possible ways to resolve the question, without having to write 99 string occurences.

Thanks,

-maritza
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
I should have said, yes, there is a way (see the toCharArray() method of the String class). However, there is probably a better way to do what you're trying to do. While you're looking at the String class, have a look at the charAt() method.

Since you are working with 900, it seems that you have already written some code that works for 0 - 99. You are on the right track when you say, "without having to write 99 string occurences." If you search in this forum for "say" or "assignment 4", you'll see that many people have struggled with this. The key is to look for things that the numbers have in common.
[ April 02, 2006: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Maritza, I think args.length is actually 1, not 3. The String which is stored as the first object in args[] is 3 characters long, but the array itself has only one item. I don't think your code will work!
Maritza Keisman
Greenhorn

Joined: Mar 28, 2006
Posts: 7
Hi,

It does not work. I needed to move args[0] to a string. Then use the length of the string to determine, how many times I needed to loop in the for next loop.

When I used charAt() in your For Loop, it does parse out the string. Now I have to figure out, how to write the methods.

Thanks,

-maritza
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Originally posted by Maritza Keisman:
Hi,

It does not work. I needed to move args[0] to a string. Then use the length of the string to determine, how many times I needed to loop in the for next loop.

When I used charAt() in your For Loop, it does parse out the string. Now I have to figure out, how to write the methods.

Thanks,

-maritza


This is going to be really tricky, since the method for some some digits will change depending on the (varying) value of the output of other digits. You're taking an challenging tack on this - parsing using the base ten representation of the numbers instead of parsing based on the actual mathematical value. If you stick with this and get it to work out, I would be curious about seeing it.


-Adam
John Simpson
Ranch Hand

Joined: Nov 28, 2005
Posts: 30
Hi,

you could use the subString method to treat the String as an array.
Something like:

substring( i, ( i + 1 )

will return a single character of the string. You can then loop through the string varying i.
It's not really an array, and it's probably terribly inefficient, but it will more or less work (I think).

John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Character Strings Redefined As Arrays