~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

A list of all getting started resources can be found in the sticky placed at the top of the forum. Go through those and post specific questions if you have any.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

You might have multiple ways of representing cards; for e.g. 4H or IV Hearts or 4 Hearts or 4 1 (where 1 is a code for hearts). Given the multiple ways in which a single card can be expressed, why not have a contract defined which deals with parsing a card based on the representation? In the absence of a CardParser interface, the parser class would contain a lot of IF's to check for the format which the user has used and then parse the Card accordingly, something which can be elegantly dealt with by polymorphism. A generic parser class would be something of a "god" class is the sense that it would need to "know" about "all" the possible representations.

Also, it would violate the basic principle of "cohesion". Any new representation added or any change in existing one would require a change in the method which is already dealing with other representations thereby increasing the possibility of a change impact. But that's just me, YMMV. :-)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> I don't believe enums would be a smart move here

Enums are a natural fit here IMO. It's a different thing if you are confused on how to get things rolling with enums. One possible OO solution would be:

  • Create a enum CardSuit (e.g. SPADE, HEART, DIAMOND, CLUB)
  • Create an enum CardRank (e.g. ACE, KING, TWO, THREE etc)
  • Create an interface CardParser having a method Card parseCard(String representation)
  • Create a concrete class AbbreviatedNameCardParser which parses the passed in String and create a Card instance out of it
  • Create a Card class which has two members; suit and rank both of enum types. Add a toString method which would print out the entire card name based on the suit and rank.
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

First and foremost, make sure you work through the official Java tutorials offered by Oracle (the "Trails covering the basics" section) to get a hang of things.

After that, picking up tutorials depends on the kind of project you are interested. Interested in developing web applications? Head over to the Java EE tutorial section. Interested in creating UI's? Head over to the "Creating graphical interfaces section". All in all, work out the basics and then post again so that the members here can give you specific advice.

If you are feeling rather adventurous, also have a peek at the sticky placed at the top of the forum which brings together all the resources you might want to refer as a beginner.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

There are forums out there which close duplicate threads ASAP thereby preventing replication of effort and so on. After all, as you said, there is no point in regurgitating the same stuff time and time again.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

but it seemed a little "backwards" to me.

Backwards? IMO, delegating to an appropriate class for performing the different functions of a system is far more logical than creating a "god" class which is responsible for everything (retrieving data, setting up responses etc.).

Could you explain in more detail how 3rd party frameworks could help? Not quite sure I follow.

Is this is web application or a RESTful API you have created for external consumption? If the former, you can use the Spring MVC framework which provides an abstraction over the basic servlet infrastructure. If the latter, using the RESTful framework offered by Spring would again significantly cut down your code. I haven't worked extensively with either of those so the Spring documentation would be your best friend here; esp the Spring MVC and Spring REST framework part.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Oh, way too quick James. I kinda updated the original post with some more content. :-)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> Also, I was not advocating using a StringBuffer

It wasn't meant for you but Jon who was mentioned using StringBuffer. :-)

> Any particular reason to use

The domain for a boolean type consists of only two values: true and false. The Boolean class has built-in two static variables; TRUE and FALSE representing truth and false values respectively.

AFAIK, new Boolean() always creates a new Boolean object (which isn't exactly required as explained above) whereas Boolean.valueOf(booleanVariable) returns one of Boolean.TRUE or Boolean.FALSE based on the passed in boolean to valueOf method.

Similar thing is with the Integer class, it is always recommended to use Integer.valueOf(int) instead of new Integer(int) since the Integer class maintains its own internal cache which returns cached objects for integer values in the range of -128 to 127. These cached objects don't present an issue given that the wrapper classes are immutable. :-)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

StringBuffer class is synchronized. The vast majority of the cases which don't need this synchronization are better off using its non-synchronized counterpart; StringBuilder.

> String str = new String(booleanVar);

I don't think there is any String constructor which takes a boolean. Anyways, the most efficient way to convert a boolean to String would be:

boolean bVar = true;
String bStr = Boolean.valueOf(bVar).toString();

It always returns the interned strings ("true" or "false").

The most commonly used String constructor is the one which accepts a byte[] or char[], which is normally required when reading from a char/byte based store.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

it looks like your ConnectionResult class, based on the psuedo code with the <String> is using generics

I had actually planned on posting a generified solution but it got a bit complicated so I stripped it out for brevity. Anyways, those changes can be made once you get your design is finalized.

Say I have N number of back-end systems, which can be database servers, web servers, etc... The base Connection class will represent a connection to each of these back-end systems, with abstract methods for retrieving xml, csv, binary, etc... responses. This response data is then output to a servlet response output stream.

I'm a bit wary of this abstraction you are proposing here. The connection class you talk about already exists. I think there is a reason why the existing connections are not grouped into a single subclass. The functionality offered by each one of them is so varied that abstracting it provides little or no benefit. There are two levels of abstraction which we are talking about here:
- Connection abstraction
- view abstraction (ability to present data in different formats)

The reason why you are finding it difficult to visualize the solution is because you are missing an important layer; the data access layer. Connection is something physical and view is something abstract which can and will vary. The way/logic a data is retrieved from a connection is very specific. For e.g. if you have a JDBC connection, you are dealing with …

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

IMO, the connection executor is not the right entity to handle the result of your execution. IMO, creating handler classes for specific return types (HandlerXxx for every Connection which returns a String etc) would be a much better strategy since the ConnectionExecutor anyways doesn't know which type of connection it is executing. Something like this should get you started (untested):

class ConnectionResult {
    private final Object t;
    public ConnectionResult(Object t) {
        this.t = t;
    }
    public Object get() {
        return t;
    }
}
abstract class Connection {
    public abstract ConnectionResult getResult();
}
class StringConnection extends Connection {
    public ConnectionResult getResult() {
        ConnectionResult<String> result = new ConnectionResult<String>("A");
        return result;
    }
}
interface Handler {
    void handleResult(ConnectionResult result);
}
class StringHandler implements Handler {
    public void handleResult(ConnectionResult result) {
        String strResult = result.get();
    }    
}
class ConnectionExecutor {
    public void execute(Connection connection, Handler handler) {
        handler.handle(connection.getResult());
    }
}

Anyways, I'd recommend stating the original scenario here so that we can suggest better solutions if any.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

The difference between having promotional links in your signature and in each of your posts is that signatures can be disabled site-wide. This is extremely useful in case you want to do away with all the promotional links placed in signature in one go (e.g. when you ban the member and disable his sig), as opposed to editing each post and removing the link spam.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

First suggestion; instead of making forum members download a file from "mediafire", host it on site open-source code hosting sites like Google code. After doing so, you'd be a bit comfortable with version control in general (which is the heart of every development) and people would be able to view your code online instead of downloading it on their own computer.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I never mentioned positive integers; 2147483646 - (-2147483644) exceeds the max value and hence overflows. Of course, the problem won't arise as long as negative ID's are not allowed (but are currently allowed). My point was that comparing numeric values should be dealt with caution.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> This might also work

Generally not recommended unless the range of values is known in advance and is small (i.e. v1 + v2 < Integer.MAX_VALUE). This is because the moment the difference between the two exceeds the range allowed for integers, the value of the expression overflows giving false results. The way OP has tackled the issue is as good as it goes.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

AFAIK, there is no way to customize the MFF since it is decided based on your usage patterns. Subscribing to forums and tracking them via your CP is one way of watching selected forums irrespective of your usage/posting history.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

`this` refers to the current object in consideration. Static methods don't have an instance associated with them and hence `this` is not allowed in static methods.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

This doesn't seem to be a problem with JUnit code but more with the way data is retrieved and its size calculated. Are you sure the DataBank and DataBuilder classes are not holding some sort of global state?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

This is how very large numbers are by default displayed in an excel file. Try pasting the same number (123456789123456789123456789) in a fresh excel worksheet and the result should probably be the same. You need to set the column/cell type of that excel sheet to "Text" instead of "General" which is selected by default. Look into the API of your excel exporting library for setting the same. In excel, this can be done by right clicking the cell/column -> selecting format cells -> Text.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

This is of course a path related issue unless you are getting an exception stack trace on your console, which might indicate some other problem. You need to provide us with the directory structure of the deployed application, the URL with which you access the registration page and the URL which gives you a 404 error. In case you are doing some path related manipulations or configuring JSP's explicitly, you'll also need to paste your deployment descriptor (web.xml).

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

@shrekting

Given that this is an international forum, please post in English since it's a violation of the rules to not do so. As per the "Keep it clean" rule:

We strive to be a community geared towards the professional. We strongly encourage all posts to be in full-sentence English

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

The simplest way here would be to create a servlet ProfileServlet which handles the task of profile creation/viewing. Map this servlet to the path /profile/* . A GET request to this path would invoke the doGet() method of this servlet which would read the username from the URL, fetch the details for the given user, store it in the request scope and and forward to a relevant view i.e. the page which should be displayed to the user. A POST request to this URL would create a profile for the same user. For an in-depth overview of these things, look up on "servlet mapping", "fileter" and "servlet rewrite".

This task should be pretty easy if you are using a framework like Struts 2.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Use Firefox when developing and look at the error console which shows all the Javascript errors on your page. Also, look at the source code generated for the above lines and see if there are any quote escaping issues which are pretty normal when spitting HTML using Servlets. It is recommend to switch to JSP for rendering (your view i.e. HTML) and use Servlets to handle things like form submission, file uploads etc.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

The functionality you require is not present in HashTable (predictable iteration order; i.e. iterating over keys in the order which they were inserted) hence you'd have to use LinkedHashMap. If you are feeling adventurous, you can extend HashTable to mimic LinkedHashMap but that would be *really* unwieldy and not recommended.

BTW, why is it that you don't want to use a LinkedHashMap?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

If you need predictable iteration order, use a hashtable implementation which supports the same. Drop HashTable in favour of LinkedHashMap.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> oos.writeObject("" + new VarnaPacket(player));

This line means you are sending across a string object and not a VernaPacket object hence the cast on the receiving end fails.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> ... or should I be using native2ascii in some way?

AFAIK, native2ascii is normally used when dealing with properties file since the specification says that properties file can contain only ASCII characters as mentioned here. IMO, instead of modifying the program text, you are better off setting the -encoding flag when compiling via command line.

BTW, are these locale sensitive strings part of the key or value part of the map? If used as keys, won't it be better to just use the ASCII characters. If used as values, would you be displaying them at any point in your application? In that case, you are better off placing them in a properties files.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> ... or should I be using native2ascii in some way?

AFAIK, native2ascii is normally used when dealing with properties file since the specification says that properties file can contain only ASCII characters as mentioned here. IMO, instead of modifying the program text, you are better off setting the -encoding flag when compiling via command line.

BTW, are these locale sensitive strings part of the key or value part of the map? If used as keys, won't it be better to just use the ASCII characters. If used as values, would you be displaying them at any point in your application? In that case, you are better off placing them in a properties files.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Except that don't use == operator for comparing strings; use the equals/equalsIgnoreCase method instead.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Yes.

In most applications, a common class instance (singleton) is used to keep track of global state. The advantage here is ease of use; just refer that class from any part of the application. The downfall is that tracking *which* part of your code changed the *global* state is difficult for an application having sizeable application logic.

In Java, it is a commonplace occurrence to rely on instance fields to be used in class methods. In languages where functions are the only units of abstraction and complex abstractions are built upon simple ones, making a function relying on some global piece of information limits its reuse.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Seriously this should be a website where people are allowed to make mistakes ESPECIALLY NEW MEMBERS

Yes, but it's our job to bring members on track when they make such mistakes. You should understand that you ignored rules which clearly say that site wide signatures need to be used instead of using URLs in your post. If you didn't understand the term (site wide signature), you could have easily posted a question and people would have gladly helped you out.

SO FOR SANJAY who gave me a negative feedback THANKYOU FOR BEING SO UPTIGHT DUDE CALM DOWN! I was just posting my appreciation for this site NO NEED TO HAVE A HEART ATTACK OVER A HYPHEN INITIAL AND HYPHEN ALRIGHT?

I was just doing my job, really. And the warning was not for using initials but having link in your post.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Looking at the Javadoc for the String class and finding the required method would be a good exercise for you.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Idempotent methods are also called pure methods; methods whose output purely depends on the passed in arguments and not any external state. Pure functions play a critical role in "pure functional languages". The wikipedia entry for pure functions describes them pretty well:

The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices

Examples:

// Pure
public int add(int a, int b) {
  // computation uses only the passed in arguments
  // all resources used are exclusive to the function stack
  // on which they were created (except shared global resources)
  int result = a + 10 - 233 * 23 / 2 + b;
  return result;
}

// Impure
public int add(int a, int b) {
  // computation uses global state; not pure
  int result = a + 10 - 233 * 23 / 2 + b;
  result = result * CommonConstants.MODIFIER;
  return result;
}
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Nick did a good job of coming up with the approximate formula used when deriving rep:

This is how I figured it's sort of working; you get +1 power for:

  1. each year of membership
  2. every ~1000 posts
  3. every ~500 reppoints
  4. negative rep power is half your +power with floor-rounding

.

The entire thread here (Area 51).

Lusiphur commented: I know it doesn't work in feedback but, it's the thought that counts right? +0
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Only form values are submitted when you press the submit button of the form. You need to place your form controls (text inputs, combo boxes, radio buttons) inside your form control for the values to be submitted to the server.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

No offense, but if you had read the posts over I did indeed give a short explanation of what I was having issues with. I do agree that my first code was a bit long, but the second one is as short as I can manage to make it.

No offense, but did you even read the link I posted? The second 'C' in SSCCE stands for compilable code which none of your posts contain. When posting queries/issues, try to phrase the question such that the person trying to help you out has to exert the least effort. Things like "i was hoping you'd understand" wastes my as well as your time. Anyways, after trying really hard to make out what you want, here's what I think.

You can't cast Arraylists the way you are trying to do. This is because generics in Java don't exhibit the covariance property shown by arrays. When dealing with arrays, T[] can be assigned to S[] if S is the super-type of T. The problem in this case is that you might end up putting elements in an array of a type which don't belong to that array which blows up at runtime (not compile time).

Integer[] ints = new Integer[] { 1, 2 };
Number[] nums = ints; // ok
nums[1] = 1.23;  // runtime exception

Generics on the other hand are invariant i.e. List<T> can't be assigned to List<S> unless both S and T are the same. Covariance can be …

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Hovering over the graph gives you the number of posts posted for each month, but yes, the ability to display reputation received is still not present AFAIK.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Just flag the same post again requesting it to be ignored, should do the job IMO.

Also, the flag bad post isn't in any way related to the age of the thread/post. If a member finds something inappropriate with a year old thread (spam/other violation), reporting it makes sense though practically speaking this isn't a regular occurrence. Spam posted in 1999 is still spam in 2010, if you know what I mean. :-)

Lusiphur commented: Ya, I know, feedback forum = no rep but still! +0
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> b = (one)a;

`b` and `a` are of reference type ArrayList but you are trying to cast them to type `one` and hence the error.

BTW, instead of posting large code snippets it would be better if you explained "what" you wanted to do and a small code snippet which demonstrates the exact problem you are facing. Read SSCCE.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

vBulletin has a public accessible bug tracker which you can find here. But yeah, finding the bugs which are affecting you might be a bit of bother given that Daniweb uses some kind of customized vBulletin.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

You don't need to run Apache Web Server to test your servlet applications. Tomcat is a webserver as well as a servlet container and by default listens to HTTP requests on port 8080.

The problem in your case is that you haven't placed your web application name in the URL. Assuming your web application (WAR file) name is "SampleWebApp", then the URL http://localhost:8080/SampleWebApp/aa.jsp should work assuming you have a JSP file aa.jsp deployed and it doesn't have any compile time errors.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> Solved the problem

Good thing that you could come up with a solution on your own. A couple of points though in case you are starting with Java:

  • Declare/define variables closest to their usage point. In your code, you create an object array on the first line and use it on the 4th line. The lesser the scope of your variables created, the more easier it would be for you to reason out with your code. A thousand declarations at the start of a method add to the confusion. In your case, you could have done something like:
    Map<String, String> map = new HashMap<String, String>();
    map.put("user", "username");
    Object[] params = new Object[] { map };

    Mind you, this is more of a "good coding practice" thing than a requirement.

  • Don't use HashTable; use an HashMap instead. In almost all cases, you really don't need the additional method level synchronization offered by HashTable. HashTable, Vector and a few other classes have got pretty good replacements in the standard library so unless you are planning on dealing with some legacy code, you are better off without them. The only reasons these classes have been around is to maintain binary/backward compatibility.
  • If you are using Java 5 or greater (btw, you really should), utilize the "generics" feature offered by the language. For example, your map could have been created like Map<String, String> map = new HashMap<String, String>(); . The advantage here is the additional compile time check placed by the compiler …
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> Can anyone tell me how to use new Instance and what mistakes i
> have made in this program.? newInstance is actually a method of the Class and Constructor classes. You need to grab the class instance to invoke this method.

package com.you;

public class Test {
  public static void main(final String[] args) throws Exception {
    Test t = null;
    t = Test.class.newInstance(); // by hardcoding the class name
    // OR
    t = Class.forName("com.you.Test").newInstance(); // load class from name
  }
}

If you need to pass in arguments when creating a new instance, grab the relevant constructor and then call `newInstance` on it.

package com.you;

public class Test {

  public Test(String name) {
    System.out.println("Called string constructor");
  }

  public static void main(final String[] args) throws Exception {
    Constructor<Test> con = Test.class.getConstructor(String.class);
    Test t = con.newInstance("your-name");
  }
}

> Thank you in advance

HTH :-)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Don't use JSP's for placing your business logic; use Servlets instead for your redirection purposes. Also, for performance reasons, before going in production make sure you replace the logic of grabbing connection directly from the DriverManager class with getting connection from a connection pool.

Coming to your code, you need to actually "execute" the SQL statement you created to get the file name. Read the JDBC tutorial on how to go about doing this.

A rough structure of your code should be something like:

// Servlet Class
public class ElearningServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
		Course course = createCourseFromRequest(req);
		ElearningDao dao = new ElearningJbdcDao(ConnectionUtils.getDatasource());
		String fileName = dao.getFileNameForCourse(course);
		// sanitize name for making it uri compatible if required
		response.sendRedirect("http://www.something.com/course/" + fileName);
	}
}
// Value object Course
public class Course {

	private String courseId;
	// getters and setters

	// other fields
}
// DAO interface
public interface ElearningDao {
	public String getFileNameForCourse(Course course);
}
// DAO implementation
public class ElearningJdbcDao {
	private Datasource;
	
	public ElearningJdbcDao(Connection connection) {
		this.connection = connection;
	}
	
	public String getFileNameForCourse(Course course) {
		String statement = "SELECT document_filename from x_masterlistofdocuments where course_id=?";
		Statement stmt = null;	// prepare statement using the connection
		// set the placeholder value using the `course` object using `course.getCourseId()`
		ResultSet rs = null;	// execute statement
		String name = null;	// read the name from the resultset
		return name;
	}
}

Of course, the example code above is …

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

> Surprised you didn't spot me standing next to Dani then!

I guess he meant that you don't remotely resemble a "chick" who is into programming. ;-)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

What do you mean by "doesn't work"? Exception? No result? Anyways, have you tried passing in an Object array as shown in the official documentation? Paste the code you are using.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

This is purely a client side issue and can be done in Javascript; read this. If you want to do it at the server side without any delay, look into the method HttpResponse#sendRedirect .

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

That and much more in the Java EE 5 tutorial.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I've already explained it in my previous post; also read this.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Did you try out the suggestion in my previous post?