Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Newbie needs help.

Thread view: 
Ed - 17 Mar 2006 19:02 GMT
I have a program which includes a string array "tabDat[crds][i]". For each
"i", "crds" conains a string of various length and content, but generally
follows this example: "4?,  10?,  1?,   8?,  10?,". I need to change each of
these strings so that where "1" exists it will read "A", where "10" exists
it will read "Q" and so on.

For example: if tabDat[crds][0] now contains "4?,  10?,  1?,   8?,  10?,".
It will be changed to contain "4?,  Q?,  A?,   8?,  Q?,"

I've tried several ways to do this, including "replace" without success. Can
someone help me, please?
Monique Y. Mudama - 17 Mar 2006 19:25 GMT
> I have a program which includes a string array "tabDat[crds][i]".
> For each "i", "crds" conains a string of various length and content,
[quoted text clipped - 7 lines]
> I've tried several ways to do this, including "replace" without
> success. Can someone help me, please?

Could you please show us some code you tried and tell us what happened
when you ran the code?

There's not much point in just giving you an answer ... better to help
you work through the problem.  I'm also not sure what you mean by
"replace" ... the replace() method in the String class?  If so, do you
realize it doesn't change the String; it returns a new String?

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Ed - 17 Mar 2006 20:05 GMT
For example one try was something like this:
     {
        for (int i = 0; i < 5 ; i++)
        {
           str = tabDat[crds][i];
           str.replace('10'. 'Q');
           tabDat[crds][i] = str;
       }
    }

On compiling I got 6 errors: one said "<identifier> expected" with and up
arrow pointing to the single quote before "Q". Another said "'>' expected"
with an up arrow under ";". The other errors were similar. I worked over the
last 2 days trying several variations on the preceding without making any
progress.

>> I have a program which includes a string array "tabDat[crds][i]".
>> For each "i", "crds" conains a string of various length and content,
[quoted text clipped - 15 lines]
> "replace" ... the replace() method in the String class?  If so, do you
> realize it doesn't change the String; it returns a new String?
Monique Y. Mudama - 17 Mar 2006 20:27 GMT
> For example one try was something like this:
>       {
[quoted text clipped - 11 lines]
> last 2 days trying several variations on the preceding without making any
> progress.

Please don't top post.  It makes it hard for others to follow the
conversation.

Anyway, one of your problems looks to be what I mentioned in my
previous post.

Looking at your code:

str was never declared. You needed to declare it either by putting

   String str;

above the for loop or by changing the first line of the for loop to

    String str = tabDat[crds][i];

Next, the delimiter for method arguments is a comma, not a period.  So
you will need to change '10'. 'Q' to '10', 'Q'.  That explains the
error message about the Q.

Next, the point I was trying to make in my previous message.  String's
replace() method does not change the string on which it operates
(str); it creates a new string and returns it.  Try replacing the last
two lines of your for loop with a single line:

    tabDat[crds][i] = str.replace('10', 'Q');

I haven't tested any of this; it's just off the top of my head.

One note on style: you will make your code much more readable if you
replace the variable name 'crds' with something that makes sense on
casual inspection.  Cards?  Cathode Ray Death Star?  I can only guess.

   

>>> I have a program which includes a string array "tabDat[crds][i]".
>>> For each "i", "crds" conains a string of various length and content,
[quoted text clipped - 15 lines]
>> "replace" ... the replace() method in the String class?  If so, do you
>> realize it doesn't change the String; it returns a new String?

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Ed - 17 Mar 2006 21:00 GMT
>> For example one try was something like this:
>>       {
[quoted text clipped - 66 lines]
>>> "replace" ... the replace() method in the String class?  If so, do you
>>> realize it doesn't change the String; it returns a new String?

I had declared str earlier at the biginning of this Class. With the new line
it complains about the "unclosed character literal" and points to the 2
single quotes around the "10". Also it still complains about the "'>'
expected" with an up arrow under ";".

Also since I need to change a number of characters, is that a way to
double-up on replace, such as by seperating the groups with a ":" or ";"?

Ed
> http://www.catb.org/~esr/faqs/smart-questions.html 
Monique Y. Mudama - 17 Mar 2006 21:58 GMT
> I had declared str earlier at the biginning of this Class. With the
> new line it complains about the "unclosed character literal" and
> points to the 2 single quotes around the "10". Also it still
> complains about the "'>' expected" with an up arrow under ";".

Oh, right.  See Oliver's post.  '10' isn't valid because it's two
characters.  You'd have to use "10", and that's not a character, it's
a string.  Of course, the replace() method only works on characters
... so you can use it to transform '1' to 'A', but not for this other
part you're working on.

I'm not sure about the other thing.  But it may not matter, because it
looks like replace() isn't the method you need right now, anyway.

JavaDocs are your friend.  Are you reading the JavaDocs for String?
Because if you're not using JavaDocs, you're coding with your hands
tied behind your back.

Take a look at the JavaDocs for replaceAll().  This looks more
appropriate for what you're trying to do.  I know that it says that
the first argument is a regular expression, but you should be able to
pass "1" or "10" to this method, because they don't contain any
special regular expression characters.

> Also since I need to change a number of characters, is that a way to
> double-up on replace, such as by seperating the groups with a ":" or
> ";"?

I'm afraid I don't understand the question.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Oliver Wong - 17 Mar 2006 22:07 GMT
> Of course, the replace() method only works on characters
> ... so you can use it to transform '1' to 'A', but not for this other
> part you're working on.

   Ed, if you're using Java 1.5, they've added the feature where the
replace() method DOES work on Strings (to be precise, it works on any
CharSequence), so you can type in:

str = str.replace("10", "a");

   And it should do the "correct" thing. See
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace(java.lang.
CharSequence,%20java.lang.CharSequence
)

   However, if you're using an older version, like 1.4, it won't work, and
you'll have to find another way to do what you want (either that, or upgrade
to 1.5).

   - Oliver
Ed - 18 Mar 2006 15:54 GMT
>> Of course, the replace() method only works on characters
>> ... so you can use it to transform '1' to 'A', but not for this other
[quoted text clipped - 14 lines]
>
>    - Oliver

Thanks to all of you for help. I should have mentioned in the beginning I'm
using Java 1.3 - I'm very sorry about that it was my fault.

In addition to transforming "1" to "A", "10" to "Q" and several other
similar actions I later need to convert them back so "A" to "!", "Q" to
"!0", etc. Based on your comments 1.5 should do all that.

So I'll go ahead and download 1.5. Can someone give me the URL for the
download site. I have it somewhere but requires some searching? I suppose I
should remove 1.3 before installing 1.5 - any ideas on that?

Thanks again to Monique and the others.

Ed
Roedy Green - 18 Mar 2006 17:07 GMT
>So I'll go ahead and download 1.5. Can someone give me the URL for the
>download site.

see http://mindprod.com/jgloss/jdk.html
http://mindprod.com/jgloss/string.html#REPLACE
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ed - 18 Mar 2006 18:52 GMT
>>So I'll go ahead and download 1.5. Can someone give me the URL for the
>>download site.
>
> see http://mindprod.com/jgloss/jdk.html
> http://mindprod.com/jgloss/string.html#REPLACE

Thanks Roedy.

I'm running WinXP.

On the download, I seem to have a choice "beans or no beans". What do these
"beans" do?

Ed
Roedy Green - 19 Mar 2006 01:49 GMT
>On the download, I seem to have a choice "beans or no beans". What do these
>"beans" do?

I believe that is an IDE. You don't need it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 18 Mar 2006 17:07 GMT
> I suppose I
>should remove 1.3 before installing 1.5 - any ideas on that?

On windows, control Panel | remove software.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 17 Mar 2006 21:22 GMT
> For example one try was something like this:
>      {
[quoted text clipped - 11 lines]
> the last 2 days trying several variations on the preceding without making
> any progress.

   In addition to Monique's advice, you also can't have a character which
is two characters long. E.g. '10' is two character longs, but the single
quote character is used to enclose a single character, not a string. Perhaps
you meant "10" (note the use of the double quotes instead of the single
quotes).

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.