aspose file tools*
The moose likes Beginning Java and the fly likes Help with coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with coding" Watch "Help with coding" New topic
Author

Help with coding

Viviana Torres
Greenhorn

Joined: Nov 01, 2011
Posts: 6
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

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^)


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Viviana Torres
Greenhorn

Joined: Nov 01, 2011
Posts: 6
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

Joined: Nov 01, 2011
Posts: 6
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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

Joined: Apr 06, 2010
Posts: 4467
    
    8

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

Joined: Nov 01, 2011
Posts: 6
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

Joined: Aug 16, 2005
Posts: 14352
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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

Joined: Nov 01, 2011
Posts: 6
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with coding