Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem w/conditional...

0 views
Skip to first unread message

Frances Del Rio

unread,
Oct 3, 2004, 11:53:26 AM10/3/04
to

I'm learning java.. following an assignment given to me by a programmer
at work I put together a little app that:

when you first run it it types a string, then prompts user to type part
of the string, and app returns sgmt of string user typed surrounded by
two chars before and after sgmt user typed..

I put a few lines in there to deal with when user types first two chars
of string, but what happens is not what I specify... pls see code here..
www.francesdelrio.com/java/string.html

lines in question near bottom of code..
(signaled with " //*************...") thank you..

Frances

Paul Lutus

unread,
Oct 3, 2004, 12:28:54 PM10/3/04
to
Frances Del Rio wrote:

>
> I'm learning java.. following an assignment given to me by a programmer
> at work I put together a little app that:
>
> when you first run it it types a string, then prompts user to type part
> of the string, and app returns sgmt of string user typed surrounded by
> two chars before and after sgmt user typed..
>
> I put a few lines in there to deal with when user types first two chars
> of string, but what happens is not what I specify...

"What happens is not what I specify"? For a more efficient exchange, please
tell us:

1. What you hoped to achieve, in detail.

2. What happened instead, in detail.

3. How (1) and (2) differ.

> pls see code here..
> www.francesdelrio.com/java/string.html
>
> lines in question near bottom of code..
> (signaled with " //*************...") thank you..

I reviewed your code, and I still cannot tell what you wanted or how what
you got disturbs you.

To write computer programs, you must be perfectly clear -- reasonably clear
is not good enough. This is also true when describing problems with
computer programs.

--
Paul Lutus
http://www.arachnoid.com

Frances Del Rio

unread,
Oct 3, 2004, 12:45:13 PM10/3/04
to

Paul Lutus wrote:

> Frances Del Rio wrote:
>
>
>>I'm learning java.. following an assignment given to me by a programmer
>>at work I put together a little app that:
>>
>>when you first run it it types a string, then prompts user to type part
>>of the string, and app returns sgmt of string user typed surrounded by
>>two chars before and after sgmt user typed..
>>
>>I put a few lines in there to deal with when user types first two chars
>>of string, but what happens is not what I specify...
>
>
> "What happens is not what I specify"? For a more efficient exchange, please
> tell us:
>
> 1. What you hoped to achieve, in detail.
>
> 2. What happened instead, in detail.
>
> 3. How (1) and (2) differ.
>
>
>>pls see code here..
>> www.francesdelrio.com/java/string.html
>>
>>lines in question near bottom of code..
>>(signaled with " //*************...") thank you..
>
>
> I reviewed your code, and I still cannot tell what you wanted or how what

> you got disturbs you..

ok, that if user types first two chars in string (instead of any other
part of string..) something should happen (it doesn't matter what.. just
now for testing purposes I put in there that app should return pos # in
string of sgmt of string user typed..) but instead app returns an error..

string is:
Hamlet is sitting on the fence, wondering whether to be or not to be..

I want if user types in 'hamlet' (i.e,. beginning of string..) I want to
be able to say something (it doesn't matter what, for now, for testing
purposes..) but: instead of doing what stmt says it returns 'this phrase
was not found', i.e., it triggers error..)

} else if (hamlet.indexOf(user) == 0) {
System.out.println(" " + hamlet.indexOf(user));
} else if (hamlet.indexOf(user) == 1) {
System.out.println(" " + hamlet.indexOf(user));

I get error if user types what is specified here (namely first two chars
of string..) instead of executing System.out.println(" " +
hamlet.indexOf(user)); which what I said should happen... it returns
error..

again, complete code at www.francesdelrio.com/java/string.html
(problem code clearly indicated with '// ***********..')

thank you... Frances

Paul Lutus

unread,
Oct 3, 2004, 1:05:01 PM10/3/04
to
Frances Del Rio wrote:

/ ...

>> I reviewed your code, and I still cannot tell what you wanted or how what
>> you got disturbs you..
>
> ok, that if user types first two chars in string (instead of any other
> part of string..) something should happen (it doesn't matter what..

Then, because something happened, doesn't matter what, your program is a
complete success. But you posted as though this criterion was not met (hard
to believe).

> just
> now for testing purposes I put in there that app should return pos # in
> string of sgmt of string user typed..) but instead app returns an error..

What error? Not all errors are equal. Some are more equal than others. We
need to see the actual text of the error message, copied from your display
to the body of your Usenet posting, with no changes whatsoever.

> string is:
> Hamlet is sitting on the fence, wondering whether to be or not to be..
>
> I want if user types in 'hamlet' (i.e,. beginning of string..) I want to
> be able to say something (it doesn't matter what, for now, for testing
> purposes..)

This is a recipe for declaring any program a total success. You need to say
what your program was expected to do, what it did instead, and how they
differ, specifically.

> but: instead of doing what stmt says it returns 'this phrase
> was not found', i.e., it triggers error..)

What error? Is this really so difficult?

>
> } else if (hamlet.indexOf(user) == 0) {
> System.out.println(" " + hamlet.indexOf(user));
> } else if (hamlet.indexOf(user) == 1) {
> System.out.println(" " + hamlet.indexOf(user));
>
> I get error if user types what is specified here (namely first two chars
> of string..) instead of executing System.out.println(" " +
> hamlet.indexOf(user)); which what I said should happen... it returns
> error..

WHAT ERROR?

>
> again, complete code at www.francesdelrio.com/java/string.html
> (problem code clearly indicated with '// ***********..')
>
> thank you... Frances

Okay, with all respect and based on current evidence, I think you are not
cut out for this line of work. Maybe you should consider one of the many
branches of human life in which waving your hands in the air counts as
meaningful activity.

Frances Del Rio

unread,
Oct 3, 2004, 1:24:52 PM10/3/04
to

Paul Lutus wrote:

gosh, I don't know how else to exlain this... if I say if car is red do
'A' and the car IS red and instead of doing 'A' it says something like
'car not found'... this is the problem... WHAT error?? error it
triggers is what it says in 'try' function it should do if there's a
problem.. evidently there IS a problem but I don't know what it is...
syntax is ok, since otherwise app wouldn't compile at all.. i.e., error
is 'phrase not found. try again or press 'Q' to quit..' I mean what does
this tell you? that in 'try' function it says if there's a problem print
such-and-such, it prints such-and-such.. can't say more precisely WHAT
error it is.. error it says in 'try' function to return if there's an
error..) ok.. thanks.. Frances


>>again, complete code at www.francesdelrio.com/java/string.html..


Andrew Thompson

unread,
Oct 3, 2004, 1:42:02 PM10/3/04
to
On Sun, 03 Oct 2004 13:24:52 -0400, Frances Del Rio wrote:

> WHAT error?? error it
> triggers is what it says in 'try' function

You can have a 'try/catch block', but the word 'function'
makes me think of Javascript (yes, Frances, I've seen your
posts over on c.l.js)

>...it should do if there's a

> problem.. evidently there IS a problem but I don't know what it is...
> syntax is ok,

What are you doing inside the 'catch'?

The exception is a gold-mine of information.
<http://www.physci.org/codes/javafaq.jsp#stacktrace>
..which can then allow you to be..
<http://www.physci.org/codes/javafaq.jsp#exact>

Sometimes the folks on usenet will be able to determine what
is wrong just from the stacktrace, but it is often helpful to
accompany it with an SSCCE*. The stacktrace will be quoting
exact line numbers of the code.

* <http://www.physci.org/codes/sscce.jsp>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane

Frances Del Rio

unread,
Oct 3, 2004, 2:41:30 PM10/3/04
to

Andrew Thompson wrote:

> On Sun, 03 Oct 2004 13:24:52 -0400, Frances Del Rio wrote:
>
>
>>WHAT error?? error it
>>triggers is what it says in 'try' function
>
>
> You can have a 'try/catch block', but the word 'function'
> makes me think of Javascript (yes, Frances, I've seen your
> posts over on c.l.js)
>
>
>>...it should do if there's a
>>problem.. evidently there IS a problem but I don't know what it is...
>>syntax is ok,
>
>
> What are you doing inside the 'catch'?
>
> The exception is a gold-mine of information.
> <http://www.physci.org/codes/javafaq.jsp#stacktrace>
> ..which can then allow you to be..
> <http://www.physci.org/codes/javafaq.jsp#exact>
>
> Sometimes the folks on usenet will be able to determine what
> is wrong just from the stacktrace, but it is often helpful to
> accompany it with an SSCCE*. The stacktrace will be quoting
> exact line numbers of the code.
>
> * <http://www.physci.org/codes/sscce.jsp>

Andrew, thank you very much for yr helpful response... the problem is I
can't TELL what error, I confess I still don't know how to handle
errors, there's a 'try' block that essentially says 'if there's sthg
wrong w/app do 'A'.. so it does 'A' when a certain cond is met (instead
of doing what I tell it to do when that certain condition is met.. it
still doesn't tell me what's wrong... (I do know 'function' is wrong
term here.. reverting back to old habits....;)..) re SSCCE, I did post
a compilable example at http://www.francesdelrio.com/java/string.html..
which does meet all yr SSCCE criteria.. (if you scroll down you'll see
what I mean, problem code denoted with '//***********..') I explained
as clearly as I could what the problem is..
I just realized by looking at code again that error is not triggered by
catch (Exception e) block.. it's triggered by sthg else (if user types
xyz do abc.. it behaves as if user is typing sthg not contained in
string at all, that's bottom line.. if I take out 'try' wrapper so it
gives me 'default' error (??), it won't compile (says 'class not
found'?? what does this mean?? all of a sudden class not found? it
doesn't say WHAT class is not found.. I'm so frustrated, I bet this is
a really simple problem..)

again.. thank you Andrew, you're always very helpful... Frances

Andrew Thompson

unread,
Oct 3, 2004, 3:17:29 PM10/3/04
to
On Sun, 03 Oct 2004 14:41:30 -0400, Frances Del Rio wrote:
...
> .....re SSCCE, I did post

Sorry, I missed it, and I notice you are printing the stacktrace.

> ..(if you scroll down you'll see

> what I mean, problem code denoted with '//***********..')

OK. I see the commented lines, but it is not quite clear
what you are trying to achieve there.

I notice you are printing the 'found phrase' as well as
two characters before and after the found phrase, but what
is supposed to happen at the beginning and end of the String?

Is the code supposed to report 'not found'? Is it supposed
to print the found string less the two extra characters?
Is it supposed to tell the user they are an idiot and crash with
lots of exceptions?

It is difficult to tell you how to make the code behave a
different way if we do not understand what the required
behaviour is.

Let's look at how your class behaves..

String:


Hamlet is sitting on the fence, wondering whether to be or not to be..

Type a phrase from above line
Hamlet
String index out of range: -1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:550)
at StringTest.main(StringTest.java:26)
Press any key to continue . . .

Now.. that's what I call an exception.
Are you sure you weren't typing 'hamlet' when you tried it?

Frances Del Rio

unread,
Oct 3, 2004, 3:48:52 PM10/3/04
to
Andrew Thompson wrote:

[.....]

> I notice you are printing the 'found phrase' as well as
> two characters before and after the found phrase, but what
> is supposed to happen at the beginning and end of the String?

THIS is what I'm trying to deal with.. I mean Andrew, this is just a
learning exercise.. I want to be able to say: if user types chars in
string return those chars surrounded by 2 chars surrounding phrase user
typed.. ELSE: if user typed 1st 2 chars @ beginning of string (or, once
I solve this problem, last 2 chars @ end of string) do such-and-such
instead... however: if user types 1st 2 chars @ beginning of string
this is what happens (it wasn't printing stacktrace before, don't know
why..)

as you noted below:
(how do you copy cocde from DOS shell? I can't, it won't paste..)

String index out of range: -1
java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.String.charAt(String.java:550)
at StringTest.main(StringTest.java:26)
Press any key to continue . . .

so my question is: WHY, if user types "Hamlet", I get that error? I
mean it's saying "Hamlet" is not part of the string... THIS is my
question... (I mean it doesn't say "phrase not found".. which is what
happens if user types chars not contained in string at all.. that is as
it should be..)

again, many thanks, Andrew..

Drostie

unread,
Oct 3, 2004, 4:01:58 PM10/3/04
to
Hello Frances,

***************
Let me first tell you what happens when I run your program.

If I type in "nderi" it gives me "wondering". If I type in "olga" it
tells me "This phrase was not found." If I type in "Hamlet" it throws an
exception.
***************

***************
Now I will deal with the exception first, because, well, exceptions are
important!

The stack trace tells me that the line causing the problem is:

char user_1 = hamlet.charAt(userpos_1);

and the error happens because userpos_1 is -1, which is an invalid index
for an array. This means that you need to handle "Hamlet" and "amlet"
better.

(Which I assume is what the lines you starred are supposed to do?)
***************

***************
Now on to the lines you starred:

They are unreachable.

Earlier on, you tested 3 things. The first two were trivial. The third
was if a query string ("user") was found in a bigger string ("hamlet").
You said that if this is not -1, you should run some code. I'll call the
number uIndex, but it stands for hamlet.indexOf(user). OK?

Let me write down a simplification of your control structure:

if (uIndex != -1)
doStuffA();
else if (uIndex == 0)
doStuffB();
else if (uIndex == 1)
doStuffC();

... If you don't see it yet, let me just put in the definition of
"else" into the control structure:

if (uIndex != -1)
doStuffA();

if (uIndex == -1 && uIndex == 0)
doStuffB();

if (uIndex == -1 && uIndex == 1)
doStuffC();

It should now be EXTREMELY obvious why doStuffB() and doStuffC() will
NEVER EVER EVER get done.
***************

Hope that helps,
--C.R. Drost

In article <2saljnF...@uni-berlin.de>, fd...@yahoo.com says...

Andrew Thompson

unread,
Oct 3, 2004, 4:29:53 PM10/3/04
to
On Sun, 03 Oct 2004 15:48:52 -0400, Frances Del Rio wrote:
> Andrew Thompson wrote:
> [.....]
>
>> I notice you are printing the 'found phrase' as well as
>> two characters before and after the found phrase, but what
>> is supposed to happen at the beginning and end of the String?
>
> ..I want to be able to say: if user types chars in
> string return those chars surrounded by 2 chars surrounding phrase user
> typed.. ELSE: if user typed 1st 2 chars @ beginning of string (or, once
> I solve this problem, last 2 chars @ end of string) do such-and-such
> instead... however: if user types 1st 2 chars @ beginning of string
> this is what happens (it wasn't printing stacktrace before, don't know
> why..)

I just want to point out that that statement goes a way to
fulfilling part of what Paul was saying.

(P.L. earlier)


1. What you hoped to achieve, in detail.

> as you noted below:


> (how do you copy cocde from DOS shell? I can't, it won't paste..)

I drag the mose across the text to select it, then hit 'enter'.

Alternately, you can invoke Java and redirect the output to a file.

java -cp . StringTest >op.txt

> String index out of range: -1
> java.lang.StringIndexOutOfBoundsException:
> String index out of range: -1
> at java.lang.String.charAt(String.java:550)
> at StringTest.main(StringTest.java:26)
> Press any key to continue . . .
>
> so my question is: WHY, if user types "Hamlet", I get that error? I
> mean it's saying "Hamlet" is not part of the string... THIS is my
> question... (I mean it doesn't say "phrase not found".. which is what
> happens if user types chars not contained in string at all.. that is as
> it should be..)

OK.. I think I already understand this part of the problem,
but I need to teach you how to analyse it.

So, let's start with. What is the exact line of the
StringText.java on which the error occurs?

Copy/paste it below.

(Hint, I found the line by looking at the stacktrace,
and the links I gave you earlier go into more detail
on how to interpret them.)

Frances Del Rio

unread,
Oct 3, 2004, 4:55:21 PM10/3/04
to

Andrew Thompson wrote:

> On Sun, 03 Oct 2004 15:48:52 -0400, Frances Del Rio wrote:
>
>>Andrew Thompson wrote:
>>[.....]
>>
>>
>>>I notice you are printing the 'found phrase' as well as
>>>two characters before and after the found phrase, but what
>>>is supposed to happen at the beginning and end of the String?
>>
>>..I want to be able to say: if user types chars in
>>string return those chars surrounded by 2 chars surrounding phrase user
>>typed.. ELSE: if user typed 1st 2 chars @ beginning of string (or, once
>>I solve this problem, last 2 chars @ end of string) do such-and-such
>>instead... however: if user types 1st 2 chars @ beginning of string
>>this is what happens (it wasn't printing stacktrace before, don't know
>>why..)
>
>
> I just want to point out that that statement goes a way to
> fulfilling part of what Paul was saying.
>
> (P.L. earlier)
> 1. What you hoped to achieve, in detail.
>
>
>>as you noted below:
>>(how do you copy cocde from DOS shell? I can't, it won't paste..)
>
> I drag the mose across the text to select it, then hit 'enter'.

cool...... this works..


>
>>so my question is: WHY, if user types "Hamlet", I get that error? I
>>mean it's saying "Hamlet" is not part of the string... THIS is my
>>question... (I mean it doesn't say "phrase not found".. which is what
>>happens if user types chars not contained in string at all.. that is as
>>it should be..)
>
> OK.. I think I already understand this part of the problem,
> but I need to teach you how to analyse it.
>
> So, let's start with. What is the exact line of the
> StringText.java on which the error occurs?
>
> Copy/paste it below.

ok.. here's stacktrace:

String index out of range: -1
java.lang.StringIndexOutOfBoundsException: String

at java.lang.String.charAt(Unknown Source)
at StringTest.main(StringTest.java:27)

I don't understand 'index out of range: -1' it's not out of range..

but, to follow yr instructions:

exact line on which this stacktrace says problem is is here:

at java.lang.String.charAt(Unknown Source)

which I don't quite understand b/c this line works for rest of app...
but ok, that's it for now, to respond to your post right away..


Andrew Thompson

unread,
Oct 3, 2004, 5:02:49 PM10/3/04
to
On Sun, 03 Oct 2004 16:55:21 -0400, Frances Del Rio wrote:
> Andrew Thompson wrote:
>> On Sun, 03 Oct 2004 15:48:52 -0400, Frances Del Rio wrote:
...

> String index out of range: -1
> java.lang.StringIndexOutOfBoundsException: String
> at java.lang.String.charAt(Unknown Source)
> at StringTest.main(StringTest.java:27)
...
> at java.lang.String.charAt(Unknown Source)

(chuckles) No, not quite..

When (new programmers are) checking stacktraces, (they can)
almost always ignore the bits that refer to Sun classes..

Have a closer look at the line of the stacktrace *after*
that one, the line that refers to *your* code..

( ..and then, for a shortcut, you might check what Drostie wrote,
I think they were a lot more explicit about something I am trying
to lead you to.. ;-)

Frances Del Rio

unread,
Oct 3, 2004, 5:17:13 PM10/3/04
to

Andrew Thompson wrote:

> On Sun, 03 Oct 2004 16:55:21 -0400, Frances Del Rio wrote:
>
>>Andrew Thompson wrote:
>>
>>>On Sun, 03 Oct 2004 15:48:52 -0400, Frances Del Rio wrote:
>
> ...
>
>>String index out of range: -1
>>java.lang.StringIndexOutOfBoundsException: String
>> at java.lang.String.charAt(Unknown Source)
>> at StringTest.main(StringTest.java:27)
>
> ...
>
>> at java.lang.String.charAt(Unknown Source)
>
>
> (chuckles) No, not quite..
>
> When (new programmers are) checking stacktraces, (they can)
> almost always ignore the bits that refer to Sun classes..
>
> Have a closer look at the line of the stacktrace *after*
> that one, the line that refers to *your* code..
>
> ( ..and then, for a shortcut, you might check what Drostie wrote,
> I think they were a lot more explicit about something I am trying
> to lead you to.. ;-)
>

I know, I'm digesting Drositie's post right now.. I see the problem..
problem is that at one point I say phrase user types -1, -2, etc..
which of course returns errors if user types 1st or 2nd char in string
b/c those positions (userPos -2, etc..) don't exist... he gives a
solution I'm trying to implement now... at any rate, main thing is NOW
I know what the problem is and can deal with it.. of course my next
plan is to learn error handling.. (realize I shouldn't have written
this app before doing that... ;)...) but thought I'd deal w/this first
b/c it deals w/cond stmts, which is what I'm covering now... (& wrote
this app a few weeks ago and still wasn't able to deal w/this problem..
and it was gnawing at me..)

Andrew, again, thank you very much.. Frances

Drostie

unread,
Oct 3, 2004, 5:46:21 PM10/3/04
to
Whoops, I just realized that I gave problems without advice. Sorry about
that. Let's see if I can't give you advice.

I think that this is a perfect place (syntactically) for a switch
statement.

if ("Q".compareToIgnoreCase(user) == 0) break;

else if (user != null && user.length() > 0) {
switch (hamlet.indexOf(user)) {
case -1:
//the stuff (user) was not found in hamlet.
//Treat that case in here.
break;

case 0:
//the stuff was in the first position.
//Treat that case in here.
break;

case 1:
//the stuff was in the second position.
//Treat that case in here.
break;

case (hamlet.length()-2):
//the stuff was in the second-to-last position.
//Treat that case in here.
break;

case (hamlet.length()-1):
//the stuff was the very last character in hamlet
//(no pun intended.)
//Treat that case in here.
break;

default:
//this is a normal case.
//Treat that case in here.
break;
}
}

Please notice that the breaks in the switch statement break the switch
statement, not the outer loop.

Frances Del Rio

unread,
Oct 3, 2004, 6:24:03 PM10/3/04
to

Drostie wrote:

C.R., thank you very much for your help and detailed explanation.. I'm
trying to implement your code...

for simplicity's sake this is what I want to do now:

return what user typed surrounded by two chars in string on each side of
what user typed (gosh, this is harder to articulate than I thought..)
but if phrase user types begins w/1st or 2nd char in string just return
what user typed + 2 chars in string that follow (and haven't even dealt
w/last two chars in string yet..)

[about 30 min, pass..]

ok, this is what I did now:
http://www.francesdelrio.com/java/string.html

one thing I did is at the begining I now have

hamlet.indexOf(user) > 1 // instead of

hamlet.indexOf(user) != -1

so solved this problem by saying if user types phrase that starts after
2nd char in string do this else do this... but, my code looks
'primitive', as I had to repeat all the vars inside 'else if' block
(variable scope issue..) am trying to figure out a way of putting all
vars outside 'if' blocks so don't have to repeat them.. but well, for
some reason can't find a way to do this.. still don't understand things
like
user = ins.readLine

because at the very top, where it says
String user = null;

I tried putting
String user = args[0];

and this didn't work and I don't understand why...

ok, now I have to deal with what happens if user types in phrase that
contains last two chars in string.. (gasp... hopefully this will not
take me another hour.. I need this app to be absolutely 'clean'..)
again, thank you very much for your help, to you and to Andrew.. Frances


Paul Lutus

unread,
Oct 3, 2004, 8:22:48 PM10/3/04
to
Frances Del Rio wrote:

/ ...

> I know, I'm digesting Drositie's post right now.. I see the problem..


> problem is that at one point I say phrase user types -1, -2, etc..
> which of course returns errors if user types 1st or 2nd char in string
> b/c those positions (userPos -2, etc..) don't exist... he gives a
> solution I'm trying to implement now... at any rate, main thing is NOW
> I know what the problem is and can deal with it.. of course my next
> plan is to learn error handling.. (realize I shouldn't have written
> this app before doing that... ;)...) but thought I'd deal w/this first
> b/c it deals w/cond stmts, which is what I'm covering now... (& wrote
> this app a few weeks ago and still wasn't able to deal w/this problem..
> and it was gnawing at me..)

If you write the program in a reasonable way, there will not be any errors
to handle. Try ... catch blocks are meant to trap errors that the
programmer cannot reasonably be expected to handle, like a file not
existing or a storage device becoming unavailable or filling up as a write
proceeds. String indices do not fall into this category -- they can be
handled with complete effectiveness using ordinary code statements and
tests.

Frances, please tell us what your program is meant to accomplish. Be
specific and concise. Try to separate how you believe it should be done
from simply saying what it is supposed to do. Do not say "It doesn't matter
what the purpose is". If you write a program that general, it will
generally fail.

I ask this because this promises to be one of those record-breaking Usenet
threads in which, in retrospect, it would have been far easier for someone
to write and give you the code than make you try to convert your idea of
what the program must do into actual code.

This is not the usual practice, but because you have no problem creating and
posting code, no one here will think you are trying to shirk your
responsibility to create and test your own code.

Finally, even if someone creates the code for you, it may have a tutorial
benefit so great that the fact that someone wrote it for you will not
represent an insurmountable obstacle.

Start here: I want my program to ____________________________

(25 words or less)

Drostie

unread,
Oct 3, 2004, 9:12:39 PM10/3/04
to
In article <2sbcg7F...@uni-berlin.de>, fd...@yahoo.com says...

If you follow my advice for a switch block (above), you can eliminate
some of the tediousness of the if-elseif-else block. Then scoping will
probably not be a issue.

As far as your most recent questions...

String user = args[0];

When you run the main method, you give it an array of strings separated
by spaces. You may not realize that you do this... if they have you
doing command-line work, then you would; if they have you doing a lot in
a JRE, then you might not. But it is not the same as saying
myBufferedReader.readLine().

In the command line, if I start the program by typing

java StringTest Hamlet hello

then I am declaring the variable at runtime to be:

args[0] = "Hamlet"
args[1] = "hello"

which is not what the buffered reader does, as I'm sure you realize.

Furthermore, if you don't give these args when you run it, then it's
like you declare the variable at runtime to be

args[] = null;

Which means that there is no args[0] for you to work with. This probably
generated a null pointer exception for you if you tried it. Using args
was not your idea, so don't use it unless you really need to.

One thing that will help you a lot is to get a good JRE that will indent
your work for you. I recommend getting DrJava. Google for it and then go
line by line through your code, hitting Tab on each line. DrJava will
automatically arrange your code so that it's more readable. Where you
hit tab doesn't matter; DrJava doesn't insert tab characters when you do
it.

Another note about style is that your code is hard to read because it
uses too many variables.

This all:
int userpos_2 = userpos - 2;
int userpos_1 = userpos - 1;
char user_1 = hamlet.charAt(userpos_1);
char user_2 = hamlet.charAt(userpos_2);
int ppos0 = userpos + userl + 0;
int ppos1 = userpos + userl + 1;
char pppos0 = hamlet.charAt(ppos0);
char pppos1 = hamlet.charAt(ppos1);

can be replaced by (and I hope you see this in a fixed-width font):

String modified_user = hamlet.charAt(userpos - 2)
+ hamlet.charAt(userpos - 1)
+ user
+ hamlet.charAt(userpos + user.length())
+ hamlet.charAt(userpos + user.length() + 1);

See? Only one variable, and it is clear what all is going into it and
why it exists.

For another note on style, steer clear from calling your user's input
"user". Give it a meaningful name. "stringIn" or "userString" or
something that tells me that it's something that was inputted by the
user. Because when I see user, I automatically think that it's an object
that abstractly symbolizes the user.

(Like the Player NPC in Morrowind, or the Ego object in Sierra games.)

Also, for style... you don't need to say "extends Object" at the
beginning. The compiler already knows that it extends Object; everything
extends Object. The coding convention

public class MyClass extends Object

typically means that you specifically want the user to know that your
class is meant to be instantiated. That is, you aren't creating it for
any static methods or anything, you're creating it because later you
will say:

MyClass m = new MyClass();

And if you're not going to say that, you should leave out "extends
Object."

One final note on style... I don't think that you ever use java.util.*
or java.text.*, so you can delete those lines from your header.

--C.R. Drost

Tony Morris

unread,
Oct 3, 2004, 9:17:48 PM10/3/04
to
> args[] = null;
>
> Which means that there is no args[0] for you to work with. This probably
> generated a null pointer exception for you if you tried it. Using args
> was not your idea, so don't use it unless you really need to.

The VM never behaves like this.
'args' will never be null, but it may be a zero-length array.
If you attempt to index a zero-length array, you will receive an
ArrayIndexOutOfBoundsException.

--
Tony Morris
http://xdweb.net/~dibblego/

Frances Del Rio

unread,
Oct 3, 2004, 10:50:36 PM10/3/04
to

Drostie wrote:

oh my gosh, I realize the diff. now.. args[] is for when you type sthg
when you're calling the app (java StringTest yada yada..) this of course
is diff. from when user types sthg in after being prompted.. ok... I
have to go... but have read rest of your post and have saved it and of
course will follow yr recommendations.. thank you very much.. Frances


Drostie

unread,
Oct 3, 2004, 11:24:35 PM10/3/04
to
thank you for the technical correction.

In article <0x18d.13215$5O5....@news-server.bigpond.net.au>,
n...@telling.you says...

Frances Del Rio

unread,
Oct 9, 2004, 12:42:46 PM10/9/04
to
I want to thank everyone who has helped me out with my little project...
I have finally gotten my little app to behave exactly the way I
want it to.. (complete code at
http://www.francesdelrio.com/java/string.html)

however, Drostie, I'm now trying to apply what you mentioned to me about
consolidating var's, etc.. but I'm getting very weird results...

when I apply this

> String modified_user = hamlet.charAt(userpos - 2)
> + hamlet.charAt(userpos - 1)
> + user
> + hamlet.charAt(userpos + user.length())
> + hamlet.charAt(userpos + user.length() + 1);

the weird thing is that hamlet.charAt(userpos - 2) returns an int, not a
char.. I thought this method always returned a char.. so, everything I
have tried trying to trouble-shoot this one:

System.out.println(modified_user); // this returns two int's,
// then user then two chars..

System.out.println(hamlet.charAt(userpos-2)); // this one returns
// a char


the weirdest thing is when I try this..

System.out.println(userpos-1 + "," + userpos-2);

I get the following error:

hamlet.java:41: operator - cannot be applied to java
System.out.println(userpos-1 + "," + userpos-2);

if I try this (diff. positions for userpos-2 & userpos-1..)

System.out.println(userpos-2 + "," + userpos-1);

I get pretty much same error..

hamlet.java:41: operator - cannot be applied to jav
System.out.println(userpos-2 + "," + userpos-1);


can you shed some light?? again, many thanks to everyone who helped me
out here.. Frances :)

0 new messages