Add an element to a separately linked list in Java

I'm implementing a singly linked list in Java. What I don't like about this code is that I need to check if ( == null) every time I add an element. But the condition is met only once, when adding the first element. Is there a way to implemen

Should I "new" an object inside or outside a loop?

Comparing test1() and test2() in the following code: class Test{ //max value is a large number int max_value = 100*1000; public void test1(){ for(int i=0; i<max_value;){ StringBuilder builder = new StringBuilder(); builder.append("String value&quo

How JUnit test ArrayList.remove ()

Hi I am trying to test a remove method of ArrayList in JUnit: @Test public void testRemoveCommentValidIndex() { item.removeComment(2); assertEquals("Remove the comment", 2, item.getNumberOfComments()); } so I just check that the size of the Arra

Create a base class object from a derived class

I have the following classes: public class Base{ //fields public String getStr(){ String str = null; //Getting str from the fields return str; } } public class Derived extends Base{ //fields //some other fileds public String getStr(){ String str = nu

Launch an exception or prevent it?

The question is if it is prefered to Throw an exception or to prevent it from ever occurring. It is for a game project. IndexOutOfBoundsException vs. coding around it. I have a List<E> private Attribute<List> attributes; A method to get an ite

Draw a car wheel

I have a very simple problem. I am learning Java, and was given an assignment to draw a car. I did this all in one class that extends JPanel, and did the drawing within paintComponent(). I realize this is poor object-oriented programming, and decided

Why do not you hesitate on a higher level class or interface?

This question already has an answer here: Why can't enums be declared locally in a method? 5 answers I am studying Java and wrote a very simple program. In it I can put enums in the top most level, but not inside a method. The way I see it, enums are

Java Hashcode gives integer overflow

Background information: In my project I'm applying Reinforcement Learning (RL) to the Mario domain. For my state representation I chose to use a hashtable with custom objects as keys. My custom objects are immutable and have overwritten the .equals()

Days difference in dates in java giving poor performance

Hi i am trying to calculate difference of two dates in days. The dates are 14 of this month and today. It should be 2. But o/p is 0 always. Code: long today = (new java.util.Date().getTime()); long difference =(long) (today - 1394809789186.0); long d

How do I access the system properties in the Thymeleaf model?

I need to access system properties in a Thymeleaf template. It would be nice if this was possible so that I don't have to populate the spring mvc model explicitly with properties. I'm trying to use SPEL for this purpose but it's not working. <h2 th:t

Delete the parentheses in a sentence and separate the string

So.. if I got a string such as: (P=>Q) ^ (L ^ M => P) ^ A or ((~B V P) V M) ^ (B V ~P) ^ (B V ~M) How can I remove these parentheses and keep the substrings I wish in the form: P=>Q, L^M=>P, A and ~BVPVM, BV~P, BV~M Thanks!It appears you're wr

First attempt at a simple GUI

I am attempting to teach myself how to make a GUI using Java swing and Window Builder Pro, after watching several youtube videos, and reading some tutorials I have accomplished the following. import javax.swing.JFrame; import javax.swing.JTabbedPane;

What is a real use case for @Stateless on @Singleton in EJB

if I understand EJB correctly, @Singleton is actually the same as Singleton in plain Java and also singleton in spring -> one instance, every call goes through the same instance concurrently. @Stateless declares a bean, that could (but must not) have

Grizzly and ServletContainerContext

I'm trying to get hold of some injected context (for example Session or HttpServletRequest) in a Servlet I've written, running on Grizzly, but nothing I do seems to work. The whole process seems to stall rather prematurely with the following error: S

How do I have a background image resize in a mistletoe?

So im making a gui, and i have a background image for it. i dont know how to make it set as the background, so any help with that would be good. an explination would also be good. also, after we get that image as a background, how do we get the image

Java - How to access the highlighted material in a JTextArea

I have a JTextArea and I have a menu which allows you to cut, copy and paste information through the cut(), copy() and paste() methods. However, I want these methods to get the highlighted text from the text area and use that in the methods. Is this

Enum in a statement

This isn't a matter of me being stuck, but rather I'm looking for a tidy way to write my code. Essentially, I'm writing an event driven application. The user triggers an event, the event gets sent to the appropriate objects, and the objects handle th

Static variable over multiple subclasses - corrected

I was wondering what happened if I define a base Activity object with all my activities as subclasses of that. Then I declare a static variable in the base class, will all the subclasses use the SAME static or will there be one per subclass. For exam

Compare two html files

Hi I want to compare code of two HTML files. How do I achieve this?If you can use a tool; there are a lot of compare-tools available like UltraCompare. But if you want to build it yourself; please let us know. Edit: There is a Java library available

Why is the rendering of Java Swing html fonts so nasty?

To display a complex multi-line label with multiple styling I use Swing's html rendering capabilities and end up with really crappy font rendering. I have tried to explicitly set the font family - infoLabel.setText("<html><span style='font-f

How to modify the value of an attribute of a bean in the list?

I am having a list of bean, now i want to change the value of an attribute of all the beans in the list. For example: class Person{ String name; int age; String attrXYZ; /* accessors and mutators */ } List<Person> lstPerson = getAllPersons(); //set

Unable to instantiate the HttpClient type

I have added the .jars to my library and I can also instantiate other classes in the JAR. What gives? I've tried to clean the project etc. It's a compiler-time error. Eclipse won't let me instantiate it. I'm importing the correct library, not the sun

Axis Security Header

Hi trying to generate a security header in a Java Axis2 Client program in the format of. <soapenv:Header> <wsse:Security xmlns:wsse="> <wsse:UsernameToken> <wsse:Username>myUsername&

Sax Analyzer: ignorant HTML

I am using the sax parser to parse a XML file. It works fine, but I don't want to parse the content of an <info> tag as it contains HTML which I want to save to a string. Can anyone tell me is there any way to go about doing this?. ThanksThough ques