Categories
discuss

Regex allowing a space character in Java

Hey all, so I’m trying to allow some text input which goes through a regex check before it’s sent off. I want the text to only include A-Z, 0-9, and the space ” ” character. Here is my code now:

if(!title.matches("[a-zA-Z0-9_]+") {
    //fail
}
else {
    //success
}

However this still gives the //fail result when I input something like “This is a test”

Any ideas? Thanks all.

Answer

You’re not including the space in the Regex. Try the following:

if (!title.matches("[a-zA-Z0-9 ]+"))
Categories
discuss

removing one property of an object

I have an object that looks like this.

{
par1: 'par1value',
par2: 'par2value',
par3: 'par3value'
};

I want to remove the property called par1 and save it separately so it looks like this

var par1 = 'par1value';

{
par2: 'par2value',
par3: 'par3value'
};

Can someone suggest a nice way to do this

Answer

Get the property:

var value = obj.par1;

Remove the value from the object:

delete obj.par1;

More detail about delete can be found here.

Categories
discuss

how do i set an alarm manager to fire every on specific day of week and time in android?

for example i want to have an alarm that will fire every sunday at noon…. how would i do this?

Answer

Use the AlarmManager class:

http://developer.android.com/reference/android/app/AlarmManager.html

Class Overview

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

Use public void set (int type, long triggerAtTime, PendingIntent operation) to set the time to fire it.

Use void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) to schedule a repeating alarm.

Here’s a full example. I don’t really remember all the Calendar methods, so I’m sure that part can be streamlined, but this is a start and you can optimize it later:

AlarmManager alarm = (AlarmMAnager) Context.getSystemService(Context.ALARM_SERVICE);
Calendar timeOff = Calendar.getInstance();
int days = Calendar.SUNDAY + (7 - timeOff.get(Calendar.DAY_OF_WEEK)); // how many days until Sunday
timeOff.add(Calendar.DATE, days);
timeOff.set(Calendar.HOUR, 12);
timeOff.set(Calendar.MINUTE, 0);
timeOff.set(Calendar.SECOND, 0);

alarm.set(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(), yourOperation);
Categories
discuss

How can I initialize an ArrayList with all zeroes in Java?

It looks like arraylist is not doing its job for presizing:

// presizing 

ArrayList<Integer> list = new ArrayList<Integer>(60);

Afterwards when I try to access it:

list.get(5) 

Instead of returning 0 it throws IndexOutOfBoundsException: Index 5 out of bounds for length 0.

Is there a way to initialize all elements to 0 of an exact size like what C++ does?

Answer

The integer passed to the constructor represents its initial capacity, i.e., the number of elements it can hold before it needs to resize its internal array (and has nothing to do with the initial number of elements in the list).

To initialize an list with 60 zeros you do:

List<Integer> list = new ArrayList<Integer>(Collections.nCopies(60, 0));

If you want to create a list with 60 different objects, you could use the Stream API with a Supplier as follows:

List<Person> persons = Stream.generate(Person::new)
                             .limit(60)
                             .collect(Collectors.toList());
Categories
discuss

JavaScript -Change CSS color for 5 seconds

I am trying to make a way of briefly highlighting the text on named links – but only for a few seconds.

<a href="#faq1"onClick="document.getElementById('faq1').style.color='#f00'">

So on a list of FAQs – it jumps to the proper ID, changes the color to red for a few seconds as a visual cue to the end user (the answer is here). but then returning to normal color and the interval is complete.

How do I make the above function to work for only a set period of time?

Answer

try this:

function highlight(obj){
   var orig = obj.style.color;
   obj.style.color = '#f00';
   setTimeout(function(){
        obj.style.color = orig;
   }, 5000);
}

and in the html:

<a href="#faq1" onClick="highlight(document.getElementById('faq1'));">

this function will work for any object you pass to it 🙂

here is a working fiddle: http://jsfiddle.net/maniator/dG2ks/

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..