# Homework - using 'IF' for 'For Loops'

• September 16th, 2009, 09:01 AM
Enzo
Homework - using 'IF' for 'For Loops'
Hey everyone. Slightly a beginner here but I'm doing Computer Science in high school. I use JCreator to write my codes and I just need a little help with the logic I need to use and some functions I would need to use.

First we made a code on inputting numbers using InputStreamReader and what the program had to do was create a multiplication table of numbers.

First it had to ask for which number you want to multiply by -- let's say 5

Then, it asked from which value you want to start multiplying -- let's say 2

Finally, it asked for the last number in which the program would stop multiplying -- let's say 10

basically it printed out

2*5=10
3*5=15
4*5=20
.
.
.
10*5=50

then it stopped.

What I need to do now is to create an if function which asks that once that is completed, if the user wants to continue running that program. The program will keep running unless the user gives a command for it to stop.

Also, my teacher recommended me to use methods and functions instead of doing everything in the public static void main.

here's the code

Code :

```import java.io.*;   public class ForLoop {     public static void main(String[] args) throws IOException {     InputStreamReader in=new InputStreamReader(System.in); BufferedReader strbuf=new BufferedReader(in);   System.out.println("Enter table for: "); int number=Integer.parseInt(strbuf.readLine()); System.out.println("The number you have entered is: "+number);   System.out.println("Enter first value: "); int first=Integer.parseInt(strbuf.readLine()); System.out.println("The first value you have entered is: "+first);     System.out.println("Enter last value: "); int last=Integer.parseInt(strbuf.readLine()); System.out.println("The last value you have entered is: "+last);   for(int initial=first;initial<=last;initial++) { System.out.println(initial + "X" + number + "=" + (initial * number)); }   } }```

any ideas?

Thanks!!
• September 16th, 2009, 09:44 AM
Freaky Chris
Re: Homework - using 'IF' for 'For Loops'
Firstly, welcome to Java Programming Forums. I hope we can help you out!

Code :

```import java.io.*;   public class ForLoop {     public static void main(String[] args) throws IOException {   do{ InputStreamReader in=new InputStreamReader(System.in); BufferedReader strbuf=new BufferedReader(in);   System.out.println("Enter table for: "); int number=Integer.parseInt(strbuf.readLine()); System.out.println("The number you have entered is: "+number);   System.out.println("Enter first value: "); int first=Integer.parseInt(strbuf.readLine()); System.out.println("The first value you have entered is: "+first);     System.out.println("Enter last value: "); int last=Integer.parseInt(strbuf.readLine()); System.out.println("The last value you have entered is: "+last);   for(int initial=first;initial<=last;initial++) { System.out.println(initial + "X" + number + "=" + (initial * number)); } }while(Cont()); }   public boolean Cont() throws IOException { System.out.print("Would you like to carry on (y/n): "); BufferedReader strbuf=new BufferedReader(new InputStreamReader(System.in));   if(strbuf.readLine().equalsIgnoreCase("y")) return true; return false; } }```

Regards,
Chris
• September 16th, 2009, 10:11 AM
Enzo
Re: Homework - using 'IF' for 'For Loops'
hey Chris, thanks so much for the help!

however, it keeps giving me this error

'non static method Cont cannot be referenced from a static context'

do I need to put static Cont somewhere?
• September 16th, 2009, 10:14 AM
chronoz13
Re: Homework - using 'IF' for 'For Loops'
try to put static on your method.
• September 16th, 2009, 10:47 AM
Enzo
Re: Homework - using 'IF' for 'For Loops'
ok. got it.

thanks again!

+rep
• September 16th, 2009, 10:52 AM
Freaky Chris
Re: Homework - using 'IF' for 'For Loops'
lol thanks chronoz, again I had no way of testing so. I normally miss statics lol!

Please mark as solved if thats everything!

Chris