Jump to content

Online Java Compiler Issue: Code Not Executing as Expected


WalidKhan

Recommended Posts

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
Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.