WalidKhan Posted November 16, 2023 Posted November 16, 2023 (edited) 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 November 16, 2023 by Phi for All commercial link removed by moderator
Sensei Posted November 25, 2023 Posted November 25, 2023 (edited) .. 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 November 25, 2023 by Sensei
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now