Java Output Code Challenge
Challenge: Print Name and Age
Test your understanding of Java Output by completing a small coding challenge.
ResultError
Your code has an error. Check the output below for details.
Click Check Code to see the result here.
Compiling & running
Runtime Error
";
list.appendChild(li);
sum.className = "w3-panel w3-pale-red ch-summary";
sum.innerHTML = "Your code has a
runtime error. Fix the error and check again.";
}
// Apply deferred green-line highlighting now (same time as checklist)
if (window.ChallengeEngine && window.ChallengeEngine.applyDeferredHighlight){
window.ChallengeEngine.applyDeferredHighlight(errorLines || []);
}
if (card) card.classList.remove("ch-waiting-result");
if (window.ChallengeEngine && ChallengeEngine.setEditorDisabled) ChallengeEngine.setEditorDisabled(false);
if (typeof fix_sidemenu === "function") fix_sidemenu();
}
// Listen for hasError postMessage from Sphere Engine result iframe
window.addEventListener("message", function(e){
if (!e.data || typeof e.data.hasError === "undefined") return;
var codeIframe = document.getElementById("chRunnerIframe");
if (e.source && codeIframe && codeIframe.contentWindow === e.source){
codeHasError = !!e.data.hasError;
if (codeHasError){
showErrorUI();
} else {
hideErrorUI();
}
// Extract error line numbers (0-indexed) if available
var errorLines = [];
if (codeHasError){
// errorLine is a deduplicated array of 1-indexed integers from the server
if (Array.isArray(e.data.errorLine)){
for (var i = 0; i 0) errorLines.push(e.data.errorLine[i] - 1);
}
}
// Fallback: parse "line N" from errorText if errorLine is empty
if (!errorLines.length && typeof e.data.errorText === "string"){
var m = e.data.errorText.match(/line\s+(\d+)/gi);
if (m){
for (var j = 0; j 0) errorLines.push(num - 1);
}
}
}
}
if (waitingForResult){
waitingForResult = false;
finalizeCheck(codeHasError, errorLines);
}
}
});
function getSolution(){
var host = document.getElementById(hostId);
if (!host) return "";
var solTa = host.querySelector(".ch-solution-codewrap textarea");
return solTa ? solTa.value : "";
}
function highlightSolution(code){
if (window.ChallengeEngine && ChallengeEngine.highlightCode) return ChallengeEngine.highlightCode(code, null);
var usePrism = (typeof Prism !== "undefined" && Prism.languages);
var grammar = usePrism ? (Prism.languages[PRISM_LANG] || null) : null;
if (grammar) return Prism.highlight(code, grammar, PRISM_LANG);
return code.replace(/&/g,"&").replace(/,"<").replace(/>/g,">");
}
function encodeForSphere(text){
var t = text;
t = t.replace(/=/gi, "w3equalsign");
t = t.replace(/\+/gi, "w3plussign");
var pos = t.search(/script/i);
while (pos > 0){
t = t.substring(0, pos) + "w3" + t.substr(pos, 3) + "w3" + t.substr(pos + 3, 3) + "tag" + t.substr(pos + 6);
pos = t.search(/script/i);
}
return t;
}
function getSolutionHtmUrl(){
if (!DATA_URL) return "";
return DATA_URL.replace(/\.js$/, ".htm");
}
function restructureLayout(){
var host = document.getElementById(hostId);
if (!host) return;
var card = host.querySelector(".ch-card");
if (!card) return;
var instructions = card.querySelector(".ch-instructions");
var workarea = card.querySelector(".ch-workarea");
var editorPanel = card.querySelector(".ch-editorpanel");
var wrap = document.getElementById("chRunnerWrap");
if (!instructions || !workarea || !editorPanel || !wrap) return;
var toolbar = card.querySelector(".ch-toolbar");
var toolbarLeft = card.querySelector(".ch-toolbar-left");
if (toolbar && toolbarLeft){
var tabsDiv = document.createElement("div");
tabsDiv.className = "ch-toolbar-tabs";
tabsDiv.innerHTML =
"
" +
"
";
toolbarLeft.insertBefore(tabsDiv, toolbarLeft.firstChild);
var checkBtn = card.querySelector("[data-action='check']");
var chkLong = checkBtn ? checkBtn.querySelector(".ch-check-label-long") : null;
if (chkLong) chkLong.textContent = "Check Code";
}
var editorWrap = card.querySelector(".c-editor-wrap");
if (editorWrap){
var solView = document.createElement("div");
solView.className = "ch-solution-view";
solView.id = "chSolView";
solView.innerHTML = "
" +
"
";
editorWrap.appendChild(solView);
var solViewTa = document.getElementById("chSolViewTa");
var solViewPre = document.getElementById("chSolViewCode");
if (solViewTa && solViewPre){
solViewTa.addEventListener("scroll", function(){
solViewPre.scrollTop = solViewTa.scrollTop;
solViewPre.scrollLeft = solViewTa.scrollLeft;
});
}
}
var grid = document.createElement("div");
grid.className = "ch-editor-preview-grid";
grid.appendChild(workarea);
wrap.style.display = "block";
grid.appendChild(wrap);
var split = card.querySelector(".ch-split");
if (split) split.parentNode.insertBefore(grid, split.nextSibling);
var codeTab = card.querySelector("[data-tab='code']");
var solTab = card.querySelector("[data-tab='solution']");
var checkBtnEl = card.querySelector("[data-action='check']");
var checkLong = checkBtnEl ? checkBtnEl.querySelector(".ch-check-label-long") : null;
var checkShort = checkBtnEl ? checkBtnEl.querySelector(".ch-check-label-short") : null;
function setActiveTab(tab){
var solViewEl = document.getElementById("chSolView");
var solCodeEl = document.getElementById("chSolViewCode");
if (tab === "solution"){
solutionActive = true;
codeTab.classList.remove("active");
solTab.classList.add("active");
var sol = getSolution();
if (solCodeEl) solCodeEl.innerHTML = highlightSolution(sol);
var solTaEl = document.getElementById("chSolViewTa");
if (solTaEl) solTaEl.value = sol;
if (solViewEl) solViewEl.classList.add("is-visible");
var edWrap = card.querySelector(".c-editor-wrap");
if (edWrap) edWrap.classList.add("sol-active");
if (checkBtnEl) checkBtnEl.style.display = "none";
} else {
solutionActive = false;
solTab.classList.remove("active");
codeTab.classList.add("active");
if (solViewEl) solViewEl.classList.remove("is-visible");
var edWrap = card.querySelector(".c-editor-wrap");
if (edWrap) edWrap.classList.remove("sol-active");
if (checkBtnEl){ checkBtnEl.style.display = ""; }
if (checkLong) checkLong.textContent = "Check Code";
if (checkShort) checkShort.textContent = "Check";
}
}
if (codeTab) codeTab.addEventListener("click", function(){ setActiveTab("code"); });
if (solTab) solTab.addEventListener("click", function(){ setActiveTab("solution"); });
}
function hookCheckButton(){
if (hooked) return;
var host = document.getElementById(hostId);
if (!host) return;
var checkBtn = host.querySelector("[data-action='check']");
if (!checkBtn) return;
var card = host.querySelector(".ch-card");
checkBtn.addEventListener("click", function(){
codeHasError = false;
hideErrorUI();
waitingForResult = true;
if (card) card.classList.add("ch-waiting-result");
if (window.ChallengeEngine && ChallengeEngine.setEditorDisabled) ChallengeEngine.setEditorDisabled(true);
setTimeout(function(){
chRunCode();
if (!waitingForResult){
if (card) card.classList.remove("ch-waiting-result");
if (window.ChallengeEngine && ChallengeEngine.setEditorDisabled) ChallengeEngine.setEditorDisabled(false);
}
}, 10);
});
hooked = true;
}
var obs = new MutationObserver(function(){
if (getEditorTextarea()){
restructureLayout();
hookCheckButton();
obs.disconnect();
}
});
var hostEl = document.getElementById(hostId);
if (hostEl){
obs.observe(hostEl, {childList:true, subtree:true});
}
window.chRunCode = function(){
var ta = getEditorTextarea();
if (!ta) return;
var code = ta.value;
var starter = ta.getAttribute("data-starter") || "";
var iframe = document.getElementById("chRunnerIframe");
var loader = document.getElementById("chRunnerLoader");
var placeholder = document.getElementById("chRunnerPlaceholder");
if (!code.trim() || code === starter){
waitingForResult = false;
iframe.style.display = "none";
loader.style.display = "none";
placeholder.innerHTML = "
No changes have been made. Edit the code and try again.";
placeholder.style.display = "flex";
return;
}
placeholder.style.display = "none";
loader.style.display = "block";
iframe.style.display = "none";
document.getElementById("chRunnerCode").value = encodeForSphere(code);
var form = document.getElementById("chRunnerForm");
form.action = ACTION_URL + "?x=" + Math.random();
form.submit();
iframe.onload = function(){
loader.style.display = "none";
iframe.style.display = "block";
iframe._loaded = true;
};
};
})();