Printing array loop
Hello world,
I want to iterate through integer values and print them out using a for loop. My goal is if someone enters 20 at the keyboard, I want to print the numbers from 1-20 and replace any number that can be divided by 2 with the word "Two".
The program is working, but it prints the word "Two", and "2". I dont want it to print "2", rather I want it to be replaced by "Two". And do the same for "Seven"
Here is the program I have, any help is highly appreciated.
import java.io.DataInputStream;
publicclass Loop
{
publicstaticvoid main (String[] args)
{
System.out.print ("Enter Integer number:");
DataInputStream input =new DataInputStream (System.in);
int number = 0;
System.out.flush ();
try
{
number = Integer.valueOf (input.readLine()).intValue ();
for (int i = 1 ; i <= number ; i++)
{
if (i % 2 == 0)
System.out.println ("Two");
if (i % 7 == 0)
System.out.println ("Seven");
if ((i % 7 == 0) & (i % 2 == 0))
System.out.println ("Two Seven");
System.out.println (i);
}
}
catch (Exception exception)
{
System.out.println ("Sorry, that's not an Integer");
}
}
}
That last if statement is wrong, in that you don't need to explicitly print "two seven" (it'll be printed by the earlier if statements), and you shouldn't be printing i in the case given.
You probably want to check that i is divisible by neither 2 nor 7, and if it isn't, then print i.
Thanks very much paulcw for your swift response.
However, I want to I have removed the last if statement and its still printing "2" after the number "Two".
I just want to replace the number "2" with "Two". This is my output
Enter Integer number:12
1
Two
2
3
Two
4
5
Two
6
Seven
7
Two
8
9
Two
10
11
Two
12
Any more helps is appreciated highly :-)
Replace this:
if ((i % 7 == 0) & (i % 2 == 0))
System.out.println ("Two Seven");
System.out.println (i);
}
with this:
if (!((i % 7 == 0) || (i % 2 == 0)))
System.out.println (i);
}
place an exit after printing "Two"
import java.io.DataInputStream;
public class Loop
{
public static void main (String[] args)
{
System.out.print ("Enter Integer number:");
DataInputStream input = new DataInputStream (System.in);
int number = 0;
System.out.flush ();
try
{
number = Integer.valueOf (input.readLine()).intValue ();
for (int i = 1 ; i <= number ; i++)
{
if (i % 2 == 0){
System.out.println ("Two");
exit();}
if (i % 7 == 0)
System.out.println ("Seven");
if ((i % 7 == 0) & (i % 2 == 0))
System.out.println ("Two Seven");
System.out.println (i);
}
}
catch (Exception exception)
{
System.out.println ("Sorry, that's not an Integer");
}
}
}
sorry use continue not exit;
like :
if (i % 2 == 0) {
System.out.println ("Two");
continue;}
paulcw, you deserve the Dukes.
Thanks for the help.
Finally, what if I want to replace any number that can be divisible by 3 and 5 and replace it with "Three Five", how do I do that with the following code
if ((i % 5 == 0) & (i % 3 == 0)) {
System.out.println ("Three five");
If you look at the out put below, you will see that number fifteen is printing Three followed by Five on different lines. I want them to be on the same line as they can be divided by both numbers
Enter Integer number:15
1
2
Three
4
Five
Three
7
8
Three
Five
11
Three
13
14
Three
Five
You guys have been great so far...
Please help me again
Sorry to come in here late in the game, but I've usually done situations like this using "else if" and "else" blocks. It just seems a clean way to do this. I also almost always like to use curly brackets with my if / else statements to prevent a common bug.
for (int i = 1 ; i <= number ; i++)
{
if (i % 2 == 0)
{
System.out.print ("Two ");
}
else if (i % 7 == 0)
{
System.out.print("Seven");
}
else
{
System.out.print (i);
}
System.out.println();
}
Sorry if this is too late or totally irrelevant.
also, I've replaced the println wih print, added a space after the "two ", and added a println() at the end of the loop block so you can print out:
two seven
on one line if a number is divisible by both.
The rule seems to be that in various situations you want to print text rather than a number, and that when you print any text you do not want to print a number, but when you print text you can print other text.
I think that if you're going to make this sufficiently complicated, you might as well create a flag that represents whether you've printed text.
Then you could do something like:boolean printedText = false;
if (i % 2 == 0) {
print("two");
printedText = true;
}
if (i % 3 == 0) {
print "three"
printedText == true;
//... lots more of the same
if (!printedText) {
print i;
}
that turned into pseudo-code halfway through, but you get the idea.
nevermind, ignore my code. just use Paul's
Message was edited by:
petes1234
> nevermind, ignore my code. just use Paul's
>
>
>
> Message was edited by:
> petes1234
Am very sorry, I saw Paul's answer before yours, besides, your answer was equally helpful. Only that Paul was lightning fast in answering my request for help and he deserve a nice cold beer for that :-)
> Am very sorry, I saw Paul's answer before yours,
> besides, your answer was equally helpful. Only that
> Paul was lightning fast in answering my request for
> help and he deserve a nice cold beer for that :-)
It doesn't matter who was first as long as you learn how to do this. Beer would be nice though.
> > Am very sorry, I saw Paul's answer before yours,
> > besides, your answer was equally helpful. Only
> that
> > Paul was lightning fast in answering my request
> for
> > help and he deserve a nice cold beer for that :-)
>
> It doesn't matter who was first as long as you learn
> how to do this. Beer would be nice though.
petes1234 , don't misunderstand me, your code was spot on! I only noticed it once I implemented Paul's solution.
Of course I leant more about print and println from you, for example the println after the last print statement ensured that two and seven stayed on the same line. Now you see that you made a big impact on my learning. And thanks you with all my heart for it.
hey, don't you worry, there's enough beer to go round. :-)
if ((i % 7 == 0) & (i % 2 == 0))
System.out.println ("Two Seven");
System.out.println (i);
Your problem is solved but I just wanted to add my two cents. The problem was caused by the fact the println(i) line of code was not inside the if statement (no braces). Therefore each time around the loop the value of i was printed regardless.
Thanks everyone for your input, everything is now working as planned thanks again for your support and expertise.
Cheers guy :-)