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);
}
}
Bucher
Sunday, 1 September 2013
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?
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?
save image to server from canvas
save image to server from canvas
hi i wanna take image from webcam and save it in server;i use following
code but it does not work.my code take pic and show it in mycanvas but
when i want to get image from mycanvas and send it to server ajax function
does not work.
var webcam = (function () {
var video = document.createElement('video'),
photo = document.getElementById("myCanvas");
function play() {
if (navigator.getUserMedia) {
navigator.getUserMedia({ video: true,
audio: true, toString: function () { return
"video,audio"; } }, onSuccess, onError);
} else {
changeStatus('getUserMedia is not supported
in this browser.', true);
}
}
function onSuccess(stream) {
var source;
if (window.webkitURL) {
source =
window.webkitURL.createObjectURL(stream);
} else {
source = stream;
// Opera and Firefox
}
video.autoplay = true;
video.src = source;
changeStatus('Connected.', false);
}
function onError() {
changeStatus('Please accept the getUserMedia
permissions! Refresh to try again.', true);
}
function changeStatus(msg, error) {
var status = document.getElementById('status');
status.innerHTML = msg;
status.style.color = (error) ? 'red' : 'green';
}
var status = document.getElementById()
function setupPhotoBooth() {
var takeButton =
document.getElementById('btnTakePicture');
takeButton.addEventListener('click', takePhoto,
true);
var saveButton =
document.getElementById('btnSave');
saveButton.disabled = true;
saveButton.addEventListener('click', savePhoto,
true);
}
function takePhoto() {
// set our canvas to the same size as our video
photo.width = video.width;
photo.height = video.height;
var context = photo.getContext('2d');
context.drawImage(video, 0, 0, photo.width,
photo.height);
// allow us to save
var saveButton =
document.getElementById('btnSave');
saveButton.disabled = false;
var data = photo.toDataURL("image/png");
data = data.replace("image/png", "");
document.getElementById("<%= eventdata.ClientID
%>").value = data;
}
return {
init: function () {
changeStatus('Please accept the permissions
dialog.', true);
video.width = 320;
video.height = 240;
document.body.appendChild(video);
document.body.appendChild(photo);
navigator.getUserMedia ||
(navigator.getUserMedia =
navigator.mozGetUserMedia ||
navigator.webkitGetUserMedia ||
navigator.msGetUserMedia);
play();
setupPhotoBooth();
} ()
}
})();
function UploadPic() {
// generate the image data
var Pic =
document.getElementById("myCanvas").toDataURL("image/png");
// var Pic =
document.getElementById("eventdata").toDataURL("image/png");
Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")
// Sending the image data to Server
$.ajax({
type: 'POST',
url: 'Save_Picture.aspx/UploadPic',
data: '{ "imageData" : "' + Pic + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert("Done, Picture Uploaded.");
}
});
}
hi i wanna take image from webcam and save it in server;i use following
code but it does not work.my code take pic and show it in mycanvas but
when i want to get image from mycanvas and send it to server ajax function
does not work.
var webcam = (function () {
var video = document.createElement('video'),
photo = document.getElementById("myCanvas");
function play() {
if (navigator.getUserMedia) {
navigator.getUserMedia({ video: true,
audio: true, toString: function () { return
"video,audio"; } }, onSuccess, onError);
} else {
changeStatus('getUserMedia is not supported
in this browser.', true);
}
}
function onSuccess(stream) {
var source;
if (window.webkitURL) {
source =
window.webkitURL.createObjectURL(stream);
} else {
source = stream;
// Opera and Firefox
}
video.autoplay = true;
video.src = source;
changeStatus('Connected.', false);
}
function onError() {
changeStatus('Please accept the getUserMedia
permissions! Refresh to try again.', true);
}
function changeStatus(msg, error) {
var status = document.getElementById('status');
status.innerHTML = msg;
status.style.color = (error) ? 'red' : 'green';
}
var status = document.getElementById()
function setupPhotoBooth() {
var takeButton =
document.getElementById('btnTakePicture');
takeButton.addEventListener('click', takePhoto,
true);
var saveButton =
document.getElementById('btnSave');
saveButton.disabled = true;
saveButton.addEventListener('click', savePhoto,
true);
}
function takePhoto() {
// set our canvas to the same size as our video
photo.width = video.width;
photo.height = video.height;
var context = photo.getContext('2d');
context.drawImage(video, 0, 0, photo.width,
photo.height);
// allow us to save
var saveButton =
document.getElementById('btnSave');
saveButton.disabled = false;
var data = photo.toDataURL("image/png");
data = data.replace("image/png", "");
document.getElementById("<%= eventdata.ClientID
%>").value = data;
}
return {
init: function () {
changeStatus('Please accept the permissions
dialog.', true);
video.width = 320;
video.height = 240;
document.body.appendChild(video);
document.body.appendChild(photo);
navigator.getUserMedia ||
(navigator.getUserMedia =
navigator.mozGetUserMedia ||
navigator.webkitGetUserMedia ||
navigator.msGetUserMedia);
play();
setupPhotoBooth();
} ()
}
})();
function UploadPic() {
// generate the image data
var Pic =
document.getElementById("myCanvas").toDataURL("image/png");
// var Pic =
document.getElementById("eventdata").toDataURL("image/png");
Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")
// Sending the image data to Server
$.ajax({
type: 'POST',
url: 'Save_Picture.aspx/UploadPic',
data: '{ "imageData" : "' + Pic + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert("Done, Picture Uploaded.");
}
});
}
Saturday, 31 August 2013
Android - after loading URL with webview can i change background color
Android - after loading URL with webview can i change background color
I have a webview and im loading an external HTML form a site. I try to
change the background color using javascript function:
function changeBGC(color){
document.bgColor = color;
}
and that does not work. but if i load locally then im able to change the
background color. Is there some kind of security inhibiting me from
changing a web page i load into the webview externally ?
I have a webview and im loading an external HTML form a site. I try to
change the background color using javascript function:
function changeBGC(color){
document.bgColor = color;
}
and that does not work. but if i load locally then im able to change the
background color. Is there some kind of security inhibiting me from
changing a web page i load into the webview externally ?
Fading specific CSS/JS Popup when opening/closing
Fading specific CSS/JS Popup when opening/closing
The following code snippet shows how I made popups with CSS and JS. Is
there any chance to let it fade when opening/closing it, without changing
the way I used to work, I mean just popping up the box by changing its
display style?
function lightbox_open(){
window.scrollTo(100,500);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
The following code snippet shows how I made popups with CSS and JS. Is
there any chance to let it fade when opening/closing it, without changing
the way I used to work, I mean just popping up the box by changing its
display style?
function lightbox_open(){
window.scrollTo(100,500);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
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;
}
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;
}
Hide variable value
Hide variable value
I'm writing a small Android app and I need to hide some variable values. I
have a API key I got from the content provider who is providing me content
to show in my app that I need to retrieve data from them. Because it is
being used in an encryption algorithm, it is of vital importance the code
isn't leaked.
I need to save the API key in my code and be sure it isn't retrievable by
the bad people in the world. What I do now, is save them in my code as a
variable:
private static final String API_KEY="mysupersecreatapikey";
After the app is compiled to an APK file, this is, in my opinion not
retrievable anymore. Am I doing it in the right way now, or is there a
better solution?
Thx in advance, Daryl
I'm writing a small Android app and I need to hide some variable values. I
have a API key I got from the content provider who is providing me content
to show in my app that I need to retrieve data from them. Because it is
being used in an encryption algorithm, it is of vital importance the code
isn't leaked.
I need to save the API key in my code and be sure it isn't retrievable by
the bad people in the world. What I do now, is save them in my code as a
variable:
private static final String API_KEY="mysupersecreatapikey";
After the app is compiled to an APK file, this is, in my opinion not
retrievable anymore. Am I doing it in the right way now, or is there a
better solution?
Thx in advance, Daryl
Subscribe to:
Comments (Atom)