Learn Programming with Java Comments

Comments

  • I already love it!

  • Very Exciting

  • it's very interesting for me !

  • I can't wait to get started

  • hope its not hard

  • So excited! :)

  • Cant' wait to learn more about it

  • Its pretty great!

  • I can't wait to get started

  • new passion

  • Is very good

  • Your Site Is Simply Incredible.

  • I hope I get this right, it's so exciting!

  • I'm so excited

  • it will become harder

  • how do you know.

  • it does become much harder

  • im on arrays now

  • is there a way to reset your progress?

  • This bit is rather scary after you finish reading the whole thing. It is however very simple.

  • It looks very hard and difficult but just you wait and see when you apply it

  • correct me if i'm wrong
    "return" has no mathematical function. It is to like "return 5+5" to return the sum of 5+5 to the screen so when the program runs it shows the sum of 5+5 ?
    I know I'm not clear but if someone understands pls reply :P

  • return is used in a method to return a value to the code that called it (see next page). This is similar to how a mathematical function returns a value.

  • It will be easy if you read the article carefully

  • its not as complicated as it looks

  • I read quick help

  • This one was a doozy for me. I tried to change the variable name(doStuff) and create new values within the name variable I created. Anyways I must have written 3 or 4 lines, just to find out I only needed to simple lines :( .

  • I love this site. Thank you very much!

  • Brilliant way to teach! I have been searching through out the whole web for a sophisticated JAVA learn by doing site! Really glad to say that I have fortunate enough to experience learneroo.com! :)

  • I don't solve the problem...
    Would someone help me?

    import java.util.Scanner;

    public class Main {

    public static int doStuff(int a, int b){
    
    cont...
  • The quotes are definitely motivating :)

  • It is taking a very long time to show the results and it is really annoying.

  • I was more confused about the value of int a and int b then anything i couldn't work out how it had come to the results if no number have been declared. so i was trying to declare it the whole time which failed me.

  • i think this code will be efficient if we generalize both the numerator and denominator with a variable(s) and give a condition where denominator is >0

  • Its not as complicated as it first seems. Just remember how the order of operations works when you write the code.

  • Hi @Yuta, the code is simple
    int sum = a + b;
    return sum / 2;

    you are putting variable sum inside parenthesis which is wrong.

  • ahh specifying parameters is what got me as well Mezbaur

  • The simplest 1line code:

    return (a + b)/2;

  • the simplest answer is to put return a/2+b/2 for you idiots who can't figure it out for yourselves

  • Have tried several times but getting "Compilation failed" Where do I go to check the correct coding?

  • lol..seriously? made several lines and giving values to int a and b. This is fun

  • can i use public static void main (string []args) instead of public static int doStuff

  • If you look at the code on the bottom you'll see that main method there, which calls the method doStuff. (See static and main methods for more info.)

  • just put return a/2+b/2 where it says your code here
    yuta

  • oh That was so easy.

  • public static int doStuff(int a, int b){
    
        int sum = a + b;
        return (sum)/2;
    
    }
    
  • It takes more time to run the code

  • return (a+b)/2;
    Is a better answer then storing it on a variable first

  • this site is very useful to me to learn javacoding..

  • There is no value of A & B then how it's working. tell me any one

  • what is use of public and static

  • this one is hard for me
    actually have no idea what to do

  • i had to cheat

  • just return(a+b)/2;

    the average is the sum of variables / the # of variables.

  • Easy I'm liking this website.

  • Enter Integer or Decimal Answer: I expect int sum == answer

  • I love this site. Now every other book is looking like a reference sheet.

  • You say "In the following code, what number will sum equal?"
    It would be better to say "In the following code, what number will sum become?"
    Also, you should give an extra example of multi-line assignment of variables before testing student understanding

    cont...
  • This is amazing thank you so much

  • Thanks to this website, I dont feel like a dumb as.. anymore!

  • so good, I like it so much!

  • Good site so far :)

  • It's easy. Forget the int x = 2 and go to x = 100 and add the y to the x.

  • I have increased my programming knowledge by 500% in the last 10 minutes.

  • In the code why it is written
    int x = 2;
    int y = 5;
    x = 100; .Can anyone tell me why data type int is not written befor x=100;

  • As mentioned on the previous page, you need to declare a variable's type the first time, but afterwards you can use it without re-mentioning what type it is.

  • Yes, but i want to learn programming. And isnt this a bit to easy to really learn programming?

  • You can start off easy. There's lot's of challenging material later if you want it!

  • ok i want that challenging stuff :-)

  • When you're ready, check out Algorithms

  • we will, but we are not ready yet :-)

  • this is just like js and c

  • This was my hardest challenge yet...How does 100 + 5 = 105 if x=2???? Computers are DU|\/|@$$3$ !!!! If X(2)=100 And X(1)=2 and Y(5) then wouldn't x+x+y =107? the only place xxy ever made sense was in Klinefelter syndrome! :p

  • Got confused here since x = 100; wasn't given any data type.

  • Great Website! Thanks.

  • This website makes a lot of sense, because a lot of people are not employed or partially employed and can't afford the high monthly costs of other training programs. Also, the questions are simple

    cont...
  • Am so great-full for the amazing challenges on this website

  • I solved this problem with first declaring sum and mul as integers. Then returning their values using modulus.

  • the question confused me, when did i have to multiply at any point???

  • @Ryan, the product of a and b means multiply a by b. Would this be clearer:

    Multiply a by b and then get the remainder from dividing that product by the sum of a and b ?

  • Yeah, thank you. The product made me think addition but i guess that would be the sum. Maybe i should have taken more math classes lol. So far this has been the best teaching program i have used

    cont...
  • Excellent tutorial thus far. It is probably hard to understand the precise syntax that is needed to pose the problems.

  • i was so confused by the wording of product. to me it processed as the same thing as sum :/

  • I read the challenge about 5 times and operated exactly what they wrote: return (a*b)%(a+b);

    I hope this helps. Wording can be tricky but it's all going back to basic math terminology.

  • The remainder of 5 % 3 could have been -1 too.

  • This one tripped me too. It can be done this way as well:
    int product = a * b;
    int sum = a + b;
    return product % sum;

  • PEMDAS is also known as BEDMAS (brackets instead of parentheses)

  • I am a math teacher so this wasnt to hard for me :-)

  • I have a question... I understand that you can just return the value of "(a * b) % (a + b);" ... But I also tried doing this one by assigning to value to a new variable, int C. I added an int

    cont...
  • i got confused, i got it when i read the question again.

  • Good way of learning..

  • that's really awsome man

  • My solutions was like ArtTric

  • I don't know where to input int product = a*b;
    Int sum = a+b;

  • I got it correct after 3 tries. I may be wrong but I surmise that '%'is to be put between code that identifies what variables you are working with such as 'sum' and really means: "divide and also give me the remainder".

  • something is wrong here because how is 5*1 divided by (5+1) have a remainder of 5?

    basic math says its .8 with a remainder of 2. so 5*1 is 5. well 5 divided by 6 equals 0.8 r2

  • I get the code, but it has a flaw because 5*1 divided by 6 does not have a remainder of 5. its 2

    because basic math says that 5/6 is 0.8 with a remainder of 2.

  • guys i did this
    //int num
    //(1*5)%(6);
    return (a*b)%(a+b)

  • This is really hard :/

  • Meho the Wormcloack finds this easy

  • This is about the properties of int...

  • Cool problem, I like how you link it to a current problem that coding can be used to explore :D Keep up the good work guys! By the way, is / performing integer division?

  • @Stanley, if both numbers are integers, / will lose the decimal place from the answer.

  • Ah, I was a bit confused but now I realise that we only want integer outputs for the Collatz Conjecture anyway. Thanks for the help :D

  • public class Main {

    public static int doStuff(int a){
        int NrQift = a % 2;
        if(a == NrQift){
            return a/2;
        }
        else{
            return 3 * a + 1;
    
    cont...
  • I figured it out already after giving a bit more thought on the "Hint" xD Ty anyway whoever was gonna reply :)

  • Pretty sure this should work...

    if (a == a % 2) {
    return a / 2;
    }
    else {
    return (a * 3) + 1;
    }

    why not?

  • @ArtTric
    That won't work because a%2 should equal 0 (meaning that there is no remainder and therefore the number is even) and not equal to a.

  • So simple...
    int c=a%2; // take remainder of the input and save it
    //in variable c.

     if(c==0){
         return a/2;   //if the remainder == 0.
    
    cont...
  • I've done some work on the Collatz Conjecture before, so I kinda knew the answer. This is simpler than what has been posted already:

    if (a % 2 != 0) { // odd
    return (a * 3) + 1; // a times 3 plus 1
    } else { // even
    return a / 2; // a divided by 2
    }

  • @mina the operation needs to be on the right side. Its just a programming rule. example :

    if (x - 2 = 0) // error

    if (x = 2) // correct

    it wont work in this example but thats just the rule.

  • Why was I wrong?

    if (a % 2 == 0) {
    return a / 2;
    }
    else {
    return (a * 3) + 1;
    }

  • Took me a while but finally figured it out. One thing that kept tripping me up was =. I understand that the equal sign in Java is used to say that an expression is true right? When is it appropriate

    cont...
  • The solution is as straight-fwd as the problem, so apply simple maths and follow syntax.

  • I want to become a hacker, so i guess i have to learn all of this first :-)

  • needs more sauce

  • How we are using doStuff method in main class ?

  • what is wrong in below mentioned code
    package basicpgm;
    import java.util.Scanner;

    public class Main {

    public static int doStuff(int a){
        return 5*3+1;
        }
    
    public static void main(String[] args) {
            int result = doStuff(a);
            System.out.println(result);
        }
    }
    
  • if ( (x & 1) == 0 ) { even... } else { odd... }
    the low bit will always be set on an odd number.

  • This is so good I love this

  • import java.util.Scanner;

    public class Main {

    public static int doStuff(int a){
        if(a%2 != 0){
            return(3*a + 1);
        else{
            return (a/2);
    
    cont...
  •     if (a%2==0)
        {
            return a/2;
    
        }
        else
        {
            return 3*a+1;
    
        }
        i dont know what is wrrong in this
    
  • I have figured out:
    import java.util.Scanner;

    public class Main {

    public static int doStuff(int a){
        a=5;
        int s=a%2;
        int b=2;
        int c=4;
    
    cont...
  • this will do it
    // this if/else block will give result for any number a
    if (a%2>0){
    return 3*a+1
    }
    else{
    return a/2;

  • ya this is really confusing

  • the only thing confusing about it is the use of the == vs the =. This concept is in most programming languages so if you program in other languages, this isn't confusing at all.
    If (you == dont){
    return str x = 'get used to it';
    }

  • this is crazy hard to me..

  • how do i remove the cheat mark my submission was the exact answer :/

  • @Jason, sorry there's no way (currently) to remove the cheat once the cheat button has been clicked.

  • yes this course is very good idea but is should be motivated by java tutorials like beginner ,intermediate,advanced

  • hey im sorry but im still stuck in this on challenge everything looks perfect but i'm still getting errors please help..

  • @Jason, you want to increase the iterator each time. For example, to print the numbers from 1 through 10, you could create a loop like this:

    for(int i=1; i<= 10; i=i+1){
       printNum(i);
    }
    

    You need to do that to go from a to b instead.

  • I love the random quotes ♥

  • why is mine not working!!!

  • @Robert, make sure to declare any variable you use, even if it's just used as a loop index.

  • I do understand the While Loop and the example about wanting to repeat something 10 times, but I have a question ( quite stupid really but I just wanna make sure even though I know what is correct xD )
    So If we have
    { int index = 1;
    while(index <= 10){
    doSmth();
    index = index + 1
    }
    }
    so now after executing this program the index will at sometime be 2 3 4 5 6 7 8 9 and in the end 10.
    Does this mean that after every time the code is executed the { int index = 1 } changes to 2 3 4 5 6 7 8 9 or 10 from the "index = index + 1" ?
    I know it does but does that appear in the "int index = ?"

    cont...
  • @Adhurim, "int index=1" is outside the while loop, so it is executed only 1 time.

  • I really was tempted to click the hint for this one but I sat there and thought about the ways to make it work. I still have trouble with the for loop but by just setting int i = a would have been so simple. Oh well I'm glad I got it to work with the while loop but I should avoid it because of the possibility of errors.

    cont...
  • I did that and although it is correct but I should think of simpler way as the model answer :D :
    public static void doStuff(int a, int b){
    int tmp = a;
    for (int i = 0 ; i< b-a+1;i++)
    {

            printNum(tmp);
            tmp= tmp+1;
    
        }
    }
    
  • Yeah this one is tough. There should be an explanation of how to include multiple indexes in the first part of the for condition. Do you just use the & and also there is no explanation on how to printout inclusive information i.e. 1-5

  • Are we supposed to solve with the while loop or the "for" loop?

  • it says error:for(int i=a;i<=b;i+1){
    not a statement ^

  • it says error:for(int i=a;i<=b;i+1){
    not a statement ^

  • you have to put: i = i+1; or you can do an abreviation, like i++;

  • This is a great course. I am more comfortable with Java now, and it has only been 30 minutes.

  • int kq=0;
        for(int i=0;kq<b;i=i+1){
            kq=a+i;
            printNum(kq);
        }
    
  • Here's the easiest solution:

    for (a=a, b=b; a <= b; a++){
    printNum(a);
    }

  • yeah i have the same thing but different variables ( i used n) and it's not working.

  • while (a <= b) {
    printNum(a++);
    }

  • import java.util.Scanner;
    public class Main {
    public static void main(String[] args){

        Scanner input = new Scanner (System.in);
    
        System.out.print("Enter 1st number: ");
    
    cont...
  • for the for loop challenge:

    for (int i=a; i<=b;i++) {
    printNum (i);
    }

    this basically assigns i as being a for the first time since a is less than b; the condition is to display including

    cont...
  • for (int i = a; i <= b; i++){
    System.out.println(i);
    }

  • just replace all the code with:
    import java.util.Scanner;
    public class Main {

    public static void doStuff(int a, int b){
    

    long x= a;
    while(x<b+1){
    System.out.print(x+" ");
    x = x + 1;
    }

    cont...
  • Can I use While loop for the problem code above?

  • The only issue I had was thinking that he wanted actual numbers, then I realized that this is a module that gets called rather than core code. Oops.

  • no comments? has no one finished this yet? its not that difficult...

  • @Mitchell, many people have solved it, just not many commented here. We don't want users to paste solutions or long code samples here, but you can link to your most recent submission instead by clicking on "Load Link" below the comment form.

  • Will the answer that was given run forever since the condition is less than or equal to a.?

  • Bernard, it will stop when 'i' in the for statement increments up to the same number as the 'a'.

  • int kq=1;
    for(int i=a;i>0;i=i-1){
    kq=kq*i;
    }
    return kq;

  • You can solve this with recursion.
    public static int doStuff(int a){
    if (a==0 || a==1) return 1;
    return doStuff(a-1)*a;
    }

  • I just gave 2 as the initial value of the variable i so I think my code will just jump the first useless iteration: 1*1.
    my code

  • import java.util.Scanner;
    public class Factorial {
    public static void main(String[] args){

        Scanner input = new Scanner (System.in);
    
        System.out.print("Enter a number to calculate: ");
    
    cont...
  • int b = a;
    while(a!=1)
    {
    b = b*(a-1);
    a -= 1;
    }
    return b;

  • why don't we get infinate free lessions!?!?!?this is the only good site i have found )=
    )=
    )=

  • Here's a coupon that gives you membership for $2 for the first month: LEARNCODING99
    Enter it at learneroo.com/get-membership before checking out.
    (Membership goes to regular rate afterwards, but you can cancel if you're no longer interested.)

  • It doesn't seem to compile my answer but it runs on Eclipse just fine.
    Here is my code

    public class DataTypes {

    public static void main(String[] args) {
    
            int a = 2;
            double b = 7.5;
            double sum = a + b;
            System.out.println("The sum is" +  sum);
    
    }
    

    }

  • @William the only class that can be public is Main. So either remove 'public' or rename the class to 'Main'.

  • First getting an error while compiling about return statement missing, wheb added about incompatible types - required String, found double
    Here the code:

    cont...
  • @Honzis, you need to return a String that contains the correct message. So remove return sum and return message instead.

  • Thanks for the quick answer, got it now. :)

  • What is the code to solve this

  • @pat, I added an explanation, which you can 'cheat' to view.

  • Don't system.out.print, return.

  • @William, in this question You should implement code in doStuff() method and return the message. my code

  • @Honzis , You should return only a string type in doStuff() method. because return type of doStuff method is string, check my code :) my code

  • Or you could cast a double on the sum my code

  • Just FYI, I had originally put my string as "The sum is: " + sum. It said it was incorrect because I put the : in. So, even though it ran fine, gave the correct answer, etc., it showed incorrect

    cont...
  • import java.util.Scanner;
    public class DataTypes
    {
    public static void main(String[] args)
    {
    int a;
    double b;
    Scanner input = new Scanner(System.in);

    cont...
  • The exercise is to get you to understand how to attach a variable into a string, over complicating the output or process only clouds the issue. keep it simple like this...

    String answer = "The sum is " + (a+b);
    return answer;

  • This error was returned. What is the problem with this syntax?
    Main.java:6: error: unexpected type
    return ((a%b) = 0);
    ^
    required: variable
    found: value
    1 error

  • = is an assignment, you probability want an equality check ==

  • I didn't finish reading the whole lesson but what if
    {
    boolean haveMoneyLeft = yourMoney > yourDebts;
    }

    What if I have a debt of 500gold and yourMoney is 500gold too
    this makes:
    {
    boolean haveMoneyLeft = false;
    }
    right ?

  • Nice this was an easy one. thanks again for the great site.

  • import java.util.Scanner;
    public class BooleanExm
    {
    public static void main(String[] args)
    {
    double a;
    double b;
    Scanner input = new Scanner(System.in);

    cont...
  • import java.util.println;
    public class Main(){
    public static void main(String[] args){
    int a = 4;
    int b = 2;
    if(a%b==0){
    System.out.println("true");
    }else{
    System.out.println("false");
    }

  • import java.util.Scanner;
    public class Main(){
    public static void main(String[] args){
    int a = 4;
    int b = 2;
    if((a%b)==0){
    System.out.println("true");
    }else{
    System.out.println("false");
    }
    }
    }

  • Keep it simple, the answer is right there.....
    boolean x;
    if(a%b ==0){
    x = true;
    }
    else{
    x=false;
    }
    return x;
    }

  • inverse way also works 'a==1 && b ==1'

  • 'a==1 && b ==1' can work too

  • @Carlos, with an overall not !, yes.

  • What is the code to solve this?

  • boolean bLarger = ( a==1 && b==1);
    return (!bLarger);
    this works

  • can be done in 1 line of code using sums
    'return ((a + b) != 2);'

  • boolean gates = a!=1 || b!=1;

  • The not so advanced one line solution:

    return( ! (a == 1 && b ==1));

  • return a!=1 || b!=1;

  • also this solution is true :
    return !(a==1 && b==1);

  • !((a == 1) && (b == 1)) also works!

  • This might be a slight cheat, but we know that a and b are both integers, meaning there are no fractions in play. Therefore, the following also works: my code

  • import java.util.Scanner;
    public class BooleanExm2
    {
    public static void main(String[] args)
    {
    int a;
    int b;
    Scanner input = new Scanner(System.in);

    cont...
  • Lol, my too-many-steps solution:

    boolean inputa = a > b || a < b;
    boolean inputb = a == 0 && b == 0;
    boolean both = inputa || inputb;
    return both;

  • import java.util.Scanner;
    public class Main(){

    public static void main(String[] args){
    int a= 4;
    int b= 1;

    boolean collect=(a==1 && b==1);
    boolean recollect=(a==1||b==1);
    System.out.println("recollect" + recollect);
    System.out.println("collect" + collect);
    }
    

    }

  • thanks guys!

  • public class Main {
    public static void doStuff(String name){
    name = name + "!";
    String hello = "Hello ";
    String message = hello + name;
    System.out.printIn(message);
    }

    How come each input doesn't print out on their own line?

  • @Ian, Sorry, the correct code is with a lowercase 'L', not an uppercase I: System.out.println. It stands for Print Line.

  • Thanks, this same issue had me stumbled for both of the Array Loop Practices too.

  • You can complete the task with only one line: System.out.printIn([Some Stuff]);

  • I agree with Cliff, that is how I did it:
    Hint:
    string + variable + char

  • nice article

  • It's just as simple as forgetting the exclamation point to be incorrect.

  • I got all the results correct, and it says "Incorrect". Why ?

  • public static void doStuff(String name){
    name = "Jim!";
    String hi1 = "Hello ";
    String hey1 = hi1 + name;
    System.out.println(hey1);
    name = "Sarah!";

    cont...
  • I don't quite understand this, can someone help me by explaining why
    when I do
    name = "Jim";
    System.out.println("Hello " + name + '!');

    it prints four
    Hello Jim
    Hello Jim
    Hello Jim
    Hello Jim

    I don't understand.

  • @AdhurimEsati, you are given the the String name each time as a parameter, you just need to print that.

  • another simple solution :
    System.out.println ("Hello "+name+"!");

  • import java.util.Scanner;
    public class Main {
    public static void main (String[] args){

        Scanner input= new Scanner (System.in);
        System.out.print ("Enter any Name: ");
        String name=input.nextLine();
        System.out.println ("Hello " + name + "!");
    }
    

    }

  • import java.util.Scanner;
    public class Main(){

    public static void main(String[] args){
    String name="Nisha";
    System.out.print("Hello" + name);
    }
    }

  • How many possible solutions are there to this challenge?

  • There are many possible ways, but one way is particularly straightforward.

  • This code fragment also works:
    for(int x = 1; x <= b; x++)

  • yo keesh can anyone post the answer...

  • import java.util.Scanner;
    public class PrintLoop
    {
    public static void main(String[] args)
    {
    int a;
    int b;
    Scanner input = new Scanner(System.in);

    cont...
  • for(int c = b; c > 0; c = c - 1)

  • import java.util.Scanner;

    public class Loop1{
    public static void main(String args[]){
    Scanner input1 = new Scanner(System.in);
    System.out.println("Enter First number !");

    cont...
  • import java.util.Scanner;
    public class Main(){
    public static void main(String[] args){
    int a=2;
    int b=4;
    for(int i=1;i<=b;i=i+1){
    System.out.print(a);
    }
    }
    }

  • I got the answer in the end, but my first attempt was:

    int CC = ar[ar.length-1];
    int VV = ar[0];
    return CC + VV;

  • int[] fl = new int[2];
    fl[0] = ar[0];
    fl[1] = ar[ar.length - 1];
    return fl;

  • Thanks for the site I am really learning from it.
    One thing I'd like to be able to do is practice typing things out as I'm following along with your explanations. I think it would help reinforce

    cont...
  • @Robin, if you just want to take notes I think it would make sense to do in your own note-taking program, though we can consider adding such a feature. Note that if you click on "split-screen

    cont...
  • Just a funny quote here... On the challenge Second-to-Last i scratched my head for two days trying to think how to declare the variable in a single sentence the reason was because i was understanding: from the second (meaning starting at index 1) to the last, rather than actually thinking about penultimate! Frustrated not being able to declare the variable i decided that i was not understanding the exercise, clear enough google explained me second-to-last=penultimate... OMG LOL

    cont...
  • What does "Second to last" mean?

  • HOLY MOLY. 2D ARRAYS MADE MY SUDOKU SOLVER EASIER!

  • And here is the answer for the challenge:

    int [] ars = {ar [0], ar [ar.length -1]};
    return ars;

  • these are very good

  • If you get a number from an array, and do not know what slot its in. Is there a code to retrieve the slot?
    Example;
    I found the largest number in the array and want to know what slot that is in.

  • I don't understand why my challenges are wrong:
    1. int n = numbers[6];
    2. boolean [] frank= new boolean [6];

  • I had the write awnser but it still does not work, what is wrong in this:

    int[] ar2 = new int[2];
    ar2[0] = ar[0];
    ar2[1] = ar[ar.lenght - 1];
    return ar2;

  • sorry i solved it lenght mus be length pfffffffffffff :-)

  • Is the free memebership limit forever? Doesn't it restore after certain time?

  • It doesn't currently restore since it's considered a free trial.

  • Or declare regular variables:

    int a = ar[0];
    int b = ar[ar.length -1];
    int[] az = {a,b};

    return az;

  • Second-to-last means, the second element in the array starting counting from the last index.

  • import java.util.Scanner;
    public class Main(){
    public static void main(String[] args){
    int n;
    int[] numbers= new int[8];
    int[] numbers={2,3,4,5,6,7,8,9};
    for(int i=0;i<8;i=i+1){
    System.out.print(numbers[i]);
    if(i==6){
    n=numbers[i]
    System.out.print(n);
    }
    }
    }
    }

  • Do it easy guys ...
    int[] res = {ar[0], ar[ar.length -1]};
    return res;

  • Hint 2: remember to print the empty char for both if and else :)

  • why am i getting errors in the boilerplate code :\

  • forgot the curly brackets haha :P

  • if(numbers[i] > largest){
    largest = numbers[i];

    }

    Shouldn't be numbers[i] < largest ?

  • How to print on one line everything, I am having issues with that.

  • Ehh, just use System.out.print(); instead of System.out.println(""); You didn't mention it earlier :(

  • //What´s wrong with my Code??
    //PLZ help I don´t get any further...
    //blessed
    import java.util.*;
    class Main{
    static void doStuff(int[] ar){
    for(int i=0; i<=ar.length;i++){

    cont...
  • @Andreas, we will be running a membership option for people to get help with their errors. Meanwhile, here's some tips: You don't need to create a new array, just print the items in the given array. To print "no3", just type System.out.print("no3");

  • How should I do?
    when i run this code, the compiler return the error message:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

    cont...
  • Someone please post the answer so I can see what I am doing wrong. :/

  • @jordan, I would suggest starting by printing the array, and then move on to modifying it for multiples of 3. The code at the top of the page shows how to print an array, though you'll want print instead of println.

  • package Testpac;

    import java.util.Scanner;

    public class Maon_Test
    {

    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
    
    cont...
  • CORRECT:

    for(int i = 0; i < ar.length; i = i+1)
    {

    if (ar[i]%3==0){
        System.out.print("no3");
        } else{
            System.out.print(" "+ar[i]);
        }
    

    }

  • Does anyone have any idea what boilerplate code means?

  • @Prashant, in these challenges, it's just standard code that is included to process the input for you. I.e. it sets up the variables or arrays and passes them to the method doStuff().

  • another simple solution using for each :
    for (int element : ar)
    {
    if (element%3 == 0) System.out.print("no3 ");
    else System.out.print(element+" ");
    }

  • I'm just wondering whether or not you have a guide for using streams?

  • Sorry these tutorials are focused on the basics of Java for beginners, so we don't cover that.

  • There is probably better ways but I worked out the largest number first.

  • static void doStuff(int[] ar){
    //your code here
    int smallest=0;
    for(int i=0;i<ar.length;i++){
    if(ar[i]<smallest){
    smallest=ar[i];
    }
    }
    return System.out.print(smallest);

  • Hello! Don't understand the error...any help would be apreciated!

  • Can you help me understand what wrong I have done:
    static void doStuff(int[] ar){
    //your code here
    int smallest=ar[0];
    for(int i=0;i<ar.length;i++){
    if(ar[i] < smallest){
    smallest =ar[i];
    }

        }
        System.out.print(smallest);
    }
    
  • @mohammad, it looks like your print statement is outside the method, which doesn't work in Java. move it up to before the } so it runs when the for-loop is finsihed.

  • I did as you said... the code work in eclipse but not in above editor...

  • @mohammad, it looks like there are some other errors in your code also. See if you can modify the code from the previous page to find the smallest number instead of the largest.

  • i cant understand what its wrong here, i plug the code of the last page and modify and the output still doesnt make any sense.
    'static void doStuff(int[] ar){

    cont...
  • i found the answer. There is a mistake in the boilerplate code, under 'doStuff(ar);' you have to put the line 'System.out.println("");'. Then the code should work.

    cont...
  • @Peter, thanks. I mentioned in the question to print each number on its own line, so that if you use System.out.println (with the ln for line) it works, and you don't need to edit the boilerplate

    cont...
  • int smallest = ar[0];
    for(int i=0; i < ar.length; i = i +1){

            if(ar[i] < smallest){
                smallest = ar[i];
              }
    
        }
    System.out.println(smallest);
    
  • Small suggestion (however this may only be me), after reading the instructions it says "(Print each number on its own line.)", i think this is misleading or requires further clarification.

    Because first it is said to print the smallest number but then print each number...

  • int valor = ar[0];
    for(int i = 0; i<= ar.length - 1; i = i + 1){
    if (valor <= ar[i]){
    }
    else {
    valor = ar[i];
    }
    }

    cont...
  • Hello, I keep getting the correct outputs, but with .0 on the end. What am I doing wrong?
    import java.util.*;
    class Main{
    static void doStuff(int[] ar){

    cont...
  • @TyB, use ints instead of doubles.

  • Thank you!

  • private static void doStuff(int[] ar)
    {
        Arrays.sort(ar);
        System.out.println(ar[0]);
    }
    
  • ' static void doStuff(int[] ar){
    int small=ar[0];
    for (int i=0; iar[i]){
    small=ar[i];
    }
    }
    System.out.print(small);
    }'

  • I used this strategy: static void doStuff(int[] ar){
    int smallest = 0;
    int teller = 0;
    for(int i = 0; i < ar.length; i = i+1){
    if(teller == 0){

    cont...
  • im getting errors like
    Main.java:12: error: ';' expected
    public static void main(String[] args) {

  • what does this mean?
    cannot return a value from method whose result type is void
    return smallest;

  • another solution using for each:
    int tmp = ar[0];
    for (int element : ar)
    {
    if (element < tmp ) tmp = element;
    }

        System.out.println (tmp);
    
  • ugghh had to cheat on this one ....will do better next time

  • thisis good!

  • happy to finish and continue

  • Am so glad i found this site.. good work. thanks a lot.

  • One of the best interactive tutorials i have seen until now.Good job!

  • This was truly a great intro to Java programming. The challenges were engaging and rewarding, and the material conceptually explained in a way that a beginner like myself could grasp. It's a step forward for learning.

  • Yay! Basics are cleared now. This was VERY basic.

  • Pretty neat! I was looking for something my son could try and this seems like a good possibility for him to try. Thanks!

  • Well that was fast

  • Pretty neat site. Love it!

  • I like this quick reference!

  • REally Cool!

  • what next?

  • @bhavana, please see the previous node, whats next

Contact Us
Sign in or email us at [email protected]