I want to include this java code in my blog (this is with the enclosing pre's):
<pre class="prettyprint">
public Vector<Instruction> decodeTree(Tree<String> gene) {
Vector<Instruction> ret = new Vector<Instruction>();
ret.add(decodeString(gene.getValue()));
Vector<Tree<String>> currentLayer = gene.getChildren();
Vector<Tree<String>> nextLayer = new Vector<Tree<String>>();
for(int i=0; i<currentLayer.size(); i++) {
for(Tree<String> t: currentLayer.get(i).getChildren()) {
nextLayer.add(t);
}
}
</pre>
But because it has several angle brackets, Blogger goes in and autocompletes all the inferred tags, transforming that chunk into the following:
<pre class="prettyprint">public Vector<instruction> decodeTree(Tree<string> gene) {
Vector<instruction> ret = new Vector<instruction>();
ret.add(decodeString(gene.getValue()));
Vector<tree tring="">> currentLayer = gene.getChildren();
Vector<tree tring="">> nextLayer = new Vector<tree tring="">>();
for(int i=0; i<currentlayer .size="" for="" i="" ree="" tring=""> t: currentLayer.get(i).getChildren()) {
nextLayer.add(t);
}
}
</currentlayer></tree></tree></tree></instruction></instruction></string></instruction></pre>
Which then shows up as:
public Vector decodeTree(Tree gene) {
Vector ret = new Vector();
ret.add(decodeString(gene.getValue()));
Vector> currentLayer = gene.getChildren();
Vector> nextLayer = new Vector>();
for(int i=0; i t: currentLayer.get(i).getChildren()) {
nextLayer.add(t);
}
}
Which is different from the code I'm trying to present. I think the problem originates in the html confusing my things with angle brackets with HTML tags. Is there a way I could get the parser to ignore all that? I tried changing all the angle brackets to > and < and got the following output:
public Vector<Instruction> decodeTree(Tree<String> gene) {
Vector<Instruction> ret = new Vector<Instruction>();
ret.add(decodeString(gene.getValue()));
Vector<Tree<String>> currentLayer = gene.getChildren();
Vector<Tree<String>> nextLayer = new Vector<Tree<String>>();
for(int i=0; i<currentLayer.size(); i++) {
for(Tree<String> t: currentLayer.get(i).getChildren()) {
nextLayer.add(t);
}
}
html entities are terminated with a semicolon:
<
>
Related
Here I'm trying to write a code that can write and create a JSON File for me with the data I provide like row, column and depth.
For example, I need to spawn a 10 x 10 x 10 cube. And I give this data in unity Inspector also in the code when I'm serializing it.
I tried achieving this in a for loop and writing the JSON file. But I'm not getting the output I wanted. I am expecting output where the cube locations are different and in place what happens instead is all the data or cube position in my data is the one before the number I give. that is if I gave my row, column, and depth to be 10. So my data is like x: 9, y: 9, z:9 for the whole 1000 elements.Better explained in image down below.I know I'm doing a mistake at some point just not able to figure out where. Thanks for the help in Advance
public class JSONWriter : MonoBehaviour
{
[SerializeField] int rows , columns, depth = 10;
[SerializeField] float padding;
public enum CubeType
{
white,
yellow,
blue,
red,
green
}
private readonly IReadOnlyDictionary<CubeType, Color> colors = new Dictionary<CubeType, Color>
{
{CubeType.white, Color.white},
{CubeType.yellow, Color.yellow},
{CubeType.blue, Color.blue},
{CubeType.red, Color.red},
{CubeType.green, Color.green}
};
[System.Serializable]
public class CubeData
{
public Vector3 cubePosition;
public CubeType Cube;
}
[System.Serializable]
public class CubeDataList
{
public CubeData[] cubeDatas;
}
public void outputJSON()
{
string strOutput = JsonUtility.ToJson(myCubeDataList);
File.WriteAllText(Application.dataPath + "/Resources/10x10x10.txt", strOutput);
}
//CubeData myCubeData = new CubeData();
public CubeDataList myCubeDataList = new CubeDataList();
void Start()
{
for (int x = 0; x < myCubeDataList.cubeDatas.Length; x++)
{
//Debug.Log(myCubeDataList.cubeDatas.Length);
for (int i = 0; i < depth; i++)
{
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < rows; k++)
{
myCubeDataList.cubeDatas[x].cubePosition = new Vector3(i, j, k) * padding;
//myCubeDataList.cubeDatas[x].Cube = Random.Range(CubeType, 3f);
}
}
}
}
}
}
You do not want to go through all i, j, k for each and every element x!
What you are doing is basically overwriting each and every element with the values for i=9, j=9, k=9.
Instead of an array I would rather simply use a dynamic List like
[Serializable]
public class CubeDataList
{
public List<CubeData> cubeDatas;
}
and then dynamically add them via
myCubeDataList.cubeDatas = new List<CubeData>(i * j * k);
for (int i = 0; i < depth; i++)
{
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < rows; k++)
{
var data = new CubeData();
data.cubePosition = new Vector3(i, j, k) * padding;
data.Cube = Random.Range(CubeType, 3f);
myCubeDataList.cubeDatas.Add(data);
}
}
}
Or if you really want to go with an array
for (int i = 0; i < depth; i++)
{
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < rows; k++)
{
var data = new CubeData();
myCubeDataList.cubeDatas[x].cubePosition = new Vector3(i, j, k) * padding;
myCubeDataList.cubeDatas[x].Cube = Random.Range(CubeType, 3f);
x++;
}
}
}
Though, from your previous question I know you actually do not want to fill the cube completely!
You actually only want the external shape (like a wall) and leave the cube empty on the inside.
So what you actually want would probably rather be
myCubeDataList.cubeDatas = new List<CubeData>(i * j * k);
for (int i = 0; i < depth; i++)
{
for (int j = 0; j < columns; j++)
{
for (int k = 0; k < rows; k++)
{
if(i == 0 || i == depth - 1
|| j == 0 || j == depth - 1
|| k == 0 || k == depth - 1)
{
var data = new CubeData();
data.cubePosition = new Vector3(i, j, k) * padding;
// TODO random enum (see below)
myCubeDataList.cubeDatas.Add(data);
}
}
}
}
For the random enum vlaue see e.g. this answer and do
private Random random = new Random();
and then where it says // TODO insert
var values = Enum.GetValues(typeof(Bar));
data.Cube = (CubeType)values.GetValue(random.Next(values.Length));
Below I take a list and convert it to a data table and bind it to an ASP listview control.
I'd like a function to convert it back to a List from an asp listview control and cannot figure out how to get the items? In Visual Studio debugging, the dataitems are null? It has the proper count but no values? I'd like to enumerate through all rows.
private void createLvwTable(List<string> table)
{
int index = 0;
DataTable dt = new DataTable();
foreach (string row in table)
{
string[] cells = row.Split('|');
if (index == 0) // header column
{
for (int j = 0; j < cells.Length; j++)
{
dt.Columns.Add(cells[j]);
//dt.Rows.Add();
}
}
else
{
DataRow dr = dt.NewRow();
for (int j = 0; j < cells.Length; j++)
{
dr[j] = cells[j];
}
dt.Rows.Add(dr);
}
index++;
}
lvwOutput.DataSource = dt;
lvwOutput.DataBind();
}
This is idiotic IMO so there is most likely a better way, but it appears you have to bind the data to an object during the listview creation. I couldn't find a good answer anywhere, this is a compilation of hours of searching and trying different combinations of semi-related answers.
On the html side you have to set the 'onitemdatabound' to a c# function. The code below also does not need to have the segmentation I'm going with a "|", I left that in to make it easier to read if you copy/paste my function.
I'd be happy to still read replies on how to do this better so I can learn.
html:
<asp:ListView ID="lvwOutput" runat="server" onitemdatabound="lvwOutput_ItemDataBound">
asp:
private List<string> lvwOutputItemsDataBoundToList = new List<string>();
private List<string> lvwOutputItemsDataBoundToListOriginal = new List<string>();
protected void lvwOutput_ItemDataBoundToList(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
object o = (object)dataItem.DataItem;
System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
object[] itemArray = rowView.Row.ItemArray;
string itemBound = "";
foreach(object item in itemArray)
{
itemBound += item.ToString() + "|";
}
if (itemBound.EndsWith("||"))
{
int index = itemBound.Length;
itemBound = itemBound.Remove(index - 2);
}
if (itemBound.EndsWith("|"))
{
int index = itemBound.Length;
itemBound = itemBound.Remove(index - 1);
}
lvwOutputItemsDataBoundToList.Add(itemBound);
}
ViewState["lvwOutputItemsDataBoundToList"] = lvwOutputItemsDataBoundToList;
}
private void filter()
{
lvwOutputItemsDataBoundToList = (List<string>)ViewState["lvwOutputItemsDataBoundToList"];
lvwOutputItemsDataBoundToListOriginal = lvwOutputItemsDataBoundToList;
foreach (string item in lvwOutputItemsDataBoundToList)
{
string[] itemSplit = item.Split('|');
}
}
To enumerate the ListViewItems see ListView.ListViewItemCollection Class and ListViewItem.SubItems Property.
List<string> lstItems = new List<string>():
foreach(ListViewItem itemRow in lvwOutput)
{
for (int i = 0; i < itemRow.SubItems.Count; i++)
{
lstItems.Add(itemRow.SubItems[i].Text);
}
}
I'm currently learing to program in AS3 in Flash CS6 (I have no previous programming experience), and now I'm trying to pass an argument to a parameter from a text box in the UI.
Here's what I've come up with:
btnKnapp.addEventListener(MouseEvent.CLICK, skrivUt(int(txtInput.text)));
function skrivUt(x:int)
{
for(var i:int=1; i<=5; i++)
{
var output:String = "";
for(var j:int=0; j<x; j++)
{
output += String(i);
}
trace(output);
txtOutput.appendText(output + "\n");
output = "";
}
}
So I want to execute the skrivUt function, and use the integer written in the txtInput text box as the x parameter,when I press the button btnKnapp.
This is a way that I'm using to pass parameters to EventListener.
btnKnapp.addEventListener(MouseEvent.CLICK, nextfuncWithParams(skrivUt, int(txtInput.text)));
function nextfuncWithParams(nextfunc: Function, params: int): Function{
return function(): void{
nextfunc(params);
}
}
function skrivUt(x:int): void
{
// Your logic
}
If you want to pass multiple params, use Object type like below.
var obj: Object = new Object();
obj.param1 = "Some String param";
obj.param2 = 123;
obj.param3 = false;
btnKnapp.addEventListener(MouseEvent.CLICK, nextfuncWithParams(skrivUt, obj));
function nextfuncWithParams(nextfunc: Function, params: Object): Function{
return function(): void{
nextfunc(params);
}
}
function skrivUt(params: Object): void
{
trace(params.param1);
trace(params.param2);
trace(params.param3);
}
the following code will definitely work
btnKnapp.addEventListener(MouseEvent.CLICK, skrivUt);
function skrivUt(e:MouseEvent)
{
var x:int = int(txtInput.text);
for(var i:int=1; i<=5; i++)
{
var output:String = "";
for(var j:int=0; j<x; j++)
{
output += String(i);
}
trace(output);
txtOutput.appendText(output + "\n");
output = "";
}
}
txt file containing urls that need to be open and display the contents in a 4x4 board in an html file. Having some trouble with this as I am new too html. the first board reads the words and the second is supposed to display the pictures in a randomized order.
this is the .text file
A,ant
B,bear
C,cat
D,dog
E,elephant
F,fox
G,goat
H,horse
I, iguana
J, Jaguar
K,kangaroo
L, lion
M,monkey
N,newt
O,ostrich
P,penguin
A,http://a-z-animals.com/media/animals/images/470x370/ant8.jpg
B,http://a-z-animals.com/media/animals/images/470x370/bear5.jpg
C,http://a-z-animals.com/media/animals/images/470x370/cat1.jpg
D, http://a-z-animals.com/media/animals/images/470x370/dog5.jpg
E, http://a-z-animals.com/media/animals/images/470x370/african_elephant.jpg
F, http://a-z-animals.com/media/animals/images/470x370/fox.jpg
G,goat, http://a-z-animals.com/media/animals/images/470x370/goat.jpg
H, http://a-z-animals.com/media/animals/images/470x370/horse8.jpg
I, http://www.vivanatura.org/Iguana_iguana_juv1.jpg
J, http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Jaguar.jpg/800px-Jaguar.jpg
K,http://images.nationalgeographic.com/wpf/media-live/photos/000/005/cache/gray-kangaroo_554_600x450.jpg
L, http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Lion_waiting_in_Namibia.jpg/800px-Lion_waiting_in_Namibia.jpg
M,http://upload.wikimedia.org/wikipedia/commons/d/d7/Crab-eating_Macaque_tree.jpg http://upload.wikimedia.org/wikipedia/commons/4/49/Notophthalmus_viridescensPCCA20040816-3983A.jpg
O,http://upload.wikimedia.org/wikipedia/commons/9/92/Ostriches_cape_point_cropped.jpg
P,http://upload.wikimedia.org/wikipedia/commons/b/be/Pygoscelis_papua.jpg
heres what i have so far, any hep would be much appreciated.
/*Muhammed Motala
* ics 240 prof. Jasthi
* THis program reads lines from a pre-existing text files and writes the
* contents to an HTMl file, while putting each line of code into a box forming two 4x4 board's.
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
public class TicTacTwice {
public static void main (String [] args) throws IOException {
Scanner sc = new Scanner(new File("/Users/Muhammed/Documents/Tic_Tac_Twice/tic_tac_twice2.txt"));
BufferedWriter output = null;
String myLine = null;
String [] words = new String[100];
int counter = 0;
int counter1 = 0;
boolean run = true;
do {
try {
while(sc.hasNextLine()) { //reads each line in the txt file
String product = sc.nextLine();
myLine = product;
if (product.contains(" ")) { // if the line contains a space
String[] array1 = product.split(" "); // if contains space add to array
words[counter] = array1[1];
counter++;
}
if (product.contains("http://")) {
String [] array1 = product.split(",");
words[counter] = array1[1];
counter++;
System.out.println(array1[1]);
}
else if (product.contains(",")) { // if contains comma split into separate array
String [] array1 = product.split(",");
words[counter] = array1[1];
counter++;
}
}
sc.close();
FileWriter fw = new FileWriter("/Users/Muhammed/Documents/tic_tac_twice.html");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("<html"); // html code upon writing of the file
bw.write("<head>");
bw.write("<table>");
bw.write("<h1> TICK TAC TWICE <h1>");
bw.write("<h1> Board 1 <h1>");
bw.write("</table>");
bw.write("</head>");
bw.write("<body>");
bw.write("<style>table, th, td{border:1px solid black;padding:5px}"
+ "table{border-spacing:15px}</style>");
bw.write("<table style=width:300px>");
for (int i = 0; i < 4; i++) { // array to create board1 and
// populate with the read in lines
bw.write("<tr>");
for (int j = 0; j < 4; j++) {
bw.write("<td>");
bw.write(words[counter1]);
counter1++;
bw.write("</td>");
}
bw.write("<tr>");
}
bw.write("<style>table, th, td{border:1px solid black;padding:5px}"
+ "table{border-spacing:15px}</style>");
bw.write("<table style=width:300px>");
for (int i = 0; i < 4; i++) { // creates board 2 and populates with
// words that are stored in array
bw.write("<tr>");
for (int j = 0; j < 4; j++) {
bw.write("<td>");
bw.write(words[counter1]);
bw.write("<td><img src=\"" + words[counter] +
"\" alt=\"some_text\"width=200 height=200></img>");
counter1++;
bw.write("</td>");
}
bw.write("<tr>");
}
bw.write("<h1> Board 2 <h1>");
bw.write("</body>");
bw.write("</html>");
bw.close();
run = false;
}
catch (Exception e) { // exception handler
System.out.print(e.toString());
}
}
while(run);
}
}
I'm writing a program where entered text is compared to an original (for memorization help).
Whenever a letter is wrong, I want the letter to turn red. However, when I loop it to set the textField for the wrong letters:
function checkAgainstBible(inputText:String):void
{
var outputTextL:String = ""
for(var n:Number = 0; n < inputText.length; n++)
{
var inputTextL:String = inputText
var specLetter:String = inputTextL.charAt(n);
if(inputText.charAt(n) != bibleVerse.charAt(n))
{
outputTextL = outputTextL + specLetter
outputText.text = outputTextL;
outputText.setTextFormat(red, n, n+1);
}
else
{
outputTextL = outputTextL + specLetter
outputText.text = outputTextL;
outputText.setTextFormat(green, n, n+1);
}
}
It overwrites the old one, making it so only the last letter is formatted. How do I avoid this?
EDIT I could use HTML text, but I would like to re-insert spaces afterwards, and I couldn't do that with the extra text HTML text adds to the string specLetter. /EDIT
You can set the whole text first, then set the textformat
function checkAgainstBible(inputText:String):void
{
outputText.text = inputText;
for(var n:Number = 0; n < inputText.length; n++)
{
var inputTextL:String = inputText;
var specLetter:String = inputTextL.charAt(n);
if(inputText.charAt(n) != bibleVerse.charAt(n))
{
outputText.setTextFormat(red, n, n+1);
}
else
{
outputText.setTextFormat(green, n, n+1);
}
}
}