JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can compare aobj with the possible JComponents that it could be, eg

if (aobj == myJTextField1) ...
else (aobj == myJTextField2) ...

When you know what the source is, you can cast aobj to the appropriate class, eg

if (aobj == myJTextField1) JTextField tf = (JTextField) aobj

or maybe all the possible sources are JTextFields anyway so you can start with

JTextField source = (JTextField) caretEvent.getSource();

Finally, depending on how many components share this listener, and how similar or dissimilar their processing is, it may be best to have a different listener for each component so you don't need to check what the source was.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, you made that clear with your first post.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, I don't have time to run your code today. What exactly do you mean by "but not working"? Specify what you expected to see and what you actually saw.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, just one map, declared as static if it's in in the new class I recommended. The important thing is the class, not the collection.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can simply use frame.add(pane);

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So you have multiple "datastrings" each of which has info1;info2;info3 values associated with it. Is that correct?
If so, definitely start with a small class that encapsulates a datastring and its associated infos. You can then put those objects into a static map with the datastring as key. That will get you off to a decent start.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
  1. Post your code in CODE tags so we can read it.
  2. frame.getContentPane().add(pane); the getContentPane() hasn't been required since Java 1.5
  3. Never override paint(), override paintComponent instead.
  4. Because your custom JPanel has no components in it Swing doesn't know how big it is, and probably has its size as 0,0 So the scroll pane won't do much. You may do better by setting your panel's size to a suitable value
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

YOu haven't givenus much to go on... eg what kind of processing and manipulation...
but one obvious option is to define a small class with fields for info1/2/3 or whatever and methods for the processing/manipulation

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Glad to help. Now if that answers your question please mark this thread "solved" (you'll see the link to click at the end of this thread) so everyone else knows not to try and solve it again.
ps: Next time please put your code in code tags (select it and click [code] just above the edit area) because that makes it much easier for us to read.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's the idea! Now maybe the next array will have numbers between 1000 and 2000 in it, so ideally you should start with a much bigger number, say 1000000.
Best of all, there's a constant in Java called Integer.MAX_VALUE which is the largest value an int can possibly have, so that's the very best starting value for min.

Average is sum/count, and you don't have a final value for sum until the loop has finished looping, so it makes no sense to calculate it inside the loop.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Well, that works, but it means you have to know what the lowest number is - which is what you are trying to find!
Look at the code for max - that starts out lower than anything in the array (zero), so it gets increased up to the highest value in the array. Now look at the code for min - that has to start out ...?... so that it gets decreased down to the lowest value in the array.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK. Here at Daniweb we always try to help you find the answer yourself rather than just giving it to you (that way you learn more and better).
Your problem is that you start out with min smaller than the smallest number in the array, so (min > grades) is always false. What do you have to set it to so that test is true, at least once?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, so what's the question? Calculation of the minimum perhaps? if (min > grades[i]) how often is that true when min is 0?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

setUndecorated(true);
(do this before making visible etc)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So, how would you remove a duplicated number in an array list without using "Collection.removeAll(Collection<?> c) " out of interest?

while (numbers.remove(new Integer(43)));

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
public static void getData(Scanner kbd, int height, int weight, int age) {
  System.out.println("Enter your height (in inches):");
  height = kbd.nextInt();

The variable "height" here is a parameter in the getData method. It's not the same thing as the other "height" that you declared earlier. It's a new separate variable. You get a value from the user and put it in the parameter height, but that "height" variable is discarded at the end of the method.
Basically, in Java, you can never change the value of a primitive (int/double/boolean) etc by passing it as a parameter.
If you just change the method to

public static void getData(Scanner kbd) {
  System.out.println("Enter your height (in inches):");
  height = kbd.nextInt();

then the variable "height" that you refer to will be the original one from the main program, which is what you want.

BUT

The original height variable is defined inside the method "main", so that's not accessible outside that method.
The variables like height that you want to share between your methods need to be declared inside the class, but outside any single method. Because the methods are static, you need to make the variables static as well

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think I've properly fixed them, but no way of telling until I complete the other methods test them

This is a very common mistake. In fact you can, and should, test methods as early and as often as possible. Waiting until all the code is "finished" makes it really hard to tell where things are going wrong - it could be anywhere. If you test methods as you go then any problems are most likely to be in the most recent method. Experienced programmers are constantly testing methods and small modules before putting them into the main program.
eg for your inputCurrency() method all you need to test it is:

public static void main(String[] args) {
   System.out.println(inputCurrency));
}

Just run that, enter some currency names (and some wrong ones), and see if it returns the right code values. Once you've debugged this method you can stop thinking about it and get on with the next one.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't understand what exactly you are trying to do, and judging by the lack of responses I expect nobody else does either!
Can you explain exactly what you mean by "show me 5 clock cycles"? Maybe a diagram or illustration of your desired output would help?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have

do {
   ...
while(Registrant == 0);

ie keep doing the loop as long as Registrant is 0
Can you see why that's wrong?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's just a perfectly normal method in the java.io.PrintStream class, just like print and println. It's just a bit newer, that's all. If you create a PrintStream explicitly you'll need to import it, but it's normally used via System.out

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There is no 'printf()' in Java.

Sorry, but yes there is. It was added in Java 1.5 This is from the API doc for PrintStream:

public PrintStream printf(String format, Object... args)

A convenience method to write a formatted string to this output stream using the specified format string and arguments.

JeffGrigg commented: Useful information. +7
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can't have a . in a name. Lab4.24 is not a valid name in Java. You could use Lab4_24 if you wanted.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

james : i cant use two dimension array cuz the number of digits in each line are different

I didn't say 2D array, because there's not really any such thing in Java. I said array of arrays, which is what Java has. The "inner" arrays don't have to be the same size. For example it's perfectly valid to have:

int[][] a = new int[3][]; // array of 3 arrays...
      a[0] = new int[1];        // first is 1 element
      a[1] = new int[5];        // second has 5 elements
      a[2] = new int[999];      // third is a lot bigger

so this is just what you need!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I won't around for the full 8 hours, but if you get started I'll try to support you with information and problem solving as far as I can (and so will others on DaniWeb, I'm sure), but you'll still have to do the real work.. Good luck!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If it crashed then you were doing something wrong - the whole of Swing relies on mouse listening, and its really solid code. Try again?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can create a Shape with those line segments, and use an AffineTransform in Graphics2D to rotate/shear/ etc
But 8 hours from now? Wow, that's going to be tough. Your teacher must be really challenging you to give you such short notice. (I assume, of course, that you haven't known about this for ages, but only just got round to it because the deadline was looming :icon_rolleyes: )

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Not that I know of - I think you just have to get down & dirty with MouseListener, MouseWheelListener, and MouseMotionListener

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe use an array of arrays? int[][] no where no[0] contains the array for the first line, no[1] contains the array for the second line etc

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
public String getText (int i)
public String getAnswer(int i)
(etc)

These are (correctly) instance methods - to call them you need an instance of Quiz, so it's a bad idea to have a parameter to identify which Quiz - in fact its completely unnecessary, just leave it out.
Eg instead of

public String getAnswer(int i)
{
  String answer = "";
  if (i == 0)
    answer = "extends";
  else(etc)
  return answer;
}

all you need is

public String getAnswer() {
   return ans:
}

Instead of the extra arrays for the user's answer and the correct/incorrect boolean, why not add an instance variable for the user's answer to the Quiz class, and have a boolean isCorrectAnswer() method that checks the user's answer against the correct answer and returns thre/false. That would make the final code much much simpler.

Finally, the biggest problem with this code is that you have included stuff about the individual answers into the code itself. That's a real no-no. If someone adds just one question to the questions file you;ll have to change your program. You have to remove every one of those details from the code and have a loop that just reads the file and processes each question one at at time, based only on what's in the file.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are referring to code that I have no access to, so I don't know what to say.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can use a simple for-each loop

for (Quiz q : questions) { // "for each quiz q in questions"
  // do something with q
}

ps: Why all those String.valueOf calls when the data is a String anyway?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Does anyone else believe that OP wrote that code in the 3 minutes between Norms' reply and his posting it?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, I don't understand your question.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's right.
static methods belong to the class, not to an instance, so you call them using the class, eg Account.consolidate(etc), not account4.consolidate(etc).
consolidate returns the new account, so you need to assign that returned value to an instance, eg
Account consolidatedAccount = Account.consolidate(acc1, acc2);

Your current code compiles because Java can find the static method from any instance of the class, but that's now a deprecated way of doing things. However, doing it that way doesn't assign the new account to account4

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just write a loop that reads two two lines on each iteration, and does nothing with one of them!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ArrayList <Quiz> questions = new ArrayList<Quiz>();

Every time you create a new Quiz, it has its own version of this ArrayList, so with n Quizes you have n ArrayLists, each with just one Quiz in it.

If you make it static, then there will be just one ArrayList that all the Quiz instances share, so all your new Quizes will go into that one ArrayList, and that will fix at least one of your problems!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In java you can only return one thing from a method. You can't return two arrays (unless you package both those arrays inside a single object and return that, which is probably a bit excessive for this case).
However, when someone passes an array into a method as a parameter, you can access, use, or update the elements of that array. And because you can have any number of parameters, you can update any number of arrays like that.
So create two arrays in main, pass them as parameters to your readArray method, and store all your states and capitals in the arrays you were passed.
Since main doesn't know how big the file is, you'll just have to create two arrays that are "big enough" and maybe have readArray return a single int that says how many states/capitals have been stored in them.

ps: Stultuske's post came in while I was typing. Between us we have given you two approaches, both equally valid. It probably doesn't matter which you chose, but you may find his easier as a beginner. In any case you should understand the stuff I was explaining about parameters and return values.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

new Portfolio[9]
Creates an array of 9 references to instances of class Portfolio. Initially all those references are null. That's why you get a Null Pointer Exception if you try to use them.
In theory the compiler could identify at least some of the cases where that could happen, but in general its just too hard.
What you need to do is to initialise each element of the array (reference) by giving it an actual instance of Portfolio to refer to.
In the simplest case that's just

for (int i = 0; i < c_port.length; i++) {
    c_port[i] = new Portfolio();
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, OK, but what are lines 32 and 33 doing there - the input is in the text field, not in System.in. Just delete those 2 lines and see what happens.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is the forum for the Java programming language, which isn't the same thing as the JavaScript scripting language.
You'll find the JavaScript forum here:
http://www.daniweb.com/web-development/javascript-dhtml-ajax/117

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you have a problem, please give us a complete description of it - full text of any error messages, or an exact description of what's wrong with the output.
Anyway... this is a GUI program, and on line 31 you get monthNumber from the field in your GUI - that's OK - but then you use a Scanner to replace that value with one from the console - that looks like a mistake.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, setIcon(icon);, or super(icon); as the first line of your constructor, gives the JLabel code access to your icon. Excellent!
I'm sorry if I made you work too hard for that, but I do believe you learn so much more when think right through to the solution yourself, and not just copy/paste something.
For the setBounds - you don't say what layout manager you are using, but you may need to call pack() on the parent to get the layout manager to use your new bounds.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK. No problem. :-)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@adarshcu
It's good that you are trying to contribute here. But did you read the previous post? Did your post add anything new?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, who said anything about the main class?
You extended JLabel.
In your constructor you store two of your own variables.
You don't call a JLabel constructor, so Java just calls its default (no-args) constructor for you - its like calling new JLabel() - a label with no text and no icon.
When Java wants to construct and paint the window, it calls various methods for your egLabel, and Java uses the methods you inherited from JLabel.
BUT
those methods you inherited from JLabel only know about the variables defined in JLabel. They know nothing about the variables you defined in egLabel, so they don't know about your icon, so they don't paint it.

How do you think you could set the icon that the inherited JLabel methods will use?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, and you pass that into your constructor, and store it in your variable, but all the code you inherit from JLabel knows nothing about that. When paintComponent is called the inherited implementation won't use your icon because it doesn't know about it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You haven't set any text or ImageIcon for your Label, so there's nothing to display. You have stored an icon variable, but how will the JLabel paintComponent method know about it?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The continue statement skips the current iteration of a for, while , or do-while loop. Since yu don't have any of those, it's not a valid use of continue.
Look at the second part of this tutorial
http://download.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The whole point of compareTo is that it compares your object with the one passed as a parameter, and tells you which is "lower" or "higher" or "the same" (those terms depend on what the data is, and how you chose to sort it).
Simply returning a value from one of the objects MUST be a mistake.

ps: @thins01 - the OP would learn more if you pointed out where his mistakes are, and let him think through how to fix them. Just providing a corrected program encourages copy/paste rather than true understanding.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's a hypertext link at the bottom of the thread that says something like "mark this thread solved". Just click it!