JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

main is a static method - it runs without any instance of NumberCheck.
But in3050 is an instance method, so it needs an instance of NumberCheck to run.
You can either
make in3050 static (quick fix, but heading in the wrong direction for understanding object oriented proramming)
or
in main, create a new instance of NumberCheck, and use that to call in3050 (whic is how things usually go in an object oriented program)

ps: next time you post a problem, include a complete copy of the compiler or runtime error message(s) so we don't have to guess what the problem is.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It depends on how you want to arrange your fields, and how you want that to adapt when the user resizes the window, or you run on a different computer...

I suggest you start with the simplest layout manager possible - you can always go back later and replace it with a more sophisticated manager.

Here a good guide that will help you pick an appropriate one...
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

(one thing to remember is that you can "nest" layout managers by using JPanels - eg you may start with a BorderLayout for headings, content and buttons at the bottom. For the buttons you have a JPanel as the bottom component and place all the buttons along it using a FlowLayout)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The big problem here is that you have a null LayoutMamager. This is a very bad idea because when you run your program on a different computer (different screen resolution, different systen fonts) your controls will all be the wrong size.
It's also a bad idea because your JFrame has no control over the size or placement of any of its contents, so it doesn't have the info that scroll pane needs to scroll properly.
Also you are adding your controls to the JFrame, not the scroll pane.

Here's what you need to do:
create a JPanel with a sensible layout manager and add all your controls to that, and pack() it - do NOT use null manager or setBounds()!
create a scroll pane with your JPanel as its contents
add the scroill pane to your JFrame.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"doesn't run" implies a run-time error or exception (array index out of bounds, maybe?). Next time please post the complete text of any error messages.
This one looks like the classis Scanner "gotcha" - nextInt followed by NextLine
nextLine leaves the scanner pointing just after the int, ie just before the following newline character, then nextLine takes eveything up to the newline, which is a zero-length String.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

hyp is a String but you are trying to set its value to a char

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

depends on how you are doing it - do you override paintComponent or is it a JLabel with an ImageIcon?
If it's paintComponent then just draw the image at (panel width - image with)/2, (panel height - image height)/2. Your paintComponent will be called after any resize and will always center the image.
If it's a JLabel then it also depends on the layout manager...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The proxy class has just a ref to the real chunk (in addition to the inherited vars - which I just edited because the linked chunks var isn't needed execpt in a real chunk), so thats one ref and one ID (int?), so it's about as small as a class instance can be. You should be OK for many millions of those at least.

The mechanism you are describing seems pretty complex to code, and harder to test/validate fully, but only you will know if the application justifies that investment. I was suggesting a scheme that you can implement very easily and at very low risk, without changing a single line of code that uses Chunks. Anyway, I hope it gave you some interesting options to consider!
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm also just back from a trip, so good timing!

Unique object IDs for database use is a standard problem with many standard solutions - just Google.

Probably the easiest way to explain the proxy idea is with some (pseudo) code - but remember this hasn't been in any way optimised for your (unpublished) complete requirement...

    abstract class Chunk { 
       int ID;
       ...
       abstract public void doStuff();
       ...
    }

    class RealChunk extends Chunk {
       // this is a real chunk that is fully populated with data
       ArrayList<Chunk> chunksThisLinksTo = ... // could be real or just proxies
       ...
       public RealChunk(int ID) {
          // load chunk data from database
          // populate chunksThisLinksTo with new ChunkProxies
       )
       public void doStuff() { ...
       ...
    }

    class ChunkProxy extends Chunk {
       private RealChunk realChunk = null; // the chunk this is a proxy for

       public ChunkProxy(int ID) {
          this.ID = ID:
       }

       public void doStuff() { 
          if (realChunk == null) loadRealChunk();
          realChunk.doStuff();   // proxy the calls
       }
       ...

       void loadRealChunk() {
          // may need to free memory by unloading a real chunk...
          // ... just set its proxy realChuck variable to null and let GC do the rest
          // (implies you maintain a list of proxies whose realChunks are in memory)
          realChunk =  new RealChunk(ID);
          (list of proxies whose realChunks are in memory).add(this);
      }
   }

So now when you are loading Chunks from the database you create create ChunkProxies for all the chunksThisLinksTo values. They just sit there until the program …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Since the command window was open before the java program started I think it unlikely that the Java program can close it. Hence my previous suggestion of wrapping it ina two-line batch file. However, if you are using VB to manage the startup presumably you can use that to exit the command prompt.

Looking wider, there's no point putting too much effort into this. Nobody actually uses the console for user interaction in real-life Java, and if you are using a real GUI you won't need the command window/console in the first place.

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

What's the complete sequence of events? Do you open a command window first, then run java - jar ... and want the command window to close when the app finishes?
... in which case maybe a small batch file that runs your app then has an exit command?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It depends on yout layout manager. You may need to call SetPreferredSize, setMinimumSize, setMaximumSize and/or setSize before calling pack. But is does work.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Instead fo taking the standrad "exit on close" option for your JFrame, listen for the close event and use that to call the moethods to shut down your app properly

   addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
              etc etc
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

== tests for two expressions representing exactly the same object. Which yours are not!
To test if two string objects contain the same sequence of characters, use
string1.equals(string2)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thank you so much for sharing that.
Java 7 has the nio ("new i/o") package that is a modern replacement for the old File class, and apparently has proper built-in support for junction points. That's where your Files.isReadable method comes from. Presumably the ideal solution would be upgrade this whole thing to nio and use the FileVistor interface to walk the tree.

http://docs.oracle.com/javase/tutorial/essential/io/fileio.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's really interesting - I'm sure a number of people here (including me) will want to know exactly how they appear for isDirectory() and listFiles() - is it the case that isDirectory() returns false, but listFiles() still returns null?
Please share you findings (and solution) with us.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What's the exact syntax of the java.exe commmand that you are trying?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why not keep it really simple and use Oracle'sfully-supported standard JavaDB (a version of Apache Derby) that you already have as part of any recent JDK installation?
http://www.oracle.com/technetwork/java/javadb/overview/index.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It works just fine in Java 7, but there are a number of calls that are needed. Something like this code from an old project's splash screen (maybe some redundancy - it start life under the temporary transparency support in Java 6.21)...

frame.setUndecorated(true); // gets rid of borders title bar etc (call before JFrame is visible)
frame.setBackground(new Color(0, 0, 0, 0)); // frame is transparent    
frame.getRootPane().setOpaque(false); // frame content (rootpane) is transparent...
frame.setOpacity(0.0f); // or maybe this achieves the same thing

JLabel iconLabel = new JLabel(); // use JLabel ImageIcon to paint image
URL imgURL = getClass().getResource("/images/xxx.png"); // transparent png
iconLabel.setIcon(new ImageIcon(imgURL))
frame.add(iconLabel;
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Debug this by starting at the line where you get the NPE and print the variables there to see which is null, then work back to see how that happened.
From the incomplete info you gave it looks like you have called allFiles() on somethingthat's not a directory, but your code seems to preclude that possibility. You print which files and directories you are processing, so what's different about the dir you are processing when you get the NPE?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's not a sensible value to initialise your factorial. Eg, if n=3 your factorial will be
312*3

Why not take a minute or two to debug that method fully (send it some simple values for n and print what it returns) before you try to debug other code that depends on it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your factorial method doesn't initialise the variable "factorial", so each time you call it factorial will keep getting bigger and bigger. That variable should be a local var in the method, initialised at the start of the method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Update: I just tried a trvial example in Eclipse, and the destroy method is called when you close the applet viewer via its red X. Here's the code I used

import javax.swing.*;

public class AppletTest extends JApplet {

   @Override
   public void init() {
      add(new JLabel("Hello World"));
    }

    @Override
    public void destroy() {
       System.out.println("Destroyed");
    }

}

So the question now is: what are you doing in your situation which stops it working relative to that simplest case?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I suspect the Eclipse applet runner is just hard terminating the app when you click x?. Try running it as a normal applet in a browser... if your @Override is not giving an error then destroy should definitely be called when the applet terminates.
Your best strategy may be to open the stream in the start() method and close it in stop(), thus not leaving it open when the user is doing something else in his browser.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1 depends on how often that will happen, but probably no.
2 how does yopur app terminate? - do you have a method? What event triggers termination? do you call System.exit? Do you have more than one thread?
3 you may be able to get away with that, but it's not recommended.

It seems odd that your destroy() is not being called. Have you tried stop() as well? Do you have an @Override annotation on the overridden methods? - this will pick up any situations where you have made an error in the method signature and thus failed to override properly.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just a quick answer beause I'm going to bed now.... but situations like this are often solved by having an abstract Chunk superclass, with all the getters/setters defined, and two concrete subclasses InMemoryChunk and OnDiskChunk. The in memory version just works as expected, but the on disk version's methods trigger a read from disk and creation of an in memory version.
The application code refers to Chunks via a ChunkProxy wrapper class that allows on disk Chunks to be replaced by in memory Chunks without the application knowing. This means that all your links/Terminals etc refer to ChunkProxies and never get involved with in memory/on disk issues at all.

ps (next norning) In any given case you may chose to merge two or more of those classes into one for a simpler solution - it depends on data volumes, complexity of interface, cost to read/write disk versions etc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

(basically there is a lock object corresponding to the current instance ("this") for synchronised methods, and when one method has the lock the other threads have to wait for it. Because it has the lock that method can call other synchronised methods for the same intsance, but other threads can't)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's not clear where you are hitting the problem. Do you have the .jar file that NetBeans creates (in the build directory of your NetBeans project).

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

So the first instance starts its bow method, which calls the other instance's bowBack
Simultaneously, the other instance starts its bow method, which calls the first instance's bowBack.
So one thread is in the first instance's bow, and cannot run it's bowBack becuase they are synchronised.
At the same time, the other thread is in the second instance's bow, and cannot run it's bowBack because they are synchronised.
Neither bow can finish until its call to the other's bowBack has finished, but neither thread can start bowBack until its bow has finished. So nothing can happen. Everything sits in an infinite "stuck" wait. That's a deadlock.

Don't worry if this seems confusing, because it is confusing. In my opinion thread-related problems are the hardest thing to understand in the whole of programming. You just have to keep thinking it through until it clicks in your brain.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... I haven't seen the spec so I don't know the requirements, but maybe you should be thinking along the lines of a Transaction class, a class to represent the data file (with its read method that returns a new transaction), and an application class that ties eveything together. If getResults just displays the results for one Transaction it should be an instance method in the Transaction class.

Anyway.. you;ll find a "Mark Question Solved" button at the bottom of this page.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Those two "Read" classes sound like they should just be methods. Read... could return a Transaction object, and getResults could take a Transaction as a parameter.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

my file only has day 0 in the second line. why ?

because the "morning lunch evening night average\n" is on the first line.

Just work slowly through a simple example like this:

str = "abc";
str = str + " def"  // str is now "abc def"
str = str + " ghi"  // str is now "abc def ghi"

...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hey, that's great!
Please mark this "solved" for our knowledge base - others may hit the same problem in future.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The delimiter you specify for a Scanner is not just a String, it's a Regex pattern, but $ is a special character in a regex, so maybe your delimiter is being parsed in a way you didn't expect.
Have a look at the API doc for Pattern for more details

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You only have one str variable. Each time you re-use it you lose the earlier value(s).

Initially it refers to "\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n", but then after
str = str + "Day "+ row + " :\t\t";
it no longer refers to that value. It now refers to
"morning lunch evening night average Day 0 " (ignoring the tabs for clarity)
you no longer have any reference to the orginal "Morning Lunch Evening Night Average\n"

then you execute
str = str + traffic[row][col] + "\t\t";
after which str refers to
"morning lunch evening night average Day 0 5"

In other words, each time you execute
str = str + xxx;
you just add xxx onto the end of whatever str was before.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
str = "hello";  // sets str to refer to the new string "hello"
str = " world";  // just sets str to refer to the new string " world"
but...
str = "hello";  // sets str to refer to the new string "hello"
str = str + "world"; // creates a new string by concatenating "hello" and " world"
                     // and sets str to refer to that new string, "hello world"
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You init it, and it appears at the start of the output. It won't appear on every line unless you append that string again at the start of every line

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The linked examples seem (a) pretty heavyweight amount of code and (b) don't meet the original requirement of formatting on field exit. Here's a simple example of what I was suggesting -(this one just changes the field to upper case when you exit and restores the user's exact input text when you re-enter. Changing the format rather than upper-casing is a trivial extension to this)

class UCField extends JTextField implements FocusListener {
   // displays input as upper case whenever it does not have the focus

   private String original = "";

   {addFocusListener(this);}

   @Override
   public void focusGained(FocusEvent e) {
      setText(original);
   }

   @Override
   public void focusLost(FocusEvent e) {
      original = getText();
      setText(original.toUpperCase());
   }

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

Get the field's text - now you have it in a String. Parse that as a number. Create a new String that's a formatted version of the number. Use that to set the field's text.
String's format method takes a String and a format spec, and returns a formatted version of the String. The format method is documented in the API doc for String,and the format specs are documented in the API doc for Formatter.
Key pressed events are useless to you if the user has a mouse - he can type some input then click the next field. That's why the lostFocus event is the one you need.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You could...
add a FocusListener to the field so you know when it gains & loses focus.
In the focusLost method you can get the text in the field, validate and format it as required (use String's format method, or a Formatter instance), and set the text.
It may be a good idea to keep the user's original text, and reinstate that in the focusGained method.

If I were doing this I'd define my own subclass of JTextField to encapsulate and re-use all the new functionality.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When exactly do you want to do that formatting? As the user types - when he exits the field - when he clicks OK? What if he exits the field, you re-format it, then he goes back to that field - will you re-instate the input as he typed it for him to edit? What happens if his input is invalid?

I'm not being difficult, it's just that the requirement constrains the solution...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Not as far as I know. You'll have to set the value of the y ScrollBar in your program to the y position of the component that you just requested focus for, or call scrollRectToVisible on the panel that contains the focus object, passing the focus object's getBounds as the rect. (But maybe someone else knows a better way?)

ps: IMHO scrolling controls on and off the screen makes for a confusing UI. Personally I would reserve scrolling for documents that are genuinely too big, and use something like a JTabbedPane for controls.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can call requestFocus or requestFocusInWindow for the JComponent where you want the focus. See also
http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Excellent! Thanks for the feedback.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

AWT is still officially supported in every version of the JVM, and it will be "forever". Swing is built on top of many pieces of AWT, so if AWT stopped working, Swing would stop as well. All I was saying is that there may be some bugs in the Windows 8 environment that haven't been identified and/or fixed yet if they only affect AWT but not Swing.
It's not hard to move to Swing, and you will benefit from a vastly superior GUI toolset, so don't leave it too long!
Cheers
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's more than 15 years since I last used AWT rather than Swing, so any experience I may have had with it is horribly out of date, and probably no use to you. However, it would not surprise me to learn that there are problems under Windows 8 with a Java GUI that was superceeded in 1998.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Mixing Swing and AWT like that is always risky. Maybe try using a JPanel instead of a Canvas?
YOu may also find some useful info on which methods to call and which to use with Swing here. If you use just Swing conponents you have double-buffering as standard, which may allow you to simplify your code.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can create a new subclass of JButton, and just override its paintComponent method to paint it however you want. (Not nececssarily the best way to do that particular task, but since this is a follow-up to your earlier posts about painting in Swing, that's the most relevant general answer.)

Have a loot ak http://docs.oracle.com/javase/tutorial/uiswing/painting/

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's when you want to do something that standard Swing GUI controls don't do, eg animated graphics/games or special text effects.