Sunday, 1 September 2013

Java Generics difference between wildcard and generic parameter type

Java Generics difference between wildcard and generic parameter type

Could someone explain to me why the third method doesnt compile?
public class Test{
public <K> void test(List<K> list){ //Compiles
K n = list.get(0);
}
public void test(List<? extends Number> list){ //Compiles
Number n = list.get(0);
}
public <K> void test(List<K extends Number> list){ //Doesn't compile !!
Number n = list.get(0);
}
}

Replacing/removing a line in a text file in java

Replacing/removing a line in a text file in java

public void removeLine() {
try {
File dir = new File("chars");
if(dir.exists()) {
String read;
File files[] = dir.listFiles();
for (int j = 0; j < files.length; j++) {
File loaded = files[j];
if (loaded.getName().endsWith(".txt")) {
Scanner s = new Scanner (loaded);
while (s.hasNextLine()) {
read = s.nextLine();
if (read.contains("char-15")) {
read.replace(read, "");
System.out.println(loaded.getName() +" -
Data: "+read);
break;
}
}
}
}
}
} catch (Exception e) {
}
}
What this should do is replace each line that contains "char-15", with an
empty String.
When I run this though, it doesn't delete the line in all the files. I
can't do this manually as there are well over 5000 files.
How can I make it delete this specific line in all of the files?

Applying background color based on scrolling content

Applying background color based on scrolling content

Here is my JsFiddle
I want to apply background-color change property to circle when the window
slides. Like in the beginning only first circle will have
background-color. and when the images slides to second screen the second
circle will have only color.
Can anybody guide me how to achieve that.
Jquery:
$(document).ready(function () {
setInterval(function () {
var A = $('.gallery').scrollLeft();
if (A < 993) {
$('.gallery').animate({
scrollLeft: '+=331px'
}, 300);
}
if (A >= 993) {
$('.gallery').delay(400).animate({
scrollLeft: 0
}, 300);
}
}, 3000);
});

Saturday, 31 August 2013

PHP using foreach to insert multiple rows not working

PHP using foreach to insert multiple rows not working

I'm trying to check if a row exists for a specific transaction and when a
new one is submitted it inserts, however, it's only inserting the first
one and not the next 5 that are there too.
Code:
foreach($transactions as $transaction) {
$check_if_transaction_exists =
$db->query("SELECT * FROM
`transactions`")->fetchColumn();
if($check_if_transaction_exists == 0) {
$insert_transaction =
$db->prepare("INSERT INTO
`transactions`
(`deposit_address`,
`amount`,
`timereceived`,
`txid`,
`category`)
VALUES
(:deposit_address,
:amount,
:timereceived,
:txid,
:category)");
$insert_transaction->execute(array(
':deposit_address' =>
$_SESSION['deposit_address'],
':amount' =>
$transaction['amount'],
':timereceived' =>
$transaction['timereceived'],
':txid' =>
$transaction['txid'],
':category' =>
$transaction['category']
));
}
echo '<br />';
echo '<br />' . $transaction['address'];
echo '<br />' . $transaction['amount'];
echo '<br />' . $transaction['confirmations'];
date_default_timezone_set('America/Los_Angeles');
echo '<br />'; echo date("F j, Y, h:i A,",
$transaction['timereceived']);
}
The bottom bit of code returns 6 of the designated elements (address,
amount, confs, etc. ) but only inserts the very first one into the
database. How can I make it insert all of them?

jQuery hide box issue

jQuery hide box issue

I'm using jQuery to hide moving div when click on i'm using this code :
$("#image").click(function () {
$("#image").hide();
});
$(document).mousemove(function(e){
$("#image").css({left:e.pageX, top:e.pageY});
});
well the box move but even when i click on it it doesn't disapear!

Modify outside variable from within AJAX function?

Modify outside variable from within AJAX function?

I use a AJAX request to get a numerical value from the database. The AJAX
function is inside another function that should return the value of the
AJAX request. However because the return value from the AJAX request is a
local variable inside the xmlhttp.onreadystatechange function it doesn't
change the "higher level" temp_return of the return_count function. I
can't have the "lower" function return the value and assign it to a
variable because it's already defined to xmlhttp.onreadystatechange... How
can I change this so that the return_count function will return the
correct value instead of 42 (predefined for testing purposes)?
function return_count(ajax_userid,ajax_date,ajax_KT,ajax_KS)
{
var temp_return = 42;
xmlhttp.onreadystatechange =
function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
temp_return = xmlhttp.responseText;
}
}
xmlhttp.open("GET",
"count.php?userid="+ajax_userid+"&date="+ajax_date+"&KT="+ajax_KT+"&KS="+ajax_KS,
true);
xmlhttp.send();
return temp_return;
}

Image handling in Rails

Image handling in Rails

I want to implement profile images to my Rails app, but I'm not quite sure
what goes in to the whole proces to enable a user to upload and manage her
images. What would be the most efficient way to go about doing this 2013?
I'd prefer to do as much of the heavy-lifting on the client side as possible.
What can I expect from libraries such as jQuery File Upload and Plupload,
can everyting (selecting, uploading, previewing, resisizing/cropping,
storing) be handled with those or do I need heavy backend solutions
(Carrierwave, Paperclick etc) as well?
If some code or libraries would be given as examples of what does what and
where, generally speaking I prefer task-specific libraries rather than
all-in-one solutions.
I'm expecting the users to have modern browsers (FF20+, Chrome20+, IE9+,
Safari5+).