Results 1 to 19 of 19
- 05-24-2010, 09:06 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
Easy How do i fix it? it doesn't compile
Java Code:public class Error2B { public static void error(int n) { /** * Prints all the multiples of 10 numbers less than the given number * n the upper limit for the numbers */ for (int i = 1; i < n; i++) { if (i % 10 = 0) { System.out.println(i); } } } }Last edited by Eranga; 05-26-2010 at 03:52 AM. Reason: code tags added
- 05-24-2010, 09:42 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You need a method called main() so that the runtime knows where to start.
Also, "error" is a bad name for your method (but that won't stop it from working). Error2B is a bad class name too, but that may not have been your choice.Java Code:public static void main(String[] args) { // TODO: your code goes here }
-Gary-
- 05-26-2010, 12:28 AM #3
When you get errors, please copy and paste full text here.it doesn't compile
- 05-26-2010, 01:27 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
f (i % 10 = 0)
should be
f (i % 10 == 0)
- 05-26-2010, 02:54 AM #5
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
that's right, there is no main method, in java SE, you must have main method to compile your program...
best regard
akbar
Hibernate Criteria Query - MySQL Command Syntax Dictionary
- 05-26-2010, 03:05 AM #6
I don't think so. Can you give me an example? Please include the compiler error message(s). What about Applets?you must have main method to compile your program
The main() method is what the java program looks for when you try to execute an application. It's the starting point for execution when using the java program.
- 05-26-2010, 03:26 AM #7
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
yes, that's i mean, main() method for execute the program, not compile. compiler for made to perform iteration errors in code.
- 05-26-2010, 11:24 AM #8
hi dsohi1821,
take this code
just changeJava Code:public class Error2B { public static void error(int n) { /** * Prints all the multiples of 10 numbers less than the given number n * the upper limit for the numbers */ for (int i = 1; i < n; i++) { if (0 == i % 10) { System.out.println(i); } } } // public static void main(String[] args) { // error(123); // } }
intoJava Code:if (i % 10 = 0)
Java Code:if (0 == i % 10)
i hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project
- 05-26-2010, 01:34 PM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Is there any difference between:
a)Java Code:if (0 == i % 10)
b)Java Code:if(0 == (i % 10))
c)Java Code:if( i % 10 == 0)
d)Need opinion.Java Code:if ((i % 10) == 0)
Last edited by cselic; 05-26-2010 at 01:48 PM. Reason: fixing code in option b)
- 05-26-2010, 01:39 PM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
a) c) and d) are equal in function. However, this:
is erroneous. i == 0 is a comparison, that returns a boolean, true or false. What exactely is true % 10 supposed to be?Java Code:(i == 0) % 10
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-26-2010, 01:47 PM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
I have made a mistake.
For b it should be:
I asked for opinions from the point of good programming practice.Java Code:if(0 == (i % 10))
What is recommended to use when someone write a program?
My personal opinion is d)
Is it a) better, faster, more profesional, or it's just one of the programmer's styles?Last edited by cselic; 05-26-2010 at 01:51 PM.
- 05-26-2010, 01:52 PM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
If I'd have to pick, I'd pick d) also, just for the sake of clarity.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-26-2010, 03:38 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Are you on a mission to simply give people the answer?
It really doesn't help you know.
- 05-26-2010, 03:45 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
I'd pick c) because I just happen to know that the % opertor has higher precedence than the == operator. I think everybody should know that simple fact. Also I never write 2+(3*4) because I know it's the same as 2+3*4.
It's outright ludicrous to write 0 == (i%10) (as was suggest in the reply #8). 0 equals 0, I know that, I'm interested in the fact whether or not i%10 == 0.
kind regards,
Jos
- 05-26-2010, 03:55 PM #15
me too.
Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
but i must admit that sometimes braces makes a term more readable, even if not needed.
- 05-26-2010, 04:05 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
I tend to over-brace...for I am a developer of very little brain.
...I mean I have better things to remember than precedence.
:)
- 05-26-2010, 04:16 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Ok, give us one little example of those 'better things'; I mean, I can hardly remember where I left my tobacco ;-)
Parentheisization (is that a word?) is a good thing: when in doubt: parenthesize; I'm almost never in doubt when it comes to those silly parentheses ;-)
kind regards,
Jos
- 05-26-2010, 04:30 PM #18
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Some people like to put the constant for comparison on the left so that if they absent-mindedly do
instead ofJava Code:if (0 = checkThisValue) {
they will get a compile error. Of course, in JavaJava Code:if (0 == checkThisValue) {
will give a compile error anyway, but in some languages (C, PHP, others), where a zero is considered false, and a non-zero int is considered true, the results of an assignment can be used in an if statement, so that code would compile, but not do what you want.Java Code:if (checkThisValue = 0) {
-Gary-
- 05-26-2010, 04:30 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Similar Threads
-
in need of help (easy)
By fasck in forum New To JavaReplies: 5Last Post: 12-30-2009, 10:45 PM -
how easy it is?
By ron87 in forum New To JavaReplies: 0Last Post: 04-01-2009, 06:36 PM -
Not so easy is it.
By Roy Gardiner in forum IntroductionsReplies: 0Last Post: 10-24-2008, 04:59 PM -
What does this mean (Very Easy)
By Zebra in forum New To JavaReplies: 6Last Post: 05-01-2008, 01:46 PM -
Easy question
By JavaNoob in forum New To JavaReplies: 10Last Post: 08-03-2007, 10:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks