java

java optional parameters

I want to write an average method in java such that it can consume N amount of items, returning the average of them: My idea was: public static int average(int[] args){ int total = 0; for(int i=0;i<args.length;i++){ total = total + args[i]; } return Math.round (total/args.length); } //test it average(1...

proper way to read user input from command line in java

I was hoping to get some opinions regarding best practices and comments on the way I read user input from the command line. Is there a recommended way to do this, am I using the try/catch blocks properly? My example here works fine, but would still like to hear if there is a 'cleaner' way to do this. Many thanks. For example are he retu...

Java: how to represent graphs?

I'm implementing some algorithms to teach myself about graphs and how to work with them. What would you recommend is the best way to do that in Java? I was thinking something like this: public class Vertex { private ArrayList<Vertex> outnodes; //Adjacency list. if I wanted to support edge weight, this would be a hash map. //me...

how to catch blank input with scanner class in java

I am using the scanner class to capture user input from the command line (strings only), as an alternative to my previous question. The following seems to work fine, except the blank lines are not caught as they should by the second conditional. For example when I press enter, this should be captured as a blank line and the second condi...

database display in textarea

I have a database, I used jsbc::odbc to connect it with my JSP page. Now I want that the contents of my access database to be printed in a textarea of my JSP page with a next and previous page button by which I can switch between different fields of database. Can anybody help me with the code? ...

BlazeDS turnkey configuration for mysql

What is the configuration and code required to use mysql within a BlazeDS turnkey environment? ...

A collection that represents a concatenation of two collections in Java

Is there a class that represents the concatenation of a collection with another collection? This class should be a Collection in itself, and should delegate all methods to the underlying (inner) collections - no extra memory should be allocated, nor any of the original collections modified. Example usage: Collection<String> foo = ... C...

Problem with Java generics

I have an error in some classes when I use generics. I specified where I get the error. I can't get rid of it. I tried with casts, and everything I could think of. class UltraTable<O extends Object> { public void setContent(Collection<O> collection) { } } class ObjectA { } class AShell { protected UltraTable<? extends ObjectA> table...

"Form" - If-else Statements

I'm trying to realize a simple form: there's a globalname and globalpass, and the User is asked to type in his name and pass. If name == globalname and pass = globalpass -> "Hello name" If name == blank or pass == blank -> "O NOEZ, you fergit sumpn!" If name != globalname or pass != globalpass -> "WHUT? Who be you, intruder?" So far ...

Creating an universal comparator to sort a tree map problem.

This is where I'm at: public final Comparator<Object> ID_IGN_CASE_COMP = new Comparator<Object>() { public int compare(Object o1, Object o2) { String s1 = null; String s2 = null; try { Class c = o1.getClass(); IO.println(c.getName()); //java.lang.string instead of Animal M...

java inheritance - please explain

class A { public void talk(){ this.sayIt(); } private void sayIt(){ System.out.println("class A says..."); } } class B extends A { private void sayIt(){ System.out.println("class B says..."); } } B b = new B(); b.talk() //output class A says... I cannot get this since: Class B inherits from class A the public member and ...

What is the Java equivalent of .NET BitConverter ?

I was writing an article in which I wanted to show how to send objects across the network, from Java to CLR/.Net and Back. http://ferozedaud.blogspot.com/2009/11/howto-serialize-data-from-object-from.html While doing research for this, I could not find a Java equivalent for BitConverter class that exists in .NET. Due to this, I had to ...

Convert typed-in Text to lowercase

I've got an index.jsp with [snip] <% String name = request.getParameter("name"); String pass = request.getParameter("pass"); String globalname = "webeng"; String globalpass = "2009"; if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass)) { %> <hr /> <p><b>Howdy, <%= request.getPara...

Do I have to do a setSize() on a Vector before using it?

Given private final Vector v = new Vector(); //instance variable the following 3 lines are in an instance method in the same class. 1. int length = v.capacity(); 2. int size = v.size(); 3. Object o = v.getElementAt(0); I am getting an ArrayIndexOutOfBoundsException at line 3. The debugger shows that the Vector has 1...

Extracting text from a PDF file.

I'm using PDFBox for a C# .NET project. and I'm getting a "TypeInitializationException" (The type initializer for 'java.lang.Throwable' threw an exception.) when executing the following block of code : FileStream stream = new FileStream(@"C:\1.pdf",FileMode.Open); //retrieve the pdf bytes from the stream. byte[] pdfbytes=new byte...

RAM memory reallocation - Windows and Linux

I am working on a project involving optimizing energy consumption within a system. Part of that project consists in allocating RAM memory based on locality, that is allocating memory segments for a program as close as possible to each other. Is there a way I can know where exactly is the position of the memory I allocate (the memory chip...

setProperty must be overridden by all subclasses of SOAPMessage

I'm trying to deploy some web services in a WAR application on JBoss 5.1.0. I have created the source files from an existing wsdl using JAX-WS tool wsgen. This created the Service files and @XmlType annotated clases that would act as request and response wrappers. This classes worked well on JBoss 4.2.3, but when moving to JBoss 5.1.0,...

After wait shoudln't thread be in blocking state rather than runnable state?

Hi, I am going through a java 6 book. A sample code snippet is given below from Threads chapter, where I need a clarification synchronized(a){ //The thread gets the lcok on 'a' a.wait(2000);// Thread releases the lock and waits for notify only for maximum of two seconds, then goes back to runnable state //The thread reacquires the lock...

Java Random Slowdowns on Mac OS cont'd

I asked this question a few weeks ago, but I'm still having the problem and I have some new hints. The original question is here: http://stackoverflow.com/questions/1651887/java-random-slowdowns-on-mac-os Basically, I have a java application that splits a job into independent pieces and runs them in separate threads. The threads have...

Checking condition inside a loop

Hi, If "flag" is true I have to perform step no. 1 otherwise skip it. Is there a way out to skip this unnecessary repetitive check within the loop. (As the value of flag is not changing while the execution of the loop) private void method(boolean flag) { while (man > woman) { if (flag) { // Step no. 1 System.ou...