Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Character Strings Redefined As Arrays

 
Maritza Keisman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic