Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: find an error

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default find an error

    public class ForLoop{
    	public static void main(String args[]){
    		for(int i=50;i<80;i++){
    		if(i%2==0){
    		System.out.println(i);
    		}
    		else{
    		break;
    		}
    		}
    		}
    		}
    Last edited by pbrockway2; June 25th, 2013 at 11:31 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: find an error

    Hi nehaverma, wecome to the forums!

    When you post code, use the code tags. Put [code] at the start of a section of code and [/code] at the end. That way the forum software will attempt to format your code properly for a web page.

    You could clean up the indents a bit. Blocks of code are indented. And the closing } should go on a line of ots own.
    public class ForLoop{
    	public static void main(String args[]){
    		for(int i=50;i<80;i++){
    		    if(i%2==0){
    		        System.out.println(i);
    		    }
    		    else{
    		        break;
    		    }
    	    }
    	}
    }

    Part of the formatting may be due to using tabs. It's better, I think, to use spaces. They are more reliable.

    Now the problem... You should tell us what it is

    Does the code compile? If it doesn't compile and you can't understand the compiler'message, post the whole message.

    If it compiles but does not do at runtime what you expect or intend, describe what you intended and what the program actually did. If there was an exception, again post the whole stack trace (the long message that an exception produces on the console.)

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: find an error

    I cannot see any point to this code. For the first value of 50 it is even and enters the if branch and prints out the value. Then the value becomes 51 which is odd and enters the else branch and exits the loop and the program ends. Perhaps you can provide more details of what you are trying to achieve.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: find an error

    My guess is, he is trying to calculate how many even numbers there are between 50 and 80, but the thing is, you need to use different type of the variable i, because int can only be a full number, etc. 1,2,3,16...So when you modulus the number it will always be 0. You can try using "double" or "float".
    Hope this helps,
    G
    Quote Originally Posted by nehaverma View Post
    public class ForLoop{
    	public static void main(String args[]){
    		for(int i=50;i<80;i++){
    		if(i%2==0){
    		System.out.println(i);
    		}
    		else{
    		break;
    		}
    		}
    		}
    		}

Similar Threads

  1. find the error
    By nidhi goyal in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 1st, 2013, 07:48 AM
  2. Please Help me Find the Error
    By jugalrockz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 18th, 2012, 02:31 AM
  3. error :cannot find symbol
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 1st, 2011, 08:26 AM
  4. can any one plz find the error.
    By shalini_sns in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2011, 05:26 PM
  5. Need help, cannot find error
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 5th, 2010, 05:26 PM