Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

In what parameter? Are we supposed to look over your shoulder at what you typed? You create a new Scanner instance for each file in a list (or any other collection). If you're getting errors trying to do that, you need to post your code and the exact error message.

By the way, my code fragment above used the wrong constructor form (it passed the string file name instead of a File object) and has been edited accordingly.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

He already has the calculation right there in his method. It was just not utilized correctly. The number of comparisons necessary for n elements is n(n − 1) / 2 .

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You just need to open a scanner on each file in your file list and process it, i.e.

String[] fileList = {"file1.txt","file2.txt","file3.txt"};
for (String filename : fileList){
    Scanner scanner = new Scanner(new File(filename));
    while (scanner.hasNext()){
        // blah blah
    }
    scanner.close();
}

Edit: Used wrong constructor on the Scanner.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It's just one single calc to determine the number of comparisons and you have the formula right there in your code - you just aren't using the correct value in it. I'll give you a hint: n is the number of elements in your values array.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, I suppose you would start with the Swing tutorial: http://java.sun.com/docs/books/tutorial/uiswing/index.html
and when you have questions, come back and post them along with your code.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You just need to specify the 1/3 so that it uses floating point division instead of int division, so change it to 1.0/3.0 or 1/3d

1/3d * Math.PI * height * Math.pow(radius, 2);

You were getting zero because 1/3 as an int is zero, causing the entire expression to yield zero. Always keep that in mind when mixing integer and floating point primitives in expressions.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Nope, because you're still using == to check string equality instead of the equals() method.

Anyway, I'd recommend looking at this method in the Scanner API: Scanner.hasNextInt()

Optionally, you might want to consider looking at the regex Pattern class as well.

iamthwee commented: *nods* +17
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, you certainly don't want to use "==" for string equality.
You could check for any of them with a regular expression.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It means the package declaration in the file does not match the package structure on disk. Are you certain that you added the 'build' folder in the Libraries section using the "Add jar/folder" button? I downloaded the code and added that folder to my own project with no problems at all.

stephen84s commented: I would have already started abusing and swearing by now :P +4
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, if you had read the instructions for that project (which I found here in just a couple of clicks), you might have seen this part:

If you just want to use the classes, all you need to do is put the aima-java/build directory on your CLASSPATH.

Which means you should just add that folder to your Libraries under the Project Properties.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

If you also have a .jar file of the classes in that download, you can just add it as a library to your project (from Project Properties window). The same would work for the folder containing the class files themselves.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Make determineGrade() a function that takes the test score as a parameter and returns the letter for the grade. Honestly, if this is a "final" you really should already know how to write a function.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Sure, take a look at Process and ProcessBuilder.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It all depends on which read() method you use. The exact behavior is detailed in the API doc of those methods.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

The API says something about blocking. What exactly is blocking, when will it happen, and how will it affect the fact that I want the whole file to be read in at once?

Blocking in that context means that code execution will halt (or wait) until one of the conditions listed there is met.

This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

So essentially, as long as an IO error does not occur and the end of the stream has not been reached, it will wait for data to be available to read, rather than immediately returning some empty result. It "blocks" your code from continuing until it's satisfied the return condition.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

yeah i am currently working with raima database and there is no forum i can ask about it.

This certainly looks like a forum to me: http://www.raima.com/forum/

I doubt that Dani wants to create a separate forum for every single database that is available for use.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

I would say it's irrelevant. If your paint method is just rendering shapes that have already been computed, I wouldn't worry about it.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

No, it isn't, really. If you are still concerned about calling the same method again and again, instead of having your class maintain the state or cache the `red' value, shove the responsibility to the invoked method instead.

private void mutate() {
  int red = color.getRed();
  // some complicated, multi step calculation using `red'
  color.setRed(red);
}

And talking of terms like overhead for a program without any concrete profiling data whatsoever is wrong IMO.

++ to this.
Premature optimization in the absence of profiling data is a recipe for all kinds of trouble and makes baby Jesus cry.
The overhead of that method call vs the variable is negligible unless it's in a long loop and even then compiler will probably identify it as a loop invariant and use a local variable for it anyway. If not then you can always make one yourself just like s.o.s. showed.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Tyster commented: Many Thanks! I think using Callable will do the trick. Cheers! +2
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You could easily use a Future object to retrieve the result from a Callable process to aggregate the results.

This may be a useful read: http://blogs.sun.com/CoreJavaTechTips/entry/get_netbeans_6

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Take a look at the Callable interface, which can return a value and throw Exceptions.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

This was also mentioned by myself and s.o.s. two days ago in your other post about the char array... :P

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

That has nothing to do with trying to use a class constructor that does not exist, which is what you are doing right here

Lottery lot = new Lottery(userNumbers);

If you make changes to the code, repost it using [code]

[/code] tags, because it's just a mess with all the formatting removed.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

That's because you only declared those variables locally in the calculateperimeter() method and they have no scope in your calculatearea() method.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Though it is not Java specific and not targeted for beginners, Code Complete 2nd Edition is an excellent book for anyone engaged in non-trivial programming or interested in software best practices. It's filled with a ton of information and suggestions gleaned from years of professional development experience - all that stuff that most of us working developers have learned the hard way in bits and pieces and wish we'd known from the outset.

~s.o.s~ commented: Indeed; like they say, "If I have seen further it is only by standing on the shoulders of Giants" +25
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Actually, I just noticed that your "board" variable should probably be a HiLoCanvas, not "HiLo" as you have it declared. I really don't think you intended to add an instance of a JApplet to a JApplet content pane.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Well, "implements board()" is invalid syntax due to the parenthesis, but even so, that does nothing for your listener. The addActionListener() method requires an object that implements the ActionListener interface - which is why I mentioned that "board" must implement that if you expect to pass it as a listener. So you need the HiLo class to implement ActionListener if you expect it to act like one.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It can't be applied if "board" does not implement ActionListener.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

You're better off using the Timer anyway to avoid repaint issues and stalling the AWT event thread.
Here's a simple example of the label with a timer:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TimerExample extends JFrame {

    final String[] words = {"The", "cat", "in", "the", "hat"};
    final JLabel label;
    Timer wordTimer;
    int wordIndex = 0;

    public TimerExample() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 200);
        label = new JLabel("", JLabel.CENTER);
        getContentPane().add(label);

        wordTimer = new Timer(1000, new WordTimerListener());
        setVisible(true);
        wordTimer.start();
    }

    class WordTimerListener implements ActionListener {
        int wordIndex = 0;

        public void actionPerformed(ActionEvent e) {
            if (wordIndex < words.length) {
                label.setText(words[wordIndex++]);
            } else {
                wordTimer.stop();
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TimerExample();
            }
        });
    }
}
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Try calling startTask() before you set the dialog visible.

You may also want to glance at this article on a modal progress monitor: http://www.javalobby.org/java/forums/t53926.html

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

That is what I was trying to determine as well. It sounds like you are just asking about communicating between 2 classes in the same application, which is no trouble at all. Post a little bit of the code and it would be much clearer what you are trying to do.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

It would really help if you posted a little more information about the nature of the two "programs". Separate JVMs? Concurrently executing threads? You question is a little too unclear to give an answer.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Mark the thread as solved if the issue is resolved.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Whether you use arrays, Lists, or whatever, the point is still the same. You'll need to read the file content into some structure and then use JDBC to update/insert your table values.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Perhaps you should post in the Perl forum. This is the Java forum.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Why are you trying to use both a FileWriter and a BufferedWriter against the same file? Just use the BufferedWriter.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Use the newLine() method to write the line separator, instead of appending '\n' to your text.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Also note that directories will also be included and can be discerned with file.isDirectory(). If you need to include file counts from subdirectories, you'll need to do the recursion yourself.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Ok, building a bit on the LoginFrame code above, here is a tiny framework that manages a login frame and a main app frame that is shown for a valid user. It doesn't do much, but should illustrate the point a bit.

I threw the other classes in at the bottom so it could be pasted into and run from a single file. Obviously, you would want to separate them to appropriate top-level public classes.

The only valid login is "Bob","12345". (And don't take this to be an example of managing secure logins, the MainApp controller is the point of the code :P )

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginFrame extends JFrame {

    private Dimension dimLF = new Dimension(250, 200);
    LoginPanel loginJP;
    ErrorPanel errJP;
    
    MainApp mainApp;

    public LoginFrame(MainApp mainApp) {
        super();
        this.mainApp = mainApp;
        
        setTitle("BMS - Login");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        loginJP = new LoginPanel();
        gbc.gridx = 0;
        gbc.gridy = 1;
        getContentPane().add(loginJP, gbc);

        errJP = new ErrorPanel();
        gbc.gridx = 0;
        gbc.gridy = 0;
        getContentPane().add(errJP, gbc);

        setSize(dimLF);

        setResizable(false);
        setVisible(true);

    }

    class LoginPanel extends JPanel {
        LoginCredentials credentials;
        final JTextField usrTF;
        final JPasswordField passPF;
        
        public LoginPanel() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            
            JPanel usrPanel = new JPanel();
            JLabel usrLabel = new JLabel("Username:");
            usrTF = new JTextField(10);
            usrTF.setToolTipText("Please enter your username.");
            usrPanel.add(usrLabel);
            usrPanel.add(usrTF);

            JPanel passPanel = new JPanel();
            JLabel passLabel = …
jasimp commented: What would the Java forum be without you? +9
peter_budo commented: Another great example +12
Alex Edwards commented: You're a hero. +5
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

A separate controlling class to handle those transitions is cleaner to manage. The frames themselves are just visual mechanisms for interacting with the data/process, so it's really just data or state information that needs to be passed to the new frame (or a reference to the aforementioned controlling class that is actually controlling the process state).

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Ok, I can't fiddle around with your exact code, because there were several parts that weren't in all that was posted above. A couple things come to mind though. First, it looks like you're sharing a single GridBagLayout manager ("gbag") among several panels, but using as if each was separate. That could definitely cause unexpected results.

On the sizing issue, Swing won't resize your JFrame by default if it's contents change size. You would need to use setSize() or pack() to resize that top-level window.

I think you would have an easier time managing all of those components if you separated the panels into their own classes, a bit like this

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginFrame extends JFrame {

    private Dimension dimLF = new Dimension(250, 200);
    LoginPanel loginJP;
    ErrorPanel errJP;

    public LoginFrame() {
        setTitle("BMS - Login");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        loginJP = new LoginPanel();
        gbc.gridx = 0;
        gbc.gridy = 1;
        getContentPane().add(loginJP, gbc);

        errJP = new ErrorPanel();
        gbc.gridx = 0;
        gbc.gridy = 0;
        getContentPane().add(errJP, gbc);

        setSize(dimLF);

        setResizable(false);
        setVisible(true);

    }

    /** one panel for login components */
    class LoginPanel extends JPanel {

        public LoginPanel() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            
            // probably don't need separate panels for each pair here
            JPanel usrPanel = new JPanel();
            JLabel usrLabel = new JLabel("Username:");
            JTextField usrTF = new JTextField(10);
            usrTF.setToolTipText("Please enter your username."); …
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Post the "loginJP" and "errJP" classes as well. Preferably with just their UI component setup portions.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Why would you wait for more instead of checking out the references you've already been given? Have you tried searching any out on your own? Waiting for others to hand you information is not a habit I'd recommend if you wish to be productive at programming.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Do you mean that depending on what the contents are of the XML file, different panels would appear which are created from classes that have their own content... but that the type and size of the panel are always the same? Because I don't see how you could use different sized panels each time, that would mess up the layout.

Layout and/or size are nothing more than attributes that you can put in an xml descriptor like any other property. We place one or more panels in a frame, with whatever attributes are required. The only part that needs reflection at all is the instantiation of a class by name with a specific constructor and the specification of the constructor parameters.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

> Can anyone provide a reasonable example of a situation that needs reflection and a short explanation of why?

We use it on our current project for dynamic configuration of our UI. All panel components share an interface and with a simple XML file we can reconfigure the frames and panels with no change to the compiled jar file. We also use it for our reporting engine to load column classes that can be plugged in to our table-based reports. These are simple usages of loading classes that share a common interface by supplying their class name and other properties in simple xml files.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

We ditched VSS for Subversion as well. Very happy with it.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Post the interview questions here and perhaps someone will oblige to answer them.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

These two are free online:
http://hudzilla.org/phpwiki/index.php?title=Main_Page
http://www.techotopia.com/index.php/PHP_Essentials

I don't think they are downloadable though.

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Then when the user inputs (1,2) later for display not input, it displays the details in (1,2).

That was the point of the skeletal code above. The details are the "Attribute" object. You can call it whatever you want. All you are doing is storing and retrieving a single object at a position in a 2D array.