Jump to content

Online Java Compiler Issue: Code Not Executing as Expected

Featured Replies

I'm using an online Java compiler for my project, and I'm encountering unexpected behavior in the execution of my code. Here's a simplified snippet:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        for (String fruit : fruits) {
            System.out.println(fruit.length());
        }
    }
}

Despite the seemingly straightforward code, I'm not getting the expected output. What could be causing this issue with the online Java compiler, and how can I rectify it to ensure proper code execution and output?

Edited by Phi for All
commercial link removed by moderator

  • 2 weeks later...

.. try using different Java online compiler.. ?

 

I tried this:

https://www.programiz.com/java-programming/online-compiler/

And it showed:

java -cp /tmp/gCdTYn4aQ5 Main
5
66

But this 66 is simply a lack of EOL....

After changing the code to:

System.out.println(""+fruit.length());

all is fine..

java -cp /tmp/gCdTYn4aQ5 Main
5
6
6

 

ps. Report the problem to the developers who created the online compiler, along with a code snippet, and it will be fixed.

 

Edited by Sensei

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.