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

Thread: Simple Problem!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post

    Angry Simple Problem!

    Hello,

    I was trying to write a program that checks if any of the numbers that the user inputs are divisible by 3 5 or both. If the number is divisible by 3 it prints out "Fizz", if divisible by 5 then "Buzz" and lastly "FizzBuzz" for both. The program should also print out the other numbers anyway, which are not divisible by either 3 or 5.

    E.g. input : 15

    Output:
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz

    My code is below and I have attached a picture of the current output. Why is it like this? How do I make it work the way I want?

    P.S. The first bit of code which I have enclosed between /* */ is another solution of the problem by another person. I do not understand it, however, hence I tried doing it on my own! If somebody would also like to explain how it works to me I would be more than grateful!

     
    public class ArrayExample {
    	public static void main(String args[])
    	{
    		/*String words[] = new String[Integer.parseInt(args[0])];
    		for(int i = 0;i < words.length;++i)
    		{
    			words[i] = ""+(i+1);
    		}
    		for(int i = 2;i < words.length;i += 3)
    		{
    			words[i] = "Fizz";
    		}
    		for(int i = 4;i < words.length;i += 5)
    		{
    			words[i] = "Buzz";
    		}
    		for(int i = 14;i < words.length;i += 15)
    		{
    			words[i] = "FizzBuzz";
    		}
    		for(int i = 0;i < words.length;++i)
    		{
    			System.out.println(words[i]);
    		}*/
     
     
     
    	for (int i=0; i<args.length; i++) //MY OWN SOLUTION BEGINS 
    	{
     
    		for (int j=0; j<Integer.parseInt(args[i]); j++)
     
    		{
    			if (Integer.parseInt(args[i])%3==0) 
     
    			{
    				System.out.println("Fizz");
    			} 
     
    			if (Integer.parseInt(args[i])%5==0) 
     
     
    			{
    				System.out.println("Buzz");
     
    			} 
     
    			if (Integer.parseInt(args[i])%15==0) 
     
     
    			{
    				System.out.println("FizzBuzz");
     
    			} 
     
     
    		}
    		System.out.println(args[i]);
    	}	
     
    	}
    }

    Currently the program prints out this:

    ArrayExample.jpg


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

    Default Re: Simple Problem!

    You have several logical problems:

    You are not doing any counting. That is you do not start at one and check if that is divisible by 3 or 5, then check 2, then check 3 etc. Everytime around the loop all you check is if the number that user input is divisible by 3 or 5.

    You should use else if statements other wise your output for 15 will be
    Fizz
    Buzz
    FizzBuzz
    because all three if statements will be true.

    Finally, if you fix the above you will still get incorrect output for 15 if the if statements are evaluated in the wrong order.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    kassavetova (October 30th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Simple Problem!

    thank you. Have found a solution, not sure why I never thought of this before!

    for (int i=1; i<=Integer.parseInt(args[0]); i++) 
    	{
     
     
                if (i%15==0) 
     
     
        		{
    				System.out.println("FizzBuzz");
     
    			}			 
    			else if (i%5==0) 
     
     
    			{
    			System.out.println("Buzz");
     
     
    			} 
                else if (i%3==0) 
     
        		{
    				System.out.println("Fizz");
     
    			} 
     
            else{
               System.out.println(i);
     
            }

    Any chance somebody can explain the below code to me?

    String words[] = new String[Integer.parseInt(args[0])];
    		for(int i = 0;i < words.length;++i)
    		{
    			words[i] = ""+(i+1);
    		}
    		for(int i = 2;i < words.length;i += 3)
    		{
    			words[i] = "Fizz";
    		}
    		for(int i = 4;i < words.length;i += 5)
    		{
    			words[i] = "Buzz";
    		}
    		for(int i = 14;i < words.length;i += 15)
    		{
    			words[i] = "FizzBuzz";
    		}
    		for(int i = 0;i < words.length;++i)
    		{
    			System.out.println(words[i]);
    		}

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Problem!

    Any chance somebody can explain the below code to me?
    What do you think it does? Try commenting it, and repost your version with the comments.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    kassavetova (October 30th, 2013)

  7. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Simple Problem!

    No worries, I managed to figure it out !

Similar Threads

  1. Simple problem, please help!?!
    By CodyMoorea in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 10th, 2013, 06:46 PM
  2. Simple problem help!
    By spunkyk014 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 14th, 2013, 07:01 PM
  3. please help, simple problem
    By grandmst in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2011, 06:26 PM
  4. Very simple problem...PLEASE HELP!
    By dungeondragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2011, 07:19 AM
  5. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 6th, 2011, 12:02 AM