BestJewSinceJC 700 Posting Maven

I'm using a socket server that is based off of java and it allows java extensions to be implemented that allow server side login to be performed. I want to create a signup/login system but I'm struggling with finding a way to validate/filter user input before manipulating with the database.

1. I want to check for the type (string or integer)
2. I want to check for the length of strings to ensure they are within a given range
3. I want to be able to determine if a field contains a valid email address
4. I want to be able to remove invalid characters easily from any string
5. I want to sanitize before using with database.

Does Java have an easy way for me to do all of these things?
If anyone knows of a free class, framework, library that handles all of these tasks, please let me know about it.

Ricky

Some of these things might be desirable, but surely it would be a waste of time to actually search for code out there that would verify the length of the String for you? You *should* be able to write that piece of code in under 10 seconds. And PS, the only way to determine if an email address is valid is to email that address and require some type of a response from the account holder. On sites like daniweb this is done through some type of auto generated link that …

BestJewSinceJC 700 Posting Maven

Why do you need a StringSelection Object? You should just need the String, i.e. the one that is stored in your variable called 'selection'.

Btw, the error is that you're using PrintWriter's "print" method, and you are passing it a StringSelection Object. But if you look at the class documentation for PrintWriter, no such method exists. However, PrintWriter does have a method that takes a String, so you should get rid of the StringSelection variable and do:

pr.print(selection);

http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintWriter.html
^ Here is the PrintWriter javadoc, take a look. No "print" method exists that takes a StringSelection Object as a param.

BestJewSinceJC 700 Posting Maven

In reference to what Vernon just said, check out this link. It is very important to learn how to read and interpret the javadoc documentation. And when I think of dynamic memory allocation, I think of malloc and calloc in C. . in Java, all you need to do is use 'new' to allocate an Object dynamically (which you already did in your code).

P.S. You might want to read on google a bit about memory allocation in Java, because some might have issues with what I said in this post about the subject. The jist of it being that the JVM handles the underlying memory allocation, and all you are doing is using new to get yourself an Object. But surely, at runtime if you need an Object, array, or whatever else, you can use the 'new' keyword. This is an interesting article: http://www.javaworld.com/javaworld/javaqa/1999-08/04-qa-leaks.html

BestJewSinceJC 700 Posting Maven

You can keep track of the length of your array by having an int variable that stores the length, then every time you add something to your array, update it, and when you delete, also update it. To move everything back one space, all you need to do is have a for loop and a 'temp' variable to hold the element that you're about to overwrite (because if you overwrite it first, it is lost and can't be recovered - store it in a temp variable before overwriting).

If you can't figure out from that then show us some code. We aren't going to write it for you. (I.e. make an attempt yourself)

BestJewSinceJC 700 Posting Maven

I prefer the forums where I can leisurely browse to "live" chats, so that's why you'd rarely catch me there regardless of whether lots of people used it or not. But Narue is right, the majority of members probably have no idea that it exists.

BestJewSinceJC 700 Posting Maven

If someone asked me about the binary search bug, I'd assume they werre talking about a new computer virus being attached to grandma's email. To be honest, if someone asked me to write a binary search routine on a white board, I'd stare at them blankly and assure them that while I had wrote such things in first year programming at college, it had probably been equally as long since doing so, so could I please consult my good friend google. I guess I'm fortunate/unfortunate (depending upon your perspective) of not working on resource intensive applications and being able to just tap into built-in functionality (an example would be LINQ in .NET or, for that matter, retrieving exactly what I want from a database).

I don't think I wrote a Binary Search until my third year, if at all. But I'm confident in my abilities to do so, since I know the concepts behind it and I'm a competent programmer. But agreed, obviously the whiteboard situation brings one's nerves into play...

BestJewSinceJC 700 Posting Maven

Your question makes no sense. Rephrase it.

BestJewSinceJC 700 Posting Maven

I must bow to your extremely well-read self. I for one don't have time to read all books, magazines and newspapers that [have been/are being] printed in the English-speaking world, so I'm sure I've missed quite a few.

I'm just saying; I'm not the one arguing with the obvious without backing it up. I never claimed to be a genius or anything like that, only challenged what I think is an outlandish statement.

BestJewSinceJC 700 Posting Maven

I mean you could use a while loop, but it would be very unusual to do so in this situation. For example, consider the following array:

int[] myints = new int[10];
int i = 0;
while (i < myints.length){
//do something
i++;
}

//compare that improper use of while loop to this:
for (int i =0 ; i < myints.length; i++){
//do something
}

Hope that helps.

BestJewSinceJC 700 Posting Maven

You wouldn't use a while loop to loop over an array [in this case], you would use a for loop, since the size of the array is known in advance. To find the max number make an Integer variable max, write a for loop that loops over the entire array

for (int i = 0; i < array.length; i++)

then if your current integer you're looking at (which is array) is > 'max' then assign it to max.

BestJewSinceJC 700 Posting Maven

You have to keep track of the length of your array and shift the elements all back one, like verruckt said, every time that you 'delete' an element. The true underlying size of the array is not changing.

BestJewSinceJC 700 Posting Maven

Much better. Unfortunately I'm currently busy forcing MSN hotmail's crappy "locally saved emails" to be re-saved in ".eml" format, but otherwise, I would help you out immediately. But at least now when someone else comes by to help they won't be turned off by a lack of code tags.

BestJewSinceJC 700 Posting Maven

Combine the path that JFileChooser undoubtedly returns with this: http://forums.sun.com/thread.jspa?threadID=760337

There might be more current solutions available, and in fact I wrote the exact program you're describing myself, but since my program is on my older machine... you're out of luck there.

BestJewSinceJC 700 Posting Maven

I like the sport, and I like apple pie. But I don't like the cheating, it cheapens the game.

BestJewSinceJC 700 Posting Maven

Post code in code tags. You'll notice the button that says 'code' right above the reply box, it makes it easier for us to help you. And welcome to the forums. :)

BestJewSinceJC 700 Posting Maven

Try to do it yourself, ask specific questions if you have any.

BestJewSinceJC 700 Posting Maven

Lol - outsourcing to geek squad?? First that is an American company associated with Best Buy, second it doesn't even require a college degree to work there.

BestJewSinceJC 700 Posting Maven

And some of us hate baseball.

Nick Evan commented: Yup. Soccer however kicks ass :) +0
BestJewSinceJC 700 Posting Maven

>There are words I know you've never heard in common
>use -- they just aren't used in your particular area.

Like "y'all"? Both y'all are yankees (according to profile), so y'all might not hear "y'all" in common use like I do. ;)

Heheh, y'all.

Actually, I do realize that ya'll is quite common, and I've heard it said many times up North, although I've been to a few Southern states. Gauche.. not so much.

BestJewSinceJC 700 Posting Maven

Only in your limited scope of the world. There are words I know you've never heard in common use -- they just aren't used in your particular area.

Like gauche, which is not used frequently anywhere, and literature reflects common language more than anything. I challenge you to find in any writings or documents where the word gauche is used in any way that could be described as commonly.

BestJewSinceJC 700 Posting Maven

The folks at the NSA will pick your super secret password in less than a second. So, pick something simple you can easily type and remember in the hope that it is safe from the boss or your underage sister.

Really, how? Because the NSA might have faster computers and similarly capable algorithms, or even more advanced, than anyone else... but mathematics still applies to them and if I use a sufficiently long password they are still never going to figure it out without seizing my computer or intercepting it on the interwebs. Not that I know anything worth knowing, I'm just saying, I call BS on you.

BestJewSinceJC 700 Posting Maven

[OFF_TOPIC]


I have a point, but I'd also like to **** off gung-ho pillocks that say things like "we're at war with the Muslims". You probably don't like my post, but I don't like people banging on about their right to invade other countries and then blast 'em with Bible bombs.

Just the point that the US government/military always seem to make a bigger mess of things wherever they go in the world. You probably get the sanitized version of news over there in the US, but we got to see what the military really got up to in Haiti - blocked roads, harbours and airfields. Aid was there but was prevented from being distributed. A friend of mine went over last week and couldn't believe the succession of cock-ups. Absolute joke.

I've got a future news flash - "US needs new Guantanamo Bay - Haiti government agrees"
[/OFF_TOPIC]

That's funny, because I do recall that the Republic of Haiti is capable of media communication, but I do not recall them ever complaining about the U.S. government or the millions of dollars in aid that has been donated by U.S. citizens.

BestJewSinceJC 700 Posting Maven

Strangely enough, I've completely avoided everything except "tell me about your strengths/weaknesses" type questions and a few questions about code I'd already written. I've only done 3 interviews (since I got a job out of my third, that's where I stopped) so that could be why, but perhaps it is because I'm a college student and easier questions are asked of college students than of professionals.

BestJewSinceJC 700 Posting Maven

I disagree. Perhaps it's becoming more and more unusual as the average level of education plummets, but I'm not surprised to see it in the wild.

It can be easily seen through common language and in literature that the word is uncommon. I agree with you, more or less, on the other points. They should have left it at "unconventional" though. I also hope they see this thread somehow, despite the unlikelihood, because it would be amusing to see such a random argument.

BestJewSinceJC 700 Posting Maven

No problem, glad you got it working, mark the thread as solved.

BestJewSinceJC 700 Posting Maven

Use the "code" tag when posting code. I see a mistake in your code (the bracket after the for loop is in the wrong place), but I cannot tell you what line the error is on because you didn't post with code tags.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

That contains everything you need to know about a JTable. Read.

BestJewSinceJC 700 Posting Maven

There is no array in my pseudocode or in my explanation. Re-read my explanation. All you're doing is prompting for each value, and after each value, checking to see if that value was greater than or less than the previous. Since you're only storing two things, you do not need an array, you only need two variables.

BestJewSinceJC 700 Posting Maven

Did you even read about modulus? If 100 % yourCents > 50 then you would round up. Otherwise, your value would stay the same. This is because an int value "truncates", so when you previously did

int dollars = cents/100;

it automatically "rounds down" by default (everything is cut off except the integer value, so if you had 1.2 it would end up as 1, and if you had 1.9 it would still be 1). Again, modulus gives you the remainder of division. So if you don't get it then look at some examples online, otherwise, think about what a remainder is.

BestJewSinceJC 700 Posting Maven

TBH, if anyone asked me to write a Binary Search Algorithm on a whiteboard, I would be terrified. I'd need a few minutes just to sit and think about what to do, and I can't do that with someone else waiting for me to do it. Also, Narue, I have a lot of respect for your methods, as I've heard some horror stories about the crazy questions that get asked.

BestJewSinceJC 700 Posting Maven

>A word I wouldn't expect to see in a programming text
>"gauche"

Why not?

Probably because it is an extremely unusual word, it is not the best word to describe the situation, and most of the readers of any programming book would not have ever seen or heard the word before.

BestJewSinceJC 700 Posting Maven

Use your brain. This problem is practically begging you to have some level of intelligent thought. They didn't even ask for programming examples, they just asked for examples. Are you really that dumb.. ?

BestJewSinceJC 700 Posting Maven

Then you probably don't have the console view (or whatever netbeans calls it) open on netbeans. So it is probably producing the output but not showing it to you. I can tell by looking at your program that it works, but I ran it just in case, and it works fine on Eclipse.

BestJewSinceJC 700 Posting Maven

You only need two variables, one to hold the largest integer and one to hold the smallest integer. Initially, you should set both the large integer and the small integer to the first value read in. For each of the four values afterwards, you should use your if statements to compare the value to the value in your smallest variable and largest variable. If the new value is smaller, set the 'smallest' variable to it. Etc.

This is what your program would look like in pseudocode:

print("please enter your first value");
int val = getTheIntegerValue();
smallest = val; 
highest = val;

//Now you have to read in 4 more values
for (int count = 0; count < 4; count++){
print ("enter the next value");
int val = getIntValue();
if (val > highest) highest = val;
if (val < smallest) smallest = val;
}

printHighestValue;
printSmallestValue;

You'll notice I gave you some code that will work in Java, but I only gave you that code because doing so makes my explanation confusing. Other code is pseudocode, so the idea works, but the exact statements will not work in any language. Look at it because that's what you have to do in Java.

BestJewSinceJC 700 Posting Maven

Look into the File class, it will allow you to create a new File by a given name, I think. And also look into the PrintWriter class which will allow you to output to a text file.

BestJewSinceJC 700 Posting Maven

To force a singleton I suppose you could simply use a getInstance method and refuse to return an instance if one already exists. I *think* you could enforce this by instantiating a private constructor (thus preventing anyone from calling it outside the class). By instantiating a private constructor, you are preventing the default constructor from being created by the compiler or whatever. Those are my thoughts, the wikipedia references the getInstance method, but as far as the private constructor, I'm just winging it, I've never actually had the need to make every constructor of a class private, so try it out yourself.

Also, sorry for not giving an example, but as I said, I've never used a singleton, so personally, I consider that design pattern completely unnecessary. Perhaps someone here can give an example that will change my mind.

BestJewSinceJC 700 Posting Maven

Use division by 100 in combination with the 'int' primitive type to see how many dollars you have. For example,

int dollars = cents/100;

No matter what (int) value you have for cents, at the end of that operation, dollars will be an integer, so it will have the exact amount of dollars. You could have up to 99 cents left over, so you can use the % operator to figure out what your remainder is. I don't know what you mean by "round to the nearest cent", did you mean "round to the nearest dollar"?

modulus: http://en.wikipedia.org/wiki/Modulo_operation

BestJewSinceJC 700 Posting Maven

Well the depth is the length of the path from the root of the tree to the node (the number of edges encountered). A binary tree has two children so you can write a simple method that takes a depth (originally passed in as 0) and a Node (the root is originally passed in). Then at the end of the method you'd increment depth, then call the same method with method (depth, leftChild) and another call to method(depth, rightChild). So that you know what Node has what depth, you could do a variety of things, but assuming you're passing in the root node then traversing with Node.leftChild and Node.rightChild, this shouldn't be a problem anyway. (In other words, why are you having trouble storing it? You could simply have a depth variable in your Node class and eliminate the problem quite simply.)

You could also look into Binary Heaps, if you wikipedia them, the wikipedia page has a section called "Heap implementation" that explains how to represent a binary tree as a heap. If you were to arrange your tree like that, finding the depth then becomes an arithmetic problem I believe.

BestJewSinceJC 700 Posting Maven

Print something to your screen? Or send something to the printer?

1. System.out.println()
2. http://java.sun.com/developer/technicalArticles/Printing/SwingPrinting/

BestJewSinceJC 700 Posting Maven

No, do not use roseindia. There are some good learning materials stickied at the top of this site.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html

this is all you will ever need to know about creating objects.

BestJewSinceJC 700 Posting Maven

^ No, that is not what he wanted. He just wants any 0's to be removed from the array. I thought I already answered this in another thread last week but in any case it's pretty simple so I don't understand why all of you guys are making it seem so complicated. In addition to the array, use one variable that contains the current size of the array. Then you just loop through the array and if there is a 0, you 'delete' it simply by replacing it with the next non-zero element in the array. So if you had

1, 2, 0, 4, 5 you'd end up with 1, 2, 4, 5, something (the something can be any value, but your size variable will hold 4 so you know the first 4 values in the array matter and the rest do not).

Shifting the non-zero values to the beginning of the array might be a bit trickier than it looks at first glance but it isn't hard.

BestJewSinceJC 700 Posting Maven

I am using the real version of google

What's that? Teach us more.

BestJewSinceJC 700 Posting Maven

So this is basically... similar to Scrabble? So you need two things:

1. Anagram finder to find every possible anagram
2. Simple search of your dictionary to see which anagram results in the most points.

P.S. I'm not a genius on the subject but it seems like you need to generate every possible permutation of your set of letters and for each permutation you generate, check to see if it is in your dictionary, and if so, check to see if it is higher than your current highest word. If so, it becomes the new highest word. I'm suggesting that you look into some open source code to learn about generating the permutations, and what you're looking for is called an anagram.

this also looks potentially useful.

BestJewSinceJC 700 Posting Maven

@ poster above me: init doesn't mean alloc. If you're assuming that space has already been set aside for your array (i.e. the array already exists) then simply by updating the appropriate variables (e.g. size, last index, whatever) you can init in constant time. Similarly, for adding, if you keep track of the index of the last element that was added, you can add in constant time by adding to the last index plus one. To remove, you're setting a constant number of bits (to some predetermined 'wipe-away' value) so that is also constant. Anyway, I don't understand what the OP is saying about junk values being "magically set" to the pointer... explain please.

BestJewSinceJC 700 Posting Maven

Yeah, you'll find them in a dictionary. Try Merriam Webster. They have a website as well.

BestJewSinceJC 700 Posting Maven

Grab an algorithms book or tutorial and read a little bit, if you're thinking you're going to get a ready made formula like a 'typical' math formula, you're on the wrong track. You might want to read about asymptotic analysis, runtime analysis, Big O, and connected topics that come up in searches. (I'm just giving you some terms to search for so you aren't at a loss where to start). The beginning of CLRS (Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Cliff Stein, published by MIT Press and McGraw-Hill) introduces everything you need to know to analyze *most* (and by most I mean simple) programs, so if you're still at a loss after looking into other sources, check that out.

BestJewSinceJC 700 Posting Maven

My girlfriend bought me a small hdtv (for playing video games). Other than that I got a pair of basketball shoes & a ball.

BestJewSinceJC 700 Posting Maven

Nobody is giving you anything except advice (and I find even that doubtful at this point). Read the rules or go away.

BestJewSinceJC 700 Posting Maven

Your question pertains to computer science in general more than it does to java, maybe you should try posting in that forum?

faisal6621 commented: In academics we are simulating the OS CPU Scheduling policies in either Java or C/C++. So it can be posted in this forum. +0
stephen84s commented: I just dont get what faisal6621 meant to justify the negative rep +6