Results 1 to 19 of 19
  1. #1
    HPcompaq256 is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Question 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

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You need a method called main() so that the runtime knows where to start.
    Java Code:
            public static void main(String[] args) {
                    // TODO: your code goes here
            }
    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.

    -Gary-

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    it doesn't compile
    When you get errors, please copy and paste full text here.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    f (i % 10 = 0)

    should be

    f (i % 10 == 0)

  5. #5
    me26 is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    you must have main method to compile your program
    I don't think so. Can you give me an example? Please include the compiler error message(s). What about Applets?

    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.

  7. #7
    me26 is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    yes, that's i mean, main() method for execute the program, not compile. compiler for made to perform iteration errors in code.

  8. #8
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    hi dsohi1821,
    take this code
    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 (0 == i % 10) {
    				System.out.println(i);
    			}
    		}
    	}
    //	public static void main(String[] args) {
    //		error(123);
    //	}
    
    }
    just change
    Java Code:
               if (i % 10 = 0)
    into
    Java Code:
               if (0 == i % 10)


    Quote Originally Posted by dsohi1821 View Post
    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);
               }
           }
        }
    }
    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

  9. #9
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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)
    Java Code:
     if ((i % 10) == 0)
    Need opinion.
    Last edited by cselic; 05-26-2010 at 01:48 PM. Reason: fixing code in option b)

  10. #10
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    a) c) and d) are equal in function. However, this:
    Java Code:
    (i == 0) % 10
    is erroneous. i == 0 is a comparison, that returns a boolean, true or false. What exactely is true % 10 supposed to be?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  11. #11
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have made a mistake.

    For b it should be:

    Java Code:
     if(0 == (i % 10))
    I asked for opinions from the point of good programming practice.
    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.

  12. #12
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Are you on a mission to simply give people the answer?
    It really doesn't help you know.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by m00nchile View Post
    If I'd have to pick, I'd pick d) also, just for the sake of clarity.
    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

  15. #15
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    I'd pick c) because I just happen to know that the % opertor has higher precedence than the == operator.

    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.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    I tend to over-brace...for I am a developer of very little brain.

    ...I mean I have better things to remember than precedence.
    :)

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    I tend to over-brace...for I am a developer of very little brain.

    ...I mean I have better things to remember than precedence.
    :)
    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

  18. #18
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Some people like to put the constant for comparison on the left so that if they absent-mindedly do
    Java Code:
            if (0 = checkThisValue) {
    instead of
    Java Code:
            if (0 == checkThisValue) {
    they will get a compile error. Of course, in Java
    Java Code:
            if (checkThisValue = 0) {
    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.

    -Gary-

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    More important things?
    Well, for starters I need to think about what I'm going to eat tonight...what to watch on TV...you know, useful stuff.
    :)

    As for doubt...I am riddled with doubt. I'm permanently amazed I ever actually deliver anything.

Similar Threads

  1. in need of help (easy)
    By fasck in forum New To Java
    Replies: 5
    Last Post: 12-30-2009, 10:45 PM
  2. how easy it is?
    By ron87 in forum New To Java
    Replies: 0
    Last Post: 04-01-2009, 06:36 PM
  3. Not so easy is it.
    By Roy Gardiner in forum Introductions
    Replies: 0
    Last Post: 10-24-2008, 04:59 PM
  4. What does this mean (Very Easy)
    By Zebra in forum New To Java
    Replies: 6
    Last Post: 05-01-2008, 01:46 PM
  5. Easy question
    By JavaNoob in forum New To Java
    Replies: 10
    Last Post: 08-03-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •