lundi 30 mars 2015

How to save a whole stack/list/array (some sort of multiple data structure) to a text document

I'm trying to create a rudimentary history system for a web browser I'm designing, unfortunately, I can only find ways to either store 1 web address for a history, or not at all, the fileWriter and fileOutputStream seem to limit to one single item per write, which would overwrite the previous if I tried to output one at a time.


I would appreciate it if people could either suggest ways to store an entire list of strings in an external txt doc, bonus points if you can retain an order to them


request for code:



private void loadWebPage(String userInput) {
if (stackTest == true) {
forwardStack.push(urlBox.getText());
stackTest = false;
} else {
backStack.push(urlBox.getText());
}

try {
createHistory(urlBox.getText());
// displays the content of a html link in the web window, as per
// user input
webWindow.setPage(userInput);
// sets the text in the urlbox to the user input so they can check
// at any time what page they are on
urlBox.setText(userInput);
} catch (Exception e) {
// if user enters a bad url then produce error

try {
File file = new File(userInput);

webWindow.setPage(file.toURI().toURL());
} catch (Exception e1) {
System.out.println("Error 001: Bad URL");
}
}
}

private void createHistory(String webAddress) {
JMenuItem button = new JMenuItem(webAddress);
history.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent histPress) {
loadWebPage(webAddress);
historyStack.push(webAddress);
try {
FileWriter writer = new FileWriter(histPath, false);
writer.write(historyStack);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}


}
});
}

private void recoverHistory() {
//this will read from the txt doc and create a
//history based on past uses of the browser
}


FileWriter will not store any data construct and will overwrite each value if done using string and forLoop


I hope someone knows the answer


Convert a string to a datatype base on a string that contains datatype name

I am converting a string to a datatype based on the another string that contains the name of datatype. I have two string like below:



string string_1 = "true";

string string_2 = "System.Boolean";


i need to convert the string 1 to the data type that is specified in string 2. how can i do that?(note that the string_2 can be each datatype) must i use if's for checking string_2 with any type of datatype?


What is the standard way to represent subsequent changes in a text and to work with this representation using Python?

Assume that I have some text (for example given as a string). Later I am going to "edit" this text, which means that I want to add something somewhere or remove something. In this way I will get another version of the text. However, I do not want to have two strings representing each version of the text since there are a lot of "repetitions" (similarities) between the two subsequent versions. In other words, the differences between the strings are small, so that it makes more sense just to save differences between them. For example, the first versions.



This is my first version of the texts.


The second version:



This is the first version of the text, that I want to use as an example.


I would like to save these two versions as one object (it should not necessarily be XML, I use it just as an example):



This is the <removed>my</removed> <added>first</added> version of the text<added>, that I want to use as an example</added>.


Now I want to go further. I want to save all subsequent edits as one object. In other words, I am going to have more than two versions of the text, but I would like to save them as one object such that it is easy to get a given version of the text and easy to find out what are the difference between two subsequent (or any two given) versions.


So, to summarize, my question is: What is the standard way to represent changes in a text and to work with this representation using Python.


How to split a string twice and add it to a linked list?

I tried to split a string and added it to a linked list each node in the linked list is a polynomial boundary . I tried this but it gave me a Dangling meta character exception , what was the wrong I did here ?



String s = "X^2+3x+5";
LinkedList p_list = new LinkedList();

s.toLowerCase();
s.replace("*x", "x");
s.replace("x^", "x");
s.replaceAll("--","+");
s.replaceAll("+-", "-");
s.replaceAll(" ", "");
String [] st = s.split("(?=[+-])");
String [] st2 = new String[2];

for(int i=0;i<=st.length;i++){
if(st[i].contains("x")){
st2=st[i].split("x");
if(st2[0].length()== 0 && st2[1].length()== 0){
p_list.addFirst(1,1);
}else if(st2[0].length()== 1 && st2[1].length()== 0){
p_list.addFirst(Integer.parseInt(st2[0]),0);
}
} else {
p_list.addFirst(Integer.parseInt(st2[0]),Integer.parseInt(st2[1]));
}


}


p_list.printList();

Why date formats miss matching when adding months

When i am getting current date 28-Mar-2015 formated into txtActDate then adding months to it then i am not understanding why its getting in this 28-Mar-15 format.



DateTime dateTime = DateTime.UtcNow.Date;
txtActDate.Text = dateTime.ToString("dd/MMM/yyyy");
DateTime firstDate = DateTime.ParseExact(txtActDate.Text, "dd/MMM/yyyy", null);
firstDate = firstDate.AddMonths(0);
txtAccExp.Text = firstDate.ToShortDateString();

dimanche 29 mars 2015

How to explode an array_agg

I have a data:



$data = '{123, 456, 231, 478, 123, 673}'; //This is a string. I used "array_agg" in SQL.

$value = explode("," , $data);


The problem with this is that I obtain something like this:



$value[0] = '{123';
$value[1] = '456';
.
.
$value[5] = '673}';


How can I get the exact value I want? I want to remove the '{}'. Thanks!


Dynamically handle json response String/array/object using Gson

I am getting dynamic res_data value, sometimes it is array/object/string values from server according to res_stat ,so I can't parse this response using Google gson library. Different responses are given bellow according to res_stat



{"res_stat":"S","res_data":[]}
{"res_stat":"N","res_data":”"}
{"res_stat”:"E","res_data”:{}}


How can i dynamically handle this json response using google gson library? Or any other solutions for this?