Code that prints random words at table columns


Code that prints random words at table columns



code that should print cs randomly at any four different columns as the picture



Android



Androids



Code that does that it should print cs randomly at any four columns


public static void main(String args) throws IOException
{

BufferedWriter bw = null;
FileWriter fw = null;

try
{
fw = new FileWriter("F:\Android.html");
bw = new BufferedWriter(fw);

String Word = "Android";

String Words = new String[4];
Words[0] = "CS";
Words[1] = "";
Words[2] = "";
Words[3] = "";

List<String> Android = Arrays.asList(Words);
Collections.shuffle(Android);

bw.write("<Table align ='center' border = '4'>");
bw.write("<TR>");

bw.write("<TD width='45'>");
bw.write(Android.toString());
bw.write("</TD>");

bw.write("<TD width='45'>");
bw.write(Android.toString());
bw.write("</TD>");

bw.write("<TD width='45'>");
bw.write(Android.toString());
bw.write("</TD>");

bw.write("<TD width='45'>");
bw.write(Android.toString());
bw.write("</TD>");

bw.write("<TD width='45'>");
bw.write(Android.toString());
bw.write("</TD>");

bw.write("</TR>");
bw.write("<Table>");
}



the code prints



enter image description here



a few configuration changes should sort that





Looks like you forgot to ask a question?
– Pradeep Simha
Jun 30 at 16:44





the cuestion is that the result espected is as earlier picture but is as later picture
– jvm_code
Jun 30 at 17:25





2 Answers
2



try this one


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Snippet {
public static void main(String... args) {
String filePath = "E:\Android.html";
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath))) {
String Word = "Android";
String Words = new String[4];
Words[0] = "CS";
Words[1] = "";
Words[2] = "";
Words[3] = "";
List<String> Android = Arrays.asList(Words);
Collections.shuffle(Android);
bw.write("<Table align ='center' border = '4'>");
bw.write("<TR>");
bw.newLine();
for (String str : Android) {
bw.write("<TD width='45'>");
bw.write(str);
bw.write("</TD>");
bw.newLine();
}
bw.write("</TR>");
bw.write("<Table>");
} catch (Exception e) {
e.printStackTrace();
}
}
}



Output



Run 1



Run 1



Run 2



Run 2



Try replacing the four repeated blocks of:


bw.write("<TD width='45'>");
bw.write(Android.toString());
bw.write("</TD>");



with:


for(String cell: Android) {
bw.write("<TD width='45'>");
bw.write(cell);
bw.write("</TD>");
}



Your problem was writing the entire list to each cell, rather than just one element per cell.



As an aside, I would suggest choosing a more descriptive name than Android for your list of strings, and the convention in java is to begin variable names with lower case letters. (Uppercase names are typically reserved for classes.)


Android





that was good, another way for table creation
– jvm_code
Jul 1 at 17:17






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C++ thread error: no type named ‘type’ MINGW

Decreasing a static image progress bar horizontally in SDL

How to input without newline? (Python)