This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Help: Getting this error: Error:(3, 41) java: <identifier> expected

 
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thanks in advance for your time,

I am getting this error 'Error:(3, 41) java: <identifier> expected' on a really simple program. Why won't this code run?
 
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
on Line 3, the parameter a has no declared type. Specify what its type is. I'd guess you want that to be a double.
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I thought that wasn't necessary since I just declared the variable. But, now I'm getting this error
Error: Main method not found in class absvalprob, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application.

Do I need to put a main method before my own functions?
 
Junilu Lacar
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter where you add the main() method. You do need it if you're going to run this code with "java absvalprob" though.

By convention, class names should start with a capital letter and use camelcase for other words. So your class name should be AbsvalProb instead.
 
Junilu Lacar
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Khayla Matthews wrote:Oh, I thought that wasn't necessary since I just declared the variable. But, now I'm getting this error


The a that you declared on line 2 is not the same as the a you specify as a parameter to that method. Those are two entirely different things.
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'm still a little confused. I changed my code to the following and I'm still getting errors. What am I doing wrong? Thanks again for your help.

 
Junilu Lacar
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You put the absoluteValue() method inside the main() method. You can't do that.
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. No more errors! Thanks again.
 
Junilu Lacar
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method needs to have code that does something. You probably want to call the absoluteValue() method from the main() method and display its result.

Here's an example:
 
Marshal
Posts: 67050
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find the old Sun style guide, it tells you to avoid if (something) return someValue; else return differentValue;I would write that absValProb method like this:-Please find out what happens if you pass NaN as an argument.
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I rewrote the code to look like so and it runs perfectly with no errors. The format recommended by the style guide takes some getting used to but I think I like it better.


 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you find the old Sun style guide, it tells you to avoid if (something) return someValue; else return differentValue;I would write that absValProb method like this:-Please find out what happens if you pass NaN as an argument.

<br /> <br /> Not sure what you mean by passing NaN as an argument. I would like to see what happens though. I updated my code like so, and got a ton of erros. so I think I did it wrong. <br /> <br />
 
Saloon Keeper
Posts: 2898
370
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Khayla Matthews wrote:Not sure what you mean by passing NaN as an argument


He probably means something like: absoluteValue(Double.NaN)
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:. . . He probably means something like: . . .

Yes, I did.
Or absoluteValue(0.0 / 0.0)
Or absoluteValue(Math.log(-1.0))
Or absoluteValue(Math.sqrt(-1.0))
They will all end up sending NaN as an argument.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!