JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I agree with what I think mKorbel is saying... much better to ORDER the records in your SELECT so they are sorted before you add them to the combo box

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe you can do it by coding your own ListModel then you can maintain the set of values sorted in alpha order. There are a number of list model classes that you could subclass (eg AbstractListModel) to handle the housekeeping

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is one of those irrelevant technical questions that interviewers like to ask, but don't ever matter in practice:

String s1 = "asdf";
String s2 = "asdf";

the compiler is smart enough to create just one String object.

String s1 = "asdf";
String s2 = new String("asdf");

the compiler will create two String objects.

Bercause Strings are immutable there is no reason I know of to use the second version.

kenadams commented: Okayyy, I think your answer just removed my mist. So in the normal case, we would most likely use String s1 ="asdf", right? +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

items.add will only add an object of the Item class because that's how the list is defined. There is not, and has never been, and add method for lists that takes two parameters String and int - check the API documentation.
Of course it can't find "weight" - you have no declaration for that variable now.
Please take a little more time to think about your code before posting "what's wrong with my code" threads.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are reading from the "get" file while trying to control it with the Scanner's hasNext(). Because you never read from the Scanner it always hasNext - this is completely separate from what you are doing with the "get" file.

ps: Please don't leave old threads open for new questions. You should have marked this thread "solved" when you got your answer a month ago, and started a new thread for this question.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You're doing very well for 14! And the best way to keep improving is to write this yourself. It's harder, yes, but that's how you learn. Keep trying and people here will help.
Here's a good tutorial on how to use a Timer
http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
it focusses on repeating events (eg updating an animation), but in your case you will use setRepeats(false) so the Timer just runs and fires once.

I'll be stopping for the night very soon, but there will be others here to help.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Its better...
inside the method you need to add that Item to the items list

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread".
That means that once your actionPerformed method starts NOTHING else will happen in Swing, including no screen updates, until your method finishes. You can update stuff and loop and sleep as much as you like, but none of that will affect what's on the screen until your method has terminated.
If you want to do stuff after a period of time, here is the right way:
In your actionPerformed start a javax.swing.Timer, and return. When the timer fires you can update whatever needs updating and return. While the timer is running Swing will be free to update the screen.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sorry, my remote mind reader is offline today, otherwize I would have immediately known about that. :)

In that case just straight to the last line of my previous post

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It would help if you explained what values you expect and exactly what "incorrect" values you are getting.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You haven't done anything to implement an Items class. That would be some code that begins

class Item {
   // variables for description and weight
   // contructor
   // etc
}

Then your addItem method should accept an Item as its parameter, so you can add that to the ArrayList.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In the code you posted above the inputs are NOT stored in the array.
The array salaries is created on line 9 and its values are used on line 25, but there is no code that puts the values into it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

see my previous posts - did you store the data into the array?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's still not enough information. If the result is wrong: (1) what is it? (2) what should it be?
ps: see my previous post - did you store the data into the array?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"Didn't work" - you mean the program wouldn't execute? Please be very specific about what errors you are getting (eg complete error messages or actual vs expected results).

That latest code looks right now. I suspect you are getting zero as the result? Maybe becuase you never actually store any data in the array?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 25 you divide the running total of "Var" by the number of salaries on every pass of the loop. I think that's wtrong - you should compute the sum then divide by the number of salaries just once at the end.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Eclipse has "content assist" and "autocomplete" that do things like listing valid method names when you start to program a method call, or providing shortcuts for common text like System.out.println(
Just Google those terms for details.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm no SQL expert, but it's my understanding that SQL tables are sets, with no physical or intrinsic ordering that you can use or rely on. You can only specify a sort order when retrieving records. Maybe someone in our database forum can be more helpful.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The best way to test code is to test it ;)
If you haven't got a disposable re-usable re-creatable test database then now's the time to create one. (Write a script to create and populate it, so you can restore/reset it to a known state before each test run)

Apart from that...
You create a new database connection (expensive!) every time. If you are going to run this frequently then its probably better to create just one connection and re-use it. Ditto prepared SQL statements
You insert the user's text into your SQL statements without any validation at all. Suppose the user has a quote character in their text. Suppose they are smart enough to have text that terminates your SQL command and does something malicious? Much better to get the text from the input fields and perform some minimal validation before allowing it near your database.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

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

There is somewhere on Oracles site a table that shows which components generate which events, but I can't remember where exactly. Otherwise, look at the addXXXListener methods for your component (don't forget to look for inherited methods as well)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The key to creating frames in specified formats is to chose the right layout manager. This tutorial is a good start.

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are 99% of the way there.
It's really hard as a beginner to think about the logic and the Java code at the same time! Step back from the computer and just run through that on a sheet of paper. You'll soon see why its going wrong, and how it should work. Then (and only then) go back to the computer and fix the code to give the right logic.
(ps that's what the most experienced programmers do when they hit a logic problem, so don't be embarassed!)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... or maybe just

   return allCards.values().toArray(new Card[0]);

... why write code when its already in the API?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A word of caution - not all console devices and/or OS's support ANSI escape sequences, so if this works for you that's great, but don't assume it's portable or will work on someone else's computer. In general your chances are much better under Linux than under Windows.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, as soon as it hits the return statemnent it returns, so you only ever get the first Card.
Your requirenent seems bizarre - you want to return multiple Cards, but not use an array or Collection? Given that, all I can think of is to return an Iterator or ListIterator that will allow the calling program to iterate through the Cards, but frankly an array would be easier for everybody.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I was playing around with this problem for fun today, and it's interesting just how simple it can be. Here, just for your amusement is a 3 line method that, given a URL of a web page (supplied as a String), returns the HTML for that page, also as a single String...

    // reads and returns the HTML text from the specified URL 
    // eg "http://docs.oracle.com/javase/7/docs"

    static String readHTMLfrom(String urlString) throws IOException {
        URLConnection urlConnection = new URL(urlString).openConnection();
        InputStream in = urlConnection.getInputStream();
        return new Scanner(in,"UTF-8").useDelimiter("\\A").next();
    }

The use of a Scanner to copy a whole input stream to a String is a favourite little shortcut - the secret is in the definition of the "\A" Pattern used as the delimiter ;)

After that it's a metter of parsing the HTML for the data you need - maybe you can do this in an ad-hoc "indexOf" kind of way, or maybe you will need to use a full XML parser. It all depends on the exact design of the web page.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This sourceforge project may help, or this blog has a simple DIY example.

ps: Just because info is on a web page that does not mean that you are free to use it any way you like. Please check that your use does not violate anyone's intellectual property rights.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just trying things at random is a v e r y slow way to write a correct program. I still recommend that you do some examples on paper and get the logic 100% clear in your own head before going anywhere near a computer.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK.
Line 1: incorrect syntax
Line 2: what's that if test for? Certainly violates the spec
Line 3: what's the point of an unused variable?
Line 4: assuming that the single delete is working perfectly, this line is OK.

Misc: You didn't verify index/count as required.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That does not check the index value as required in your spec, and overwrites currentIndex to point at some arbitrary location regardless of what its previous value may have been. See my reply to your other post for suggestions on how to get this logic right.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Based on what you have posted before, the first version (delete one element) is not yet working - it doesn't check the index and it doesn't maintain the currentIndex variable correctly. Maybe you should be certain that is fixed before moving on?

Anyway, I don't want to be discouraging, but here you posted four lines of executable code, none of which makes sense. Your problem seems to be with the problem's logic rather than with Java syntax - in which case I strongly recommend that you move away from your computer and work through some examples on a piece of paper. Start with an array of values and the currentIndex variable, chose one to delete, and work out and write down the correct final values. Do this a few times and study how you are doing it. That's the logic you then need to code in Java. You can also use those as test cases to check your final code.
(Working throiugh an algorithm on paper first isn't just a novice thing... the more experienced the programmer the more likely they are to use techniques like that!)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No. Once you have used an interator you have to get a new one for the next loop. It's just one tiny line of code to copy/paste, so it's not a problem.

You could forget iterators, and use an enhanced for loop like

for (Publisher p : publisherList) { // for each Publisher in publisherList
    System.out.println(p);
}
aslam.junaid786 commented: Thanks Sir. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

does this part update it?

That updates the existing array to overwrite the "deleted" entry. Eg if the array was {2,4,6,8} and you delete index 1 the array will now be {2,6,8,8} - ie the correct entry has been deleted, but you now have a "spare" entry at the end. There's also a problem with the currentObject variable - with the example above, if currentObject was 0 that's OK, if it was 2 or 3 it will now be referring to the wrong entry because they have moved. And what if it was 1 and you just deleted that?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

update current object - depends on whether it was one of the objects you moved in that loop

you can't "compress" an array. If your array has unused space at the end you can copy it to a new smaller array and let the old array get garbage collected

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You get a list iterator (line 16) then use that to iterate thru the list (lines 20/21). Then you go straight on to try to iterate some more (lines 27/28) using the same iterator. Butyou have already used that iterator so it has no entries left.
You need to get a new iterator each time you want to start a new iteration loop.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thanks for sharing your final solution. I had a quick scan thru the code and can't see anything objectionable, so well done. (Rather than static methods I would generally prefer to see an instance-based implemention, but in this case it really doesn't matter.)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You probably wanted to AND those conditions together to see if the letter was not a vowel - just take a couple of examples and run them through it in your head or on paper to see how it works.

Alternatively, since you ask, the String class has a useful contains method that tells you if the string contains a specified substring. eg "ABC".contains("A") returns true, but "ABC".contains("D") returns false. ;)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
if ( letter != 'i' || letter != 'o' ...

There's a problem.
If the letter is 'i' then letter != 'o' is true
If the letter is 'o' then letter != 'i' is true
If the letter is anything else then both are true.
You OR them together, so the result is always true for all possible values of letter

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

One way is to have a boolean matchFound = false When you find match in the file you set that to true. Then at line 49 you can test the boolean to see if you need to add the user.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unless I missed something it looks like your init() methods creates the threads and makes them eligible for execution, but then on lines 12-16 you immediately print the contents of consumerData. At that point there is absolutely no guarantee that the other threads will have executed anything. Creating and starting a thread makes it eligible for execution, but it's up to the system to determine when they get allocated any CPU time. My guess is that you are printing the data before either other thread has executed.
Ideally you should use some kind of semaphore or wait/notify synchronisation to ensure the producer and consumer threads have executed before printing the results - a CountDownLatch would be ideal.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In Java a class can diectly extend one superclass, and implement any number of interfaces.

Java's "extend a superclass" is called "generalisation" in UML. The UML representation of a Generalization is a hollow triangle shape on the superclass end of a solid line or lines that connect it to one or more subclasses.

Java's "implementing an interface" is called "realisation" in UML. The UML representation of a Realization is a hollow triangle shape on the interface end of a dashed line or lines that connect it to one or more implementers.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you showed us the code and told us what the error messages were, then maybe we could help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sound advice Seldar, but perhaps you could now explain for Andrew's benefit why he had that error and why that fixes it?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Check out the % (remainder) operator - gives you the remainder after dividing, eg
27/6 = 4
27%6 = 3
ie 27 is 6*4 + 3

or

secs = 140;
secs/60 = 2 (minutes)
secs%60 = 20 (seconds)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Normally I would say " go ahead and test it, you'll soons ee if it's ok". But in this case you have a couple of catastrphic mistakes that will prevent any useful testing. You have 2 methods that look something like

int doIt() {
   return doIt();
}

These will call themselves recursively until you get a stack overflow.

Normally methods like that simply return a (private) local variable, or some result derived from local variables.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's platform-independent unless you deliberately include something that depends ona particular platform (eg accessing the Windows registry).

Java is not quite "pure" OO, it has primitive variables (int, boolean etc) which are not objects.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is so frutrating! I know that you know how to do this, but it seems I'm going to have to break my own rules and spell out the code...

to push an item something onto a Stack stack1 the code is

stack1.push(something);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Looks to me like that value is a valid integer, but its not a valid int because it's too big, >= 2^32. You could use a long instead of an int.