• Post Reply Bookmark Topic Watch Topic
  • New Topic

Going mad with a program  RSS feed

 
Chris D Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Totally new to this programming lark so grateful for any help I am trying to write a program that works out probability and entropy of a given message (code below). It won't compile. Can anybody give me a clue to where I'm going wrong or tell me if I'm in the wrong ball park? 1 error at line 27. identifier expected.

 
Paddy Howard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there's a couple of potential issues. I think the immediate one might just be that in the line:



"message" should be a double.

However, I think you'll experience two sets of further problems. Both of these are common problems for when you start coding in Java.

The first problem is that you're trying to access non-static methods from a static method. Very simply, if you qualified all those methods as "static" - if you wrote



that individual compile problem would be avoided. However, I'm sure some of the folk here will quickly point out that, if you write Java code that solely consists of static methods, you're not really using the features that Java is offering. So I'm only saying that to give an idea of how you might get this code to compile and run.

Another issue in the code is around the scope of variables. If you declare a variable within a method, it won't be seen outside that method. I think that issue is present in the "entropy" variable.

Hope that's of some use. If you google something like "non-static method in static context" and "scope local variable Java", I'd expect you'll find sample code that demonstrates the issues.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When source code doesn't successfully compile, there are resulting compiler errors that are helpful to explain and locate the errors. Learning what those errors mean is an important step in debugging your code. Please copy and paste the error and stack trace into your post so that we can help you understand the error and learn what to do about it or them.

Good luck!
 
Chris D Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,

Made a few changes but still getting one error! Error is java:45:error: missing return statement


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's telling you exactly what's wrong. There's no return statement. Your method claims that it returns a double, but then doesn't.
 
Chris D Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the advice and I have removed the double now.

Code now compiles and runs but now I have another issue! How do I return probA, probB, probC, probD, to the main? The probabilities can be printed in the calculateProb method but not in the main.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris D Wilson wrote:Thank you for the advice and I have removed the double now.

Code now compiles and runs but now I have another issue! How do I return probA, probB, probC, probD, to the main? The probabilities can be printed in the calculateProb method but not in the main.



If you want the calculateProb() method to return a result, then you should return the result -- instead of removing the declaration that it should return a double.

Henry
 
Chris D Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I return all 4 values or just one?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris D Wilson wrote:Can I return all 4 values or just one?


Have you learned of any way to contain those 4 values yet? ie. arrays, collection classes, or creating you own class?... so that it can be returned as one?

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice - without looking at a single line of what you wrote - is throw it all away and start over.

From skimming this thread, it seems like you are trying various things to try to get it to work, and not trying to understand what is happening. When you code, you should spend between 60 and 90% of your time THINKING about what needs to happen, and the rest coding. I will often literally write down in English what I want my code to do, step by step. I then try and revise it until I think my 10yr old daughter could follow the steps and not have any questions about what to do at any given point.

When I then start writing code, I write 2-3 lines at most before I compile and test. That means I do a LOT of compiling and testing, but I always know withing 2-3 lines were the newly discovered error has to be.

I think you wrote too much, without testing, without compiling, and without having a solid understanding of what your code should do.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Please use {} on all your loops and selection statements, as described here. Your lines 28‑30, etc., are all cramped together and need space and braces for reasons of legibility.
Please remove the (double) casts in lines 49‑52 because they are unnecessary.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:When I then start writing code, I write 2-3 lines at most before I compile and test. That means I do a LOT of compiling and testing, but I always know withing 2-3 lines were the newly discovered error has to be.

I could not agree more with this statement. The time taken for multiple compilations and running tests will normally still be less than the time sat scratching your head when you have written a load of code which you don't fully understand and it isn't doing what you expect it to.
 
Paddy Howard
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris D Wilson wrote:Thank you for the advice and I have removed the double now.
I don't mean to be disheartening, but I think Fred is giving sound advice above when he says
fred rosenberger wrote:throw it all away and start over.
I'm saying this because I notice you seem to have removed the "static" keyword from the "main" method, which means that you'll have no way of executing the program if it does compile.

I think you need to do some simpler code first, just to get an understanding of how to get one method to invoke another. Fred's idea of compiling a few lines at a time would be good, too.

I'd also feel it would be helpful to get your hands on a basic book on Java, just to start getting some understanding of what all these keywords are for. I know folk around here think highly of the "Head First Java" book, as it introduces the Object Oriented Programming concepts from the start. For what it's worth, I found the "SAMS" book good for a start - just to guide me into getting those first chunks of code to work - but maybe not so good on actually getting an appreciation of what Java offers that you won't find in other languages.
 
Chris D Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got the head first java book and am trying to work with it but I'm more of a traditional type of learner. After the advice about using an array I finally got it working, only to rip it to pieces again to answer another question. Only been learning for 4 weeks and must admit I'm struggling a bit.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris

Just take it at your own pace. Go through the topics but don't move onto a new one until you fully understand the one you are on.

And take the advice given here. Code and test in small increments.

Obviously, if you got stuck, come back here and someone will always be willing to help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!