Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Code that writes radio buttons, with css the alignment changes on button click and four buttons !are aligned straight would want four straight aligned center radio buttons

public class ScanForWordsHtml {
    private static final Scanner INPUT = new Scanner(System.in);
    private static final String FILENAME = "F:/Android.html";

    public static void main(String[] args) throws FileNotFoundException {
        try (PrintStream output = new PrintStream(FILENAME)) {
            output.print("<html>");            
            output.print("<head>");            
                       
            output.print("<Style type = text/css>"); 
            output.print(".right_aligned label {"); 
            output.print("float: left;"); 
            output.print("clear: left;"); 
            output.print("padding-right: 1em;"); 
            output.print("text-align: right;");               	        	        	         
            output.print("}");     
            output.print("table.center {");    
            output.print("margin-left:auto; ");    
            output.print("margin-right:auto;");             
            output.print("}");                 
      
            output.print("</Style>"); 
            
            output.print("<script src='https://code.jquery.com/jquery-1.12.4.min.js'>");
            output.print("</script>");            
            
            output.print("<script type='text/javascript'>");
            output.print("$(document).ready(function(){");
            output.print("$('#btnGetValue').click(function() {");
            output.print("var selValue = $('input[name=rbnNumber]:checked').val();");
            output.print("$('p').html('<br/>You Selected : ' + selValue + '');");
            output.print("});");
            output.print("});");
            output.print("</script>");
            
            output.print("<script>");
            output.print("function buttonClickHandler()");
            output.print("{");
            output.print("}");
            output.print("</script>");             
            
            output.print("</head>");
            output.print("<body>");            
                             
            output.print("<table class = 'center'>");  
            output.print("<TR>");
            output.print("<TD>");            
            String Frenchword = readLine("Frenchword");   
            output.print(Frenchword);   
                
            output.print("</TD>");
            output.print("</TR>");                                
            output.print("<TR>");
            output.print("<TD>");               
            readFromUser(output);
            output.print("</TD>");         
            output.print("</TR>"); 
            output.print("<TD>");
            output.print("<input type='button' id='btnGetValue' Value='Get Value'>");
            output.print("<p>and the results are </p>");
            output.print("</TD>");
            output.print("<TR>");            
            output.print("</TR>");            
            output.print("</table>"); 
            output.print("<Title>");
            output.print("String Frenchword = readLine('Frenchword')");
            output.print("</Title>");
            output.print("</body>");             
            output.print("</html>");            
            output.println('\n');                                
        }
    }

    public static void readFromUser(PrintStream output) {
        
        String wordA = readLine("Word (a)");
        String wordB = readLine("Word (b)");
        String wordC = readLine("Word (c)");
        String wordD = readLine("Word (d)");
        String answer = readLine("Correct letter");        
        
        //output.println(radio("a", Frenchword, answer));
        output.println(radio("a", wordA, answer));
        output.println(radio("b", wordB, answer));
        output.println(radio("c", wordC, answer));
        output.println(radio("d", wordD, answer));
    }

    private static String readLine(String prompt) {
        System.out.print(prompt + ": ");
        return INPUT.nextLine();
    }

    private static String radio(String letter, String word, String answer) 
    {
        
        String option = "(" + letter + ") " + word;
        String is = letter.equals(answer) ? "is" : "is not";

        return "<table class = 'center'><input type='radio' align ='center' name='rbnNumber' value='You selected" 
                + option + "which" + is + "the correct answer'/>"
                + option + "<br/>";                        
    }   
}


What I have tried:

java classes, java docs and search
Posted
Updated 28-May-18 7:01am
v4
Comments
Patrice T 27-May-18 16:39pm    
What is the question ?
four systems 28-May-18 13:03pm    
the code should write four straight aligned radio buttons with css

1 solution

Few issues:
1. Your style sheet missing }
2. Your TD contains align=center that cause your divs to hang in the middle
3. Remove the width: 50% from your style. It will fix line breaking issue;
4. Your entire HTML is messed up. But, I won't suggest anything to improve.

I am not sure about the point of generating this way.

An alternative solution is:
1. Create a static HTML file with proper indention. This way you can judge your html and css code better;
2. Add a JavaScript file in your header, as example name it: data.js
2. Read user input from console, as you have written already
3. Create and write all the inputted value as JS data in data.js as is.
 
Share this answer
 
Comments
four systems 28-May-18 13:01pm    
Ty, that was good after few changes the buttons are lined up but on button click event the buttons move towards left, changed the code with changes, cause the code uses jcuery !could create data.js. Thought about that but with pure js it was difficult to select radio buttons and print correct answer(could have been ideal solution). Did you meant that data.js would write the values entered as html
Mohibur Rashid 28-May-18 17:42pm    
Html text should be in a fixed file. Your question and answers would be in data.js file. And javascript should render thise value onload
four systems 29-May-18 11:39am    
what about the code changed it still goes towards left on button clic, ty

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900