• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with coding

 
Viviana Torres
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to run this code and it's not working.... it keeps giving me the "Syntax error on token(s), misplaced construct(s) at line 45 (i've put it in red below). Can anyone help?

Thanks so much!



 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method compareToIgnoreCase ONLY taks a single argument as a string. You can only give it one thing. Further, 'or' isn't valid in java anywhere as you have used it.

Tell us, in plain english, what you are trying to do here.

Also, this code won't compile. Line 29 is just wrong...an 'else' doesn't take a condition, and what condition you have is incomplete.

There are other issues with the code you provide - you are missing at least two import statements as well.

Note: I have put code tags around your java to make it easier to read. It works just like bolding or italicizing - highlight everything you want to include, then click the "code" button above where you enter text. And since colors don't work inside sourcecode, I took them out and put in a comment indicating the line.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Viviana Torres, "bem vinda" to the Coderanch.

Wrap your code with the code tag.

Follow the tip above. (Removed my sample code, little but! ^o^)
 
Viviana Torres
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, still so very new to this and trying really hard to understand coding (as you can tell). I took out the or from the line, but now I get an error 2 lines below. I'm trying to turn away all animals that are not friendly, and all animals that are not black, nor white.



 
Viviana Torres
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the code wrapping tip! I took out the or from line 28, but now I get an error 3 lines below. I'm trying to turn away all animals that are not friendly, and all animals that are not black, nor white.


 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about making mistakes like that...it's how we all learn. Here are some general guideline which I cannot stress enough:

write no more than 2-3 lines before you compile and test
break projects down into tiny parts
test often
when you are done testing, test some more.


The problem with your most recent code is that the correct format should be



Note that there shouldn't be anything between the "else" and the curly bracket...unless you meant to have an "else if"

And I still think this is wrong:


According to the String api, compareToIgnoreCase only takes one argument. I think you need to do something like:



I think this says "if sColor is equal to "black" OR sColor is equal to "white""
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the next error not the one Fred's already mentioned? You can have an else if (...condition...) {...}, or an else {...} but not an else (...condition...) {...}.

else means "anything else other than what I just checked", so there's no need for additional conditions.
 
Viviana Torres
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the encouragement. I was doing good up until the point of the black or white condition. Now I'm confused

So now i've done it like this and i get this error "Error: The method compareToIgnoreCase(java.lang.String) in the type java.lang.String is not applicable for the arguments ()" I'm thinking the compareToIgnoreCase is wrong?? Sorry so many questions!

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compareToIgnoreCase method takes only one argument. You're still trying to call it with two arguments:

You seem to want to do: "if sColor is equal to "black" or "white", then ...". But you didn't express that correctly in your code.

Look at fred rosenberger's post above; he explained exactly how you should write that in Java.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler error will tell you what is wrong - or at least, a good place to start looking. Compiling this code, I get the following (and please, post these yourself next time - it would save me some time)


I put that in code tags just to preserve the spacing.

This is telling you you have two errors, one on line 28, and one on 31. You should always fix the first error first and then recompile, since syntax errors can often cascade, and fixing one fixed another 30 reported ones...

In any case, this is telling you that the method compareToIgnoreCase doesn't have a version that takes two strings (the 'applied to (java.lang.String,java.lang.String)' part).

You need to take one of those two string literals out - either the "black" or the "white".

The second error is telling you that the compareToIgnoreCase has no version that takes NO arguments (The "cannot be applied to ()" part).



I would say at this point, you need to forget about the code for a minute. write down in english what you are trying to do. What do you need to compare to what, and under which circumstances should you print "keep" and when should you print "send away"?
 
Viviana Torres
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I think I have everything good to go except for the all other colors besides black or white part. That's where I'm lost now. The code runs, finally (Huge YAY moment - Thank you everyone!!!) However, when the questions come up and the users respond "Fido, blue, yes" then the code doesn't display what it's supposed to - "Fido, I'm sorry, I cannot keep you".....

So how do I get it to display "Fido, I'm sorry, I cannot keep you" when a user enters any other color besides black or white?

The program is supposed to turn away all animals that are not friendly, and all animals that are not black, nor white.



Here's what I have so far:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another tip:

ALWAYS use curly braces on if/for/while/whatever statements, even when you don't need them. Here is your code with them inserted:




So let's go through your logic, with the inputs you suggest of "fido, blue, yes".

the compareTo on line 22, "yes", should return true, so we go into the next one...
the compareTo on line 23, "black" will return false, so we drop out of that condition, which puts us on line 28, which is just the close curly. We now go to line 29.
The the compare to "no" will not return zero, so we skip the body of this condition, and thus exit the they try-catch block entirely, printing nothing.

This is why I keep saying you need to think through your logic. When should you print "keep", and when should you print "send away" messages? Be specific. Don't just say "if age is 2 or 3". java doesn't think that way. You have to say it like "if age is 2 or age is 3". try figuring that out for your color and nice variables, again in English.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic