Print Ordered Numbers in Program

hello anyone who reads this :smile:

i’m new to coding, i am trying to create things to practice coding, so i went to this page :

http://www.beginwithjava.com/java/loops/questions.html

because i saw that in processing, it is using java to write codes, so i looked at this site’s practice exercise to try to practice coding.

the first question is to print number from 1-10, i tried and failed, so i watched the solution, and pasted it in processing to see if it works, but it didn’t work - i don’t know what’s the problem…

so then i changed the codes according to my limited “knowledge” (barely can amount to claim this word) , and still doesn’t work…

i want to get through the question, but i am stuck at the first one…

i wonder if anyone can help me with solving this puzzle??

thank you in advance ^^

Hello,

There are some on-line java compilers.
This came up first in search:
https://www.tutorialspoint.com/compile_java_online.php

This is what Processing is (Wikipedia definition):

I would start here to understand the Processing environment:

Be sure to check out the tutorials.

And here:

You can print() to the console and write text() to the canvas; look in the Processing reference for these.

There are also many examples that come with the Processing PDE (IDE):

These are in File > Examples…
image

:)

3 Likes

Indeed Processing sketches become Java code just before they’re compiled when we hit the “Run” button (CTRL + R).

But notice that the default extension in Processing is “.pde” and not “.java”.

It means that its syntax is slightly diff. than Java’s, and it still needs some small behind-the-scenes changes in order to output a 100% fully compliant Java compilable code.

All the “.pde” files within a Processing’s IDE (PDE) project folder are concatenated as 1 file.

Then the PDE’s preprocessor transpiles that 1 single “.pde” to a “.java” file, wrapping up our whole code as 1 class that extends PApplet:
Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html

This is your site link’s Java syntax answer for it:

public class PrintNumbers
{
    public static void main(String[] args)
    {
        for(int i=1; i<=10; i++)
        {
            System.out.println(i);
        }
    }
}

In order to convert that Java syntax code to Processing’s we need to pay attention to some details of it.

The code we actually want is just this: for (int i = 1; i <= 10; i++) System.out.println(i);

That code is inside a method called main(), which in turn is wrapped up inside a class called PrintNumbers.

The method main() is special to Java b/c it acts as the entry-point where the code starts.

However, Processing’s preprocessor creates its own custom main() behind-the-scenes.

Therefore it’s highly unrecommended to attempt creating our own main()!

As I’ve mentioned before, all “.pde” files end up wrapped up as 1 PApplet subclass; so we don’t need PrintNumbers either!

Another cool thing is that Processing’s already got a function called println():

Even though System.out.println() still works in Processing, there’s no point to type in such boilerplate when we can go w/ just println()!

Now, after removing the class PrintNumbers and its main() method, all that’s left is:

for (int i = 1; i <= 10; ++i)  println(i);

Just copy & paste that 1-liner in Processing and it will just magically run!

That’s called immediate mode, when we don’t create any functions in our “.pde” tab file.

However, most of the time we’re gonna need to create our callback setup(), which in some way acts like Java’s main():

void setup() {
  for (int i = 1; i <= 10; println(i++));
  exit();
}

When we define Processing callbacks, it’s called interactive mode.

4 Likes

thank you sooooo much!!! i have been reading and trying some more basic things according to those reference^^

i wanted to reply as soon as i saw the replies, but i’ve decided to only reply when i figured it out!!!

and i haven’t figured anything out! because it is you guys who figured it out for me lol

i read the help from GoTooLoop, taught me a great deal about processing…!!! thank you GoToLoop… thank you thank you!!!

i tried running the codes for about 100 times, restarted the program… wondering what’s wrong lol , then i realised… my problem was that i thought it would show up in the box that pops up after i clicked “Run” - truth is it’s gonna show up in the Console area lololol the answer was there but i just couldn’t see it because i had my eyes hoping it’d show up in that pop-up box…

i’m so delighted now lol i hope this is of use to somebody else that’s in the same place that i was lol

thank you so much, glv and GoToLoop…!!! if not for your replies, i don’t know how long would i have lasted at that square, which actually lasted for about 2 weeks…

now i’ll go conquer the next questions in that site!!! ^^ too delighted…!!!

thank youuuuuu : ) : ) : )

4 Likes