JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Norm's advice makes perefect sense. Alternatively, if you are going to be doing a lot of these drawings (eg animation) then you can create a new BufferedImage, get its Graphics, and draw your Shapes into that Graphics. Now you have your creature as a simple Image that you can draw with a single method call wherever/whenever you want.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Its essential that you fix the I/O formats in both directions before worrying about the factorial calculation.
Your algorithm overflows an int value with 54 as the input, and actually results in a zero result. You print '0', then read it as a byte - which is ASCII 48.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In this case i sent msg=6 but no is having 54 value.

That's what I was trying to explain. You print the value 6, which sends the character '6' to the output stream as an ASCII character. The ASCII code for '6' is the numeric value 54. At the other end you read a single binary byte from the stream, which gives you the numeric value 54.
Of couse that doesn't explain why your claculation seems to be wong, but it does explain why its using the wrong data.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's the problem: you are writing your data to the output stream as ASCII text, but you are reading from your input streams as raw bytes. It's essential that you read and write using the same format, eg if you print to the output then you should use readLine to read the text input, then convert the text to an int value if necessary.
Like Norm says - add more System.out.println statements to show the values of your variables as the code is executing - that will help you see where it's going wrong.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The standard Java GUI library is called "Swing", which is built on, and still uses many classes from, an earlier version called "AWT". Swing classes have names that begin with a J, as in JFrame, JLabel, JButton etc. Swing GUIs are portable across all platforms where Java runs.
SWT was developed (by IBM I believe) to work directly with the Windows APIs, so it gives a perfect Windows look & feel. Howver, for the same reason, it's not portable across platforms.
If you are learning Java then Swing is the standard.

vedro-compota commented: ++++++++ +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The keyword "new" is wrong here. You are just calling the static method "forName" in the Class class. It's not a constructor.
Class ourClass = Class.forName("com.app.something");

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You did compile it. Now you need to execute it. You fill find many many tutorials on the web on how to execute a Java applet.

empror9 commented: thank you :) +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Perhaps it's a task switch thing? You could try a sleep(1000) after taking the screenshot but before accessing the clipboard?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You may have "enough braces", but you don't have an equal number of { (five) and } (four).
Proper indntation would make the error immediately obvious.

Johannady2 commented: /* It's still giving me the same error. vowelConsonant2.java:30: error: reached end of file while parsing } ^ 1 error */ import java.lang.String; import java.io.*; import java.util.*; public class vowelConsonant2{ static Scanner scan = new Scanner(Syste +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Long is an integer type. Your Strings do not represent an integer. If you put the code in a try/catch block you will probably catch a NumberFormatException
Perhaps you confused long (integer) with double (floating point)?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you have lines like
System.out.println("The Start Date: " + getStartDate());
getStartDate is an instance method (ie not a static method) so you need an instance of the class for the method call, If your code was in an instance method then the current instance would be used, but main is a static method that (by definition) doesn't have a current instance. So you have to provide an instance explicitly, eg

Holiday hol = new Holiday(null, null, null, null);
hol.setStartDate("1/1/1990");  // not Holiday, setStartDate is not a static method
System.out.println("The Start Date: " + hol.getStartDate());
speakon commented: thanks +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This tutorial is about as simple as it gets:
http://www.tutorialspoint.com/java/java_overriding.htm

In your code you have made everything static - that means you can only have one value for the dates, price etc and those values are shared between all the holidays. That's obviously wrong. Every instance of Holiday should have its own dates. price etc. The only thing that should be static is the main method.

ps: You should be greatly honoured to get a post from Queen cscgal - it's incredibly rare for her to speak to one of us ordinary people!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have most of the required bits there, but just not quite in the right order! Here are some suggestions:
Keep an instance variable for fileName, so in your doSave method you can simply use that without the file dialog stuff on lines 17/18
If that fileName is not null or "" then save the file to that fileName, otherwize call doSaveAs
In doSaveAs, line 4 is wrong. You can't get the file name until after the dialog has been displayed.
In both methods, once you have got a valid file name, you need to call a method that takes that name, creates a File from it, and writes all your text to that file.

ps: awt was officially superceeded in 1998 by Swing. If awt is what your prof asks for then there's not much you can do about it, but be aware that in real life you will need to update your skills to the Swing classes that have been used for over a decade. Your prof should be retired, if not actually shot before he does any more damage.

Binisha commented: can u write n give it to me please as i have the project To submit n i m not getting anything right please help me +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

temp is the current node in the list
temp.next is a pointer to the next node
(temp.next).next is the next node's pointer to it's next node
eg If temp is node 1 then temp.next is node 2 and temp.next.next is node 3
In that example temp.next = temp.next.next; changes node 1's next node pointer to point to node 3, thus dropping node 2 from the list.

jhamill commented: wow that made it extremely clear. thanks! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Beware!

while (listPtr != null) {
    ...
    listPtr = listPtr.next;
}

When you get your list properly circular then this loop will never terminate, it just keeps going round and round the list forever. YOU need to terminate the loop when you get back to the start.

jhamill commented: I believe this is precisely the problem! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
catch (RuntimeException ex){
    }

You should never, and I do mean never, yes, really never never never ever do this in a new piece of code. If an error happens you will never see the error message and will never know what happened. Always start with an ex.printStackTrace(); in the catch block.

NormR1 commented: very good advice +13
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What's wrong with myLong * myLong ?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm not sure why he suggests regex here

There's no choice, the API doc for String split specifies that the param is a regex.

public String[] split(String regex)
Splits this string around matches of the given regular expression.

fortunately ',' isn't a metachar, so you can split on "," and it works.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you split on the comma each line will give you a two element array. The first element will be the name and second will be the price. It's a String array, so you will then need to parse the price from String to double (or whatever) - the Double class has a method to do that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's the code I used in a tutorial a while back to illustrate some of the techniques for exactly this kind of app. The lecture "notes" were all in my head, so the code is all there is. I'm posting it here in case it turns out to be useful for you. Don't try to use it directly, but you may find the coneceptsd it employs useful in your own design. I'm happy to answer any questions.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

/* Runnable demo of Java techniques for an electrical circuit design application
 * has NAND gates with connections etc and a drag'n'drop GUI
 * Main classes:
 * Component - abstract class for gates etc. Has inputs & outputs and a drawing method 
 *   NANDGate - concrete Component - you can create others for NOR etc
 * Connector - abstract class for inputs and outputs
 *   Input, Output - concrete Connectors
 * Connection - joins two Connectors (joins one Input to one Output)
 * 
 * These classes have the logic for GUI interaction (dragging, selecting Connectors etc)
 * but also have the potential for simulating the actual circuit behaviour.
 * 
 */

public class CircuitDesigner {

 // NAND gate drag'n'drop demo with connections etc

 JFrame frame = new JFrame("drag demo");

 ArrayList<Component> …
v3ga commented: Thankyou. This way is much easier than using java.awt.dnd +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

String s1 = "Hello!";
String s2 = "Hi!";
// s1 might '==' s2, but !s1.equals(s2).

No, in that example s1 cannot == s2.
The == tests for s1 referring to exactly the same object as s2, whereas equals tests for two Strings containing the same sequence of letters.
If s1 == s2 then by definition s1.equals(s2) and s2.equals(s1)
But if s1.equals(s2) then it does not imply that s1==s2 because they may refer to two different String objects that just happen to contain identical sequences of letters.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You overrode paint so you lost all the stuff that normally happens when a JFrame paints itself. Just start your method with a super.paint(g) to get all that stuff painted before you add your own drawing.

jackbauer24 commented: It works now, thanks! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have lost the original if tests on bgColor and textEditable. You need them both...

if (this is the disable button) {
    if (disableText) {
        disableText = false;
        inResult.setEditable(true);
    else {
        disableText = true;
        inResult.setEditable(false)
    }
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Well. exactly like that! The parameter is a perfectly ordinary array of Strings that you can access or index just like any other array.
Eg

for (int i = 0; i < arg.length; i++) {
    // do stuff with arg[i]
}

or

for (String s : arg) {
    // do stuff with s
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe it's just me having a "senior moment", but we seem to have lost the facility where you could hover the mouse over a topic's title on the forum listing page and see the first line or two of the post. I used this all the time to get a quick triage of which posts to read in full. Has this feature been dropped? Is it there but I'm missing some obvious point?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your diagnosis sounds probable to me.
Rather than using invokeLater in the constructor, why not move that up to startGame() so all the startup code runs on the same (swing) thread?

DavidKroukamp commented: Thank you James, that was spot on +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi kolibrizas
Your help, advice, and assistance are very welcome here. But please don't just post solutions to people's homework for them to copy & paste. All they learn from that is how to cheat. Give them guidance that allows them to learn how to do it for themselves. Thanks.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your loop calls move() to update positions etc, but Swing has no reason to update the screen. Call repaint() on your main window to request a screen refresh (which will use your latest values).
Your paint method seems to be doing its own double buffering? If so, no need, because Swing does its own double buffering anyway.
http://java.sun.com/products/jfc/tsc/articles/painting/

You use a thread and a loop to control animation - this certainly can be done, but you will avoid all kind of thread-related problems if you use a javax.swing.Timer to call your move() method at regular intervals. ps 5 mSec (200 moves/sec) is probably too fast. The eye isn't going to see anything faster than 50/60 mSec intervals, even if your program can refresh that fast.

kolibrizas commented: Very decent approach! +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. Declare and initialise your ArrayList as as ArrayList of Games to avoid casting and possible errors ArrayList<Game> theDB = new ArrayList<Game>(); 2. htScore is already an int, so there's no need to use Integer.parseInt - that's just for where the data is in a String. homesum += theDB.get(i).getHtScore(); where getHtScore() is a public accessor method that returns the value of htScore.

3. No need to keep a counter, you can use theDB.size()

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

int[] myParrot = {1,2,3}; // simpler

You can use Random to generate random numbers. I donlt kniw what you mean by "a given set of numbers"

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't explain the logic, but the public method is the one that gets called initially (that's why it's public), and the private method is the "helper" (being private it can only be called from within the class).
There's no reason why a recursive method can't be void. Recursive just means that the method calls itself.
Two method declarations may have the same name, but they have different parameters, so they are quite different methods. In Java a method's "signature" is its name plus its parameters. Names may be duplicated within a class, but signatures cannot be duplicated. The scope (public vs private) and the return type are not part of the signature.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, so
1. Since the map only contains references, never Objects, your approach is based on a misunderstanding. You cannot store an object in a hashmap. If you try to add the same object twice you will always get two references to the same object.
2. The JVM won't use all available 8Meg by default - you have to play with the memory options on the java or javaw command to allocate very large amounts.
3. If this is really a problem then maybe look at alternative structures such as having a simple array of URLs and using the hashmap to store arrays of ints as its values, where the ints are indexes into the URL array. My guess, however, is that you won't gain much (except complexity).
Remember - don't confuse objects with references in Java.

Arpssss commented: 10 +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use a javax.swing.Timer to execute an actionPerformed method every (eg) 60 millisecs. In each execution of the method increase the size by a small amount (eg 4 pixels). When it reaches the desired size, stop the Timer.
Doc and examples for Swing Timer in all the usual places.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, you just need to enhance them a bit to include formatting info. When you print to your printstream you need to use the printf (formatted print) method rather than the ordinary print method. Then you add one new parameter, which is a format specification. Eg

print(name); // prints the contents of name without any formatting
printf("%-8s", name);  //prints the contents of name left aligned in a column 8 chars wide

That example format works like this:
% this is a format item
- left-aligned
8 column width
s string format

Just try executing these two lines to see it working in pratice:

System.out.printf("|%-8s|%5s|\n","hello", "me");
      System.out.printf("|%-8s|%5s|\n","goodbye", "you");

Google java printf for lots more examples

ps You don't need to create new PrintStream for every print, just create one at the start and use it for all the print/printf calls.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have missed a semicolon on line 143.

(No, not really, but what on earth do you think anyone is supposed to do with anything as unhelpful as "Still not working"?)

stultuske commented: aaah ... well placed sarcasm :) +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sounds like OP may be using null layout (?) - in which case the answer is to use a proper layout manager and a cross platform L&F like Nimbus or whatever you prefer.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In Java there are primitives (int, char, float, byte, boolean) and there are Objects.
A HashTable can only contain references to Objects. No primitives and no Objects, just references to Objects. If you try to put an int into a HashTable the compiler automatically creates an Integer Object for you (that's an optimised ptocess b.t.w.) and puts a reference to that Object into the HashTable. If you put the same Object multiple times into a HashTable the table will just contain multiple references to that one Object.

The space taken by a reference is something private to the particular JVM you are running on at the time. There is no reason to think that a reference on a cellphone JVM will be the same size as a reference on a 64 bit server JVM.

Is this a just-for-interest question, or do yo have an actual application where the memory usage of a HashTable is an issue?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Example:

Object[] myArrayOfObjects = new Object[2];
myArrayOfObjects[0] = "Hello";
myArrayOfObjects[1] = new Integer(42);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why not just try to connect to the server and see if that works?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why is your forst question a problem? Your code counts 5, does something random, counts on up to 7 then starts again. How is that different from what you want?


Which value do you want the user to enter?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

carProcess is an inner class of roundabout01 (ps - please use standard Java capitalisation to make your code easier to understand). It's not declared static, so just like any other instance member of roundabout01 you need an instance of roundabout01 to qualify any reference to carProcess. Read this next:
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Unless your instances of carProcess need access to instance variables from roundabout01 then you can probably just declare carProcess as a static class.

So,, moving on...
You have screwed up your run/start solution. You are trying to create a new Thread with your carProcess class. You were right to define a run() method in the class, so change that back again. The start() method is defined in the Thread class. If you want carProcess to be a thread then declare it as extends Thread and it will inherit the necessary method(s). Then when you call start() the method inherited from Thread will start a new Thread, and call your run() method on that new Thread.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have detailed instructions. Why not follow them? Start at the beginning:
1. Ask the user to enter the number of points.
Then go on to step 2...
2. Ask the user to enter x and y positions for each point. Store the positions in a 2-D array. What should the dimensions of the 2-D array be?
etc.

ps: you get zero as the distance because x1, x2, y1 and y2 are all equal to 2, but don't worry about that because when you follow the instructions you won't use that code anyway.

bibiki commented: undoing the downvote +5
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Create two panels, one with the four buttons and a separate panel with the label. Then add both panels to your main panel. This allows you to use different layout managers in each panel to get the exact result you want.

Alternatively, you could use a GridBagLayout to do the whole thing, but the learning curve is quite steep.

mKorbel commented: great minds thinks alike !!!, I didn't saw your answer :-) +11
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Two small suggestions... new ImageIcon("file name") is certainly convenient, but it throws no Exceptions if it fails, thus leading to confusing errors later on when you try to use the image. It's a good idea to check the resulting Image, eg im.getWidth(null)>0 and issue a diagnostic if necessary

If the frame is resizable you may prefer to scale the image to fit the frame by using im.getScaledInstance using the panel's current width and height.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You declare im and frame inside the constructor ImageBackGround(), so they can't be accessed from anywhere else. If you want them available to all the methods of that class then declare them in the class but outside any one method.

jackbauer24 commented: You almost solved the whole issue! +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can set a compositing rule then draw the second icon with an alpha between 0.0 (totally transparent (invisible)) and 1.0 (not transparent at all (opaque))

Graphics2D g2d = (Graphics2D) g;
// draw image 1
composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(composite);
// draw image 2

Have a look at the API doc for AlphaComposite

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't confuse variables and objects. A variable is just a reference (pointer),, not an object. You can't "declare an instance". You can only declare variables or create new instances. int[] arr; declares a reference variable, initially containing a null pointer new int[99] creates an array of 99 ints int[] arr = new int[99]; creates an array and sets the variable arr to refer to it arr = null means that arr no longer refers to the array. If that was the last remaining reference to that array then the array will be garbage collected at some time later. That's a function of GC that you do not control. There is no concept of "destroying" an object in Java. for (int i=0;i<=somearray.length;i++) somearray[i]=0; resets all the elements of the array to zero.

Whether you clear all the elements back to zero, or dereference the array then create a new one is your choice. In practice it won't make the slightest difference unless the array is so large (millions of elements) that you are running short of memory.