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

Thread: Problem getting code to print string after numbersn that qualkify for it. WHATS WRONG

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Problem getting code to print string after numbersn that qualkify for it. WHATS WRONG

    Hello, I am suppose to write program that displays number 1-50 and then also print HiFive next to the ones that are multiples of five, HiTwo if they are divisible by two and HiThreeOrSeven if they are divisibles of three or seven..

    My problem is it is printing all three of them for every single number, here is my code.

    public class QuestionFive {
    public static void main(String[] args) {


    int i = 1;
    int num = 50;
    String hiFive = "";
    String hiTwo = "";
    String hiThreeSeven = "";

    while (i <= num) {

    if (i % 5 == 0);
    hiFive = "HiFive";


    if (i % 2 == 0);
    hiTwo = "HiTwo";

    if (i % 3 == 0
    || i % 7 == 0);
    hiThreeSeven = "HiThreeOrSeven";

    i++;

    System.out.println(i + "" + hiFive + "" + hiTwo + ""
    + hiThreeSeven);

    }
    }

    }


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

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    if (i % 5 == 0);
    hiFive = "HiFive";

    Don't put a semicolon as part of the if statement like that.

    Instead:

    if (i % 5 == 0) {
        hiFive = "HiFive";
    }

    Notice the use of brackets and indenting the block associated with an if statement. The braces are not actually required for a one line block, but they are generally considered a good idea as they improve readability and can help avoid errors.

    -----

    When you post code use the "code" tags. Put [code] at the start of the code and [/code] at the end. That way the code will be readable (indents and all) when it appears here on a web page.

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

    curmudgeon (September 16th, 2012)

  4. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    You have misplaced semicolons that are short-circuiting your if blocks.

    In other words, it's not:
    if (foo == 3);
      bar;

    but rather:


    if (foo == 3)
      bar;

    See the difference? Also, you will want to enclose *all* blocks in curly braces as this will save your tail in the future. So even better is:


    if (foo == 3) {
      bar;
    }

  5. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    ok i made my changes but I am still getting it for everynumber, is something wrong with my strings?

    im gettin
    51HiFiveHiTwoHiThreeOrSeven
    50HiFiveHiTwoHiThreeOrSeven
    49HiFiveHiTwoHiThreeOrSeven

    instead of for example
    51 HiThreeOrSeven
    50 HiFive HiTwo
    49 HiThreeOrSeven

    the actual question is:
    Write a complete program that prints numbers from 1 to 50, but if numbers that are multiples of 5, print HiFive, else if numbers that are divisible by 2, print HiTwo, and else if numbers that are divisible by 3 or 7, print HiThreeOrSeven.

    my code now is:

     
    public class QuestionFive {
    public static void main(String[] args) {
     
     
    	int i = 1;
    	int num = 50;
    	String hiFive = "";
    	String hiTwo = "";
    	String hiThreeSeven = "";
     
    	while (i <= num) {
     
    			if (i % 5 == 0) {
    				hiFive = "HiFive";
    			}
     
    			if (i % 2 == 0) {
    				hiTwo = "HiTwo";
    			}
    			if (i % 3 == 0 
    				|| i % 7 == 0) {
    			hiThreeSeven = "HiThreeOrSeven";
    			}
    			i++;
     
    		System.out.println(i + "" + hiFive + "" + hiTwo + ""
    				+ hiThreeSeven);
     
    			}
    		}
     
    }

    also i believe i posted this in wrong forum did i?

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    i think i am suppose to replace the numbers with the correct string maybe to instead of 51 getting HiThreeOrSeven in place of it? if I am reading that right.

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

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    ok i made my changes but I am still getting it for everynumber
    I'm not sure that the strings are being printed for every number. Check the small numbers. When the problem occurs might give a clue about why.

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

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    Quote Originally Posted by Rstuart970 View Post
    i think i am suppose to replace the numbers with the correct string maybe to instead of 51 getting HiThreeOrSeven in place of it? if I am reading that right.
    Yes, I think you're right. Notice that every line will be HiSomething: that's because every number 2->50 is a multiple of at least one of 2,3,5, or 7. and notice that you only print one thing. (That's the meaning of the "else" in the instruction)

    if numbers that are multiples of 5, print HiFive, else if numbers that are divisible by 2, print HiTwo
    So ten, for example, should print "HiFive" even though it is a multiple of 2.

  9. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    your right im gettin

    2
    3HiTwo
    4HiTwoHiThreeOrSeven
    5HiTwoHiThreeOrSeven
    6HiFiveHiTwoHiThreeOrSeven
    7HiFiveHiTwoHiThreeOrSeven
    8HiFiveHiTwoHiThreeOrSeven

    and I am missing my 1 and i am gettin count to 51 instead of 50.

  10. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    is my System.out.println suppose to be out of the while brackets right below the end brackets?

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

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    Make sure you are clear about what you have to print. Then think about the code: you may well find that you don't need those string variables at all.

    for loops are the loops of choice when you know the range like this. But a while loop is ok. Inside the loop just do what the question says: if it's one thing print this, else if some other condition is true print that, etc.

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

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    Quote Originally Posted by Rstuart970 View Post
    is my System.out.println suppose to be out of the while brackets right below the end brackets?
    You are printing in the right place. The printing has to be inside the loop because you want to print for each number.

    But see my previous post and make sure you are really clear avout what has to be printed, then rethink your code.

  13. #12
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    Ok I have it working good now except for the fact my numbers start at 2 and end at 51, instead of 1 - 50,

    my code now looks like:

     
    public class QuestionFive {
    public static void main(String[] args) {
     
     
    	int i = 1;
    	int num = 50;
     
     
    	while (i <= num) {
     
    			if (i % 5 == 0) {
    				System.out.println("HiFive");
    			}
     
    			else if (i % 2 == 0) {
    				System.out.println("HiTwo");
    			}
    			else if (i % 3 == 0 
    				|| i % 7 == 0) {
    				System.out.println("HiThreeOrSeven");
    			}
    			i++;
     
    			System.out.println(i);
    	}
    		}
     
    }

  14. #13
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    I got it worked out for the 1-50

  15. #14
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    trying to get them to print on the same line as the numbers though instead of before the corresponding number, for example

    right now its printing

    1
    HiTwo
    2
    HiThreeOrSeven
    3
    HiTwo
    4
    i want it to print
    1
    2 HiTwo
    3 HiThreeOrSeven
    4 HiTwo

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

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    Quote Originally Posted by Rstuart970 View Post
    trying to get them to print on the same line as the numbers though instead of before the corresponding number
    It's hard to say what you're doing wrong without seeing the new code ...

    But, basically, printing the number should be in the last "else" of the if-elseif-chain. That's because you only what to print the number if it isn't a multiple that you've already checked for.

  17. #16
    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: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    Quote Originally Posted by Rstuart970 View Post
    ... print on the same line as the numbers...
    See System.out.print vs System.out.println

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

    Default Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

    Oh! I didn't read the "on the same line" business. Jps is right.

    I interpreted the question's "but" as meaning "instead of" - but either reading's possible.

Similar Threads

  1. Can someone see whats wrong with my code, please?
    By DJBENZ10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 11th, 2012, 08:54 PM
  2. Whats wrong with my code??
    By mozza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2012, 10:37 AM
  3. Whats wrong with my code?
    By Bryan29 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 5th, 2011, 09:12 AM
  4. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  5. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:59 PM