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 11 of 11

Thread: 1-100 counter

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

    Lightbulb 1-100 counter

    I am new to Java programming and I have ran into a problem which I can not seem to solve. I have tried for two hours now to come up with a way to solve this problem with no luck. I want to print 1-100 and beside them label the number odd or even. The problem is, I can't get odd or even to show up next to each number. It does show up after 100 though. I have finally, after a while, been able to get it to print 1-100 in a row rather than linear. Now my problem is getting the odd and even numbers labeled. Also it is showing an error at the else statement now. You wouldn't believe how frustrated I am.



    import java.util.Scanner;
     
     
    public class counter
    {
     
    	Scanner in = new Scanner(System.in);
     
     
     
    	public static void main(String[] args) {
    		int count;
    		for(count = 1; count <= 99;count++)
    			System.out.println( count+",");
    			System.out.println(100);
     
    			if((count++%2)!=2);
    				System.out.print("odd"); 
    				else
    				System.out.print("even"); 
     
     
    }
    }
    Last edited by jps; September 17th, 2013 at 06:07 PM. Reason: code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: 1-100 counter

    Welcome to the forum.
    Always post the full text of the error message with your code and question.
    Use code tags when posting code, instructions can be found on the Announcements - What's Wrong With My Code?.

    Start with the syntax error first, once you get it to compile and run again, work on the rest. See this tutorial on if-else in Java
    It seems the if statement was terminated early with a semicolon in a bad place.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,070
    My Mood
    Mellow
    Thanks
    243
    Thanked 752 Times in 738 Posts

    Default Re: 1-100 counter

    Please post your code in code tags. You can learn how to do that and many other useful things about the forum in the Announcements topic at the top of the page.

    It's a best practice to include the bodies of loops inside braces, '{}' even if they're only one line long. In this case, there should be several lines in the body of your for loop, yet you have only one. Your for loop should look like:

    Pseudo-code:
    for ( counter = 1 to 100 )
    {
    	// print the counter WITHOUT a linefeed
     
    	if ( the counter is even )
    	{
    		// print "even" 
    	}
    	else
    	{
    		// print "odd"
    	}
    }
    I'm not sure why you have the println( 100 ) statement in the middle. That shouldn't be there at all.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 1-100 counter

    I have fixed the syntax error and I also took out the useless println for the 100. I still can't figure out what I need to do to print odd and even beside the appropriate numbers. UGH!

    public class counter
    {
     
     
     
     
     
    	public static void main(String[] args) {
    		int count;
    		for(count = 1; count <= 100;count++)
    			System.out.print( count+",");
     
     
    			if((count++%2)!=2)
    				System.out.print("odd");
    				else
    				System.out.print("even"); 
     
     
    }
    }

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,070
    My Mood
    Mellow
    Thanks
    243
    Thanked 752 Times in 738 Posts

    Default Re: 1-100 counter

    There's no point in asking for advice if you're not going to use it.

    Put the entire for loop clause, including the if statement, inside braces, '{}'.

    The 'if' statement needs work. Since you want to determine if the count is even, you can use the modulo (or remainder) operator:

    if ( count % 2 == 0 ) // the expression is true if count is even

    You'll also want to add a linefeed (using println()) after "odd" or "even". To make it look nice, you might add a space either after the ',' in the previous print() statement or before " odd" or " even".

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 1-100 counter

    I was taught the method of using brackets I originally posted. I have attempted to use what you all have said but it is hard to take words and transfer it to code. Here is where I am at:
    [highlight = java]public class counter

    {




    public static void main(String[] args) {
    int count;
    { for(count = 1; count <= 100;count++)
    System.out.print( count+",");{
    }
    {
    if(count % 2 ==2)
    System.out.print ("odd");}

    { if (count % 1 ==1);

    }}
    { System.out.print("even"); }
    }
    }
    [/highlight]

    Still no luck as far as printing odd or even beside the numbers.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: 1-100 counter

    You need to go back to basics in regards to for loops. When you do not braces { } around your loop only one line, the next line, is inside the loop. You also need to place the braces in the correct location. Currently your code is all over the place like a mad woman's knitting
    Improving the world one idiot at a time!

  8. #8
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: 1-100 counter

    It'd be much easier to understand if you used the code tags here... but I can help.

    GregBrannon gave a simple way of testing if an integer is even.

    If an integer is not even, then it must be odd, so you only need to test for an even integer.

    If ( count % 2 == 0 ), integer is even, so print count + ", even"
    Else, integer is odd, so print count + ", odd"

    You can streamline your code a bit by defining count inside of the loop statement itself.

    Your syntax looks messy, and you have an unnecessary print statement at the beginning of your code. Use println instead, and handle all the printing inside the if/else statement.

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 1-100 counter

    The code shown below is ....
    /*Considered spoonfeeding, and removed*/

    I believe that you need to review the basics of loops and how the System.out.println actually work. Reading other peoples code is often helpful so I provided you with my solution. If you don't understand anything, just ask.
    Last edited by jps; September 18th, 2013 at 03:42 AM. Reason: spoonfeeding

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: 1-100 counter

    Please do not spoonfeed fully coded solutions.
    Improving the world one idiot at a time!

  11. #11
    Member
    Join Date
    May 2013
    Posts
    107
    My Mood
    Amused
    Thanks
    16
    Thanked 10 Times in 10 Posts

    Default Re: 1-100 counter

    if((count++%2)!=2)
    That statement is needlessly convoluted. If you're going to use an increment or decrement operator like count++ or count--, you should do it all by itself on its own line. Never mix it with other statements. Inside the head of a for loop, such as for(count=0; count<10; count++), is fine because the count++ is its own independent statement. When you mix something like that in with the rest of a statement, you make things unpredictable because your variable will be incremented somewhere midstream and you'll lose track of the variables.

    Also, that if statement will always be true regardless of what the value of count is. Modular division only considers the remainder. If you divide any number by 2, the highest remainder you can get is 1. So any number % 2 will != 2.

    You're on the right track, except for your misplaced curly brackets, as others have mentioned. Whenever you do a loop, if, or switch, the only thing that is affected by it is the very next statement. In this case, your for-loop has only one statement affected--the System.out.print(count +", "). To get around this and include more statements, you need to wrap your code up in curly brackets. Everything in curly brackets following a conditional will be affected by that conditional. So....

    for(int x=1; x<10; x++)
    ...only this statement is affected by the for loop...

    for(int y=10; y>10; y--)
    {
    ...this statement is affected by the loop....
    ....so is this one....
    ..... this one too...
    }
    ...this one is not...

    Some folks say that as a best practice you should do a curly bracket after every conditional, even if you only have one statement. There's no harm in doing that.

    Your if statement needs adjustment too. Ask yourself this: what is 4 % 2 (the remainder when you do 4 / 2). What is 5 % 2? What is 6 % 2? Get rid of that count++ in the condition.. it's incrementing a variable that doesn't need to be incremented and as I said that kind of thing is ridiculously confusing.

    --- Update ---

    Also, use println instead of print. Println automatically adds a newline at the end of each line and makes output much easier to read.

Similar Threads

  1. [SOLVED] Counter
    By mssim in forum Java Theory & Questions
    Replies: 2
    Last Post: November 7th, 2012, 05:32 AM
  2. Is Java 100% OOP
    By kbbaloch in forum Object Oriented Programming
    Replies: 5
    Last Post: December 29th, 2011, 12:54 AM
  3. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 08:50 AM
  4. Output of 0 or 100?
    By Scotty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2011, 05:15 PM