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

• September 16th, 2012, 08:15 PM
Rstuart970
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);

}
}

}
• September 16th, 2012, 08:21 PM
pbrockway2
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W
Code :

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

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

Code :

```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.
• September 16th, 2012, 08:23 PM
curmudgeon
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:
Code :

```if (foo == 3); bar;```

but rather:

Code :

```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:

Code :

```if (foo == 3) { bar; }```
• September 16th, 2012, 08:54 PM
Rstuart970
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

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:

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);   } }   }```

also i believe i posted this in wrong forum did i?
• September 16th, 2012, 08:55 PM
Rstuart970
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.
• September 16th, 2012, 08:58 PM
pbrockway2
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W
Quote:

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.
• September 16th, 2012, 09:03 PM
pbrockway2
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W
Quote:

Originally Posted by Rstuart970
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)

Quote:

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.
• September 16th, 2012, 09:04 PM
Rstuart970
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W

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

and I am missing my 1 and i am gettin count to 51 instead of 50.
• September 16th, 2012, 09:05 PM
Rstuart970
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?
• September 16th, 2012, 09:08 PM
pbrockway2
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.
• September 16th, 2012, 09:10 PM
pbrockway2
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W
Quote:

Originally Posted by Rstuart970
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.
• September 16th, 2012, 09:27 PM
Rstuart970
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:

Code :

```  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); } }   }```
• September 16th, 2012, 09:50 PM
Rstuart970
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W
I got it worked out for the 1-50
• September 16th, 2012, 09:59 PM
Rstuart970
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
• September 16th, 2012, 10:17 PM
pbrockway2
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W
Quote:

Originally Posted by Rstuart970
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.
• September 16th, 2012, 10:48 PM
jps
Re: Problem getting code to print string after numbersn that qualkify for it. WHATS W
Quote:

Originally Posted by Rstuart970
... print on the same line as the numbers...

See System.out.print vs System.out.println
• September 16th, 2012, 11:59 PM
pbrockway2
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.