Java Loops Comments
Comments

It can be solved with either kind of loop. This question may be too hard, so I will add some more explanation (and a cheat option).

This is what I did, I don't understand the setting of variable to 1, I set mine to 2 and it worked.
public static int doStuff(int a, int b){
int product=a;
for(int i=2;i<=b;i=i+1){product=product*a;}
return product;
} 
I changed it, and it still works, but i think this is more correct and will have less errors with certain numbers. public static int doStuff(int a, int b){
int product=1;
for(int i=1;i<=b;i=i+1){product=product*a;}
return product; 
James, I changed the explanation to just show the code. My mention of setting the variable to 1 would have been relevant if I had cases where the exponent was 0, but I decided to leave them out. This means your first solution would also work.

(complete noob running through ) I cant figure this out :(. Can someone please explain what is happening with the Zonko problem...

@moses, there are now 3 challenges which all follow the same idea...

Anyone having problems, try thinking about it as that a needs to be multiplied by a, b amount of times.

This was a tough one. I originally put the following code, which was incorrect:
int solution = 0;
for (int i=0; i<=b; i++)
{
solution += (a * a);
}
return solution; 
The best thing to do is to take physical notes of the previous lesson and come back to here. My notes really helped.

Thanks much from learneroo for helping noobs like myself to learn a bit more. My 25 cents for this exercise even though i may be going to ahead of time is that it would have been useful to know
that the return must go out of the for loop and the reason.. Or was this explanation on an old lesson o_O? Anyway cheers yall!

This is the easiest way
<<int Solution = (int) Math.pow(a, b);
return Solution; 
int solution = 1; for(int i=1; i <= b; i =i+1){ solution = a * solution; } return solution;

I'm stuck on the first one. The loop stops when the condition is no longer true. If I do a+b>i; . Sum=sum+1 aka a=a+1; will run until the condition is no longer true. However I keep getting incorrect. Appreciate the advice thanks.

@learneroo can I get some help in reference to my comment. Thank you.

a+b>i
does not work as the condition since it doesn't look ati
. Try it out with an example, e.g. where a is 3 and b is 5. Also, the goal is to not add numbers greater than 1 together. How many times should you add 1 toa
to reach a+b? 
I am getting runtime error everytime System.out.println(); is being executed.

@David, your code looks like it runs ok. How would you print the next different multiple of
a
each time, when you have an indexi
that increases each time? 
Hi, thanks for the answer. The runtime error was showing up before I clicked reset, seems it cached some error? Anyway great work this website.

Can you tell me what I've done wrong in the code below:
For (int index=a, index=<b, int index = index + 1){
a*i=answer;}
System.out.println(answer); 
To fix the syntax errors, replace
=<
with<=
and remove the secondint
since you've already declaredindex
as an int in the beginning. You can click on the buttons on the side for help with syntax. 
if someone is looking for some other solution here is mine and it includes 1 if:
public static void doStuff(int a, int b){ for (int i=a;i<=a*b;i=i+1){ if (i%a==0){ System.out.print(i + " "); } } }

CORRECT:
public class Main {
public static void doStuff(int a, int b){ for(int i=1; i<=b ; i=i+1){ System.out.print(" "+ a*i);

I am constantly getting the following runtime errors:
Main.java:13: error: class, interface, or enum expected
public static void main(String[] args) {^
Main.java:16: error: class, interface, or enum expected
int n = in.nextInt();
^
Main.java:17: error: class, interface, or enum expected
for(int i=0; i<n; i++){
^
Main.java:17: error: class, interface, or enum expected
for(int i=0; i<n; i++){
^
Main.java:17: error: class, interface, or enum expected
for(int i=0; i<n; i++){
^
Main.java:19: error: class, interface, or enum expected
int b= in.nextInt();
^
Main.java:20: error: class, interface, or enum expected
doStuff(a, b);
^
Main.java:21: error: class, interface, or enum expected
System.out.println("");
^
Main.java:22: error: class, interface, or enum expected
}
^
9 errors 
@Joshua, careful with
{}
. Reset the code and put back code in the method body. 
What development app should I use to try this on my own? NetBeans? I've heard of that one...

In the next module, we suggest beginners use BlueJ to get started. Afterwards, you can use any of the main IDEs (Netbeans, Eclipse or IntelliJ)

Why do we have to put the print statement for the space on a separate line? I had no luck with (m, ' ')

Its reading my < symbol as a > symbol

If anybody needs help, take a look at this solution using the bubble sort algorithm.

hmmm cant help it, no idea ..is there easier way than "this solution"?

@calo, this problem can be solved much in a much simpler manner. See the hint that I added to the problem.

I got there in the end! This is the link to my answer: http://www.learneroo.com/user_answers/0675578220

@Victoria, well done! When you learn how to create your own methods, you'll be able to improve it further to avoid code duplication.

I think that solutions given on links above are far from perfection, since they change the data of the array

@Ksenia, that's OK in this challenge. Another approach would be to just track the 2 smallest numbers in the array as you go through it..

If you use the way shown in the Hint, be sure to declare your "secondsmallest" variable AFTER changing the smaller variable to a bigger number. Otherwise inputs 1 and 3 wont work!
Btw, thanks to @Victoria Holland, I understood the hint trough her example. 
I get the three numerical outputs green but it still says incorrect ("Your Output" is in red). Is it running some other checks that aren't being announced?

@Robin, yes, it often runs on other cases and only shows the overall result for them.

I finally got it! Though I feel like I cheated a bit by setting my 2ndsmallest variable to an unreasonably high number to begin with, rather than setting it to a value drawn from the array itself. It's here: http://www.learneroo.com/user_answers/8850767557
(P.S. Please tell me that Java has a way to automatically return the secondsmallest number from an array. I'd hate to actually have to use this sort of stuff in production code.)

Setting it to a large number is fine, though you could have also selected a number from the array. Java doesn't have any builtin way to do it, though maybe it could be done a little cleaner.
See also the featured submissions for this challenge.

why does not this work?
int smallest = ar[0];
int secondSmallest = ar[0];for(int i=0; i<ar.length; i++){ if(ar[i] <= smallest){ secondSmallest=smallest; smallest=ar[i]; } } System.out.print(secondSmallest); }

Here is the solution for the unpatient ones:
static void doStuff(int[] ar){
int x = Integer.MAX_VALUE;
int y = Integer.MAX_VALUE;// Loop over the array
for (int i = 0; i < ar.length; i++) { if (ar[i] < x) { y = x; x = ar[i]; } else if (ar[i] < y){ y = ar[i]; } } System.out.println(y);
}

Took me all day today and i think a little of yesterday to figure this exercise out.
//your code here
//Modified the code to comply this exercise with the input provided//however my logic is crashing on how to initiallize the //secondSmallest and how to define it if all //variables in the array are of the same value does the //variable become null? say > ar[]={8, 8, 8, 8}
Never mind to answer i guess after further lessons i'll prob get to know, this is just for those people out there that go through this exercise without landing on the dot and don't want to "cheat", the exercise is made to comply with scenario provided, meaning input given at the botton, period!
Anyway gotta thank Learneroo for helping noobs like me : )

Here's an example without "cheating" by having to pick an absurdly large number:
http://www.learneroo.com/user_answers/6675108810 
static void doStuff(int[] ar) { Arrays.sort(ar); System.out.println(ar[1]); //your code here }

@Dino Šišić : there is a better solution that doesnt require using integer.Max value (using this would make the problem trivial). Also, there must be no repeating value in the array

int smallest = ar[0]; for(int i = 1; i < ar.length; i++){ if(ar[i] < smallest){ smallest = ar[i]; } } int second = smallest;
boolean match = false; for(int i = 0; i < ar.length; i++){ if(!match){ if(ar[i] > smallest){ second = ar[i]; match = true; } } else{ if((ar[i] < second) && ar[i] > smallest){ second = ar[i]; } } } System.out.println(second);

Another solution:
http://www.learneroo.com/user_answers/1556388287 
it might not be the best solution
int small = ar[0];
int large = ar[0];
int index = 0;
int repeat = 0;while(repeat<2) { for(int i = 0; i < ar.length; i++)
{ if(small>ar[i]) { small = ar[i]; index = i; } if(large<ar[i]) { large = ar[i]; } } ar[index] = large; if(repeat<1) small = ar[0]; repeat++; } System.out.println(small);

static void doStuff(int[] ar){ //your code here int[] small = new int[2]; small[0] = ar[0]; small[1] = ar[1]; if(small[0] > small[1]){
int t = small[0]; small[0] = small[1]; small[1] = t; } for(int i = 2; i < ar.length; i++){ if(ar[i] < small[1]){ small[1] = ar[i]; if(small[0] > small[1]){ int q = small[0]; small[0] = small[1]; small[1] = q; } } } System.out.println(small[1]); }

Keep the previous code to find the smallest number, and after that code, add the exact same thing over again with a second condition stating that the cell cannot be equal to the smallest number.

Can anyone tell me why this won't work?
static void doStuff(int[] ar){ int smallest = ar[0]; for(int i=0; i<ar.length; i=i+1){ if(ar[i]<smallest){
ar[i]=smallest; } } int largest=0; for(int i=0; i<ar.length; i=i+1){ if(largest<ar[i]){ ar[i]=largest; }//your code here } smallest=largest; int second_smallest = ar[0]; for(int i=0; i<ar.length; i=i+1){ if(ar[i]<second_smallest){ ar[i]=second_smallest; } } System.out.print(second_smallest);
}

@corey
ar[i]=smallest;
will modify the value in the array, is that really what you want to do? 
Literally tried this for 5 hours without trying to use Google as help, What am i doing wrong here?
import java.util.*;
public class Main{
static void doStuff(int[] ar){int smallest = ar[0]; for(int i=0; i < ar.length; i = i+1){ if(ar[i] < smallest){ smallest = ar[i]; } } int small = ar[0]; for(int i=0; i < ar.length; i = i+1){ if(ar[i] < small && ar[i] != smallest){ small = ar[i]; } } System.out.println(small); }

@Gaetano, step through your code with the examples that are wrong to spot the error. For example, what happens when your code runs on the first case? Consider changing the smallest numbers so it doesn't cause trouble the second time...

It took me a while (from int i = one a clock; i= two o clock; i++) :) but i think i found it.
Here is my answer.
Jay Learn
Jul 23, 10:28 AMIt would have been easy if you would have mentioned whether the given problem is to be solved using 'for' or 'while' loop!