Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with array

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
I have an program that adds an item to a list if the user has the corresponding application loaded on their machine. My problem is if the user doesn't have the application it leaves a blank line. I believe the problem is with the line "String AppsLoaded[] = new String[8];" as I'm setting the length prior to really figuring out what the user has loaded. What's the best way to solve this? Should I remove a blank entry after the fact or should I not reserve a set amount of entries in AppsLoaded to begin with?

thanks!

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;

public class ApplicationList extends JFrame implements ActionListener {
   
   //store Access & Internet Explorer paths
    String MSACCESS = "C:\\Program Files\\Microsoft Office\\Office10\\MSACCESS.EXE";
    String IE = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";

    public static void main(String []args) {
        new ApplicationList();
    }
    public void actionPerformed(ActionEvent e) {
    JMenuItem source = (JMenuItem)(e.getSource());
    String cmd = source.getText();
    
    HashMap apps = new HashMap ();
    apps.put ("CNN", "[URL unfurl="true"]http://cnn.com");[/URL]
    apps.put ("db1", "C:\\db1.mde");
    apps.put ("db2", "C:\\db2.mde");
    apps.put ("db3", "C:\\db3.mde");
    apps.put ("db4", "C:\\db4.mde");
    apps.put ("db5", "C:\\db5.mde");
    apps.put ("db6", "C:\\db6.mde");
    apps.put ("db7", "C:\\db7.mde");

    if (cmd.equals ("Exit")){
        System.exit(0);
    }
    
    String path = (String)apps.get (cmd);
    int pos = path.indexOf("/");
    if (pos == -1) {
        try{
            String [] commands = new String [] {MSACCESS,path};
            Process child = Runtime.getRuntime ().exec (commands);
        }catch (IOException a){
            System.err.println (a.getMessage ());
            }
    }
    //if path has backslash, we know it's a website
    else {
        try{
            String [] commands = new String [] {IE,path};
            Process child = Runtime.getRuntime ().exec (commands);
        }catch (IOException a){
            System.err.println (a.getMessage ());
            }
    }
}
    public ApplicationList ()
    {
        super ("Launcher");
        setSize (150, 50);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        //Store every application name in an array
        //Make sure these names match the names in the HashMap EXACLTY!!
        String AppNames[] = { "CNN", "db1", "db2", "db3", "db4",
        "db5", "db6", "db7"};
        
        File[] AppPaths = new File[8];
        AppPaths[1] = new File("C:\\db1.mde");
        AppPaths[2] = new File("C:\\db2.mde");
        AppPaths[3] = new File("C:\\db3.mde");
        AppPaths[4] = new File("C:\\db4.mde");
        AppPaths[5] = new File("C:\\db5.mde");
        AppPaths[6] = new File("C:\\db6.mde");
        AppPaths[7] = new File("C:\\db7.mde");
        
        String AppsLoaded[] = new String[8];
        
        //add applications that everybody has; don't need to check for
        AppsLoaded[0] = "CNN";
        
        for(int i=1;i<AppNames.length;i++){ //don't check cnn, it's a web application, so everybody has it, so start at 1
          if(AppPaths[i].exists()){
            AppsLoaded[i] = AppNames[i];
            System.out.println(AppsLoaded[i]);}
        }
        
        JMenuBar jmb = new JMenuBar ();
        JMenu myMenu  = new JMenu ("Application");
        jmb.add(myMenu);
        JMenuItem [] menulist = new JMenuItem[20];
        
        for (int i=0; i < AppsLoaded.length; i++) {
        menulist[i] = new JMenuItem(AppsLoaded[i]);
        menulist[i].addActionListener(this);
        myMenu.add(menulist[i]);
        }
        myMenu.addSeparator ();
        JMenuItem quititem = new JMenuItem("Exit");
        myMenu.add(quititem);
        quititem.addActionListener(this);

        setJMenuBar (jmb);
        setVisible (true);
    }
}
 
I didn't read the code, but from the description I guess the best way to do that is using an ArrayList to store the Strings and after the processing, convert it into an array.

Cheers,

Dian
 
Wow, that codes a bit messy !
Why are you maintinaing lists of essentially the same data ? Madness ...

I'm bored today, so here is a bit of a refactoring of your code to demonstrate a path that you should probably be going down ...

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;

public class ApplicationList extends JFrame implements ActionListener {

	// A private class representing our application data
	class ApplicationsHolder {
		public String name;
		public String path;
		public boolean checkPath;

		public ApplicationsHolder(String name, String path, boolean checkPath) {
			this.name = name;
			this.path = path;
			this.checkPath = checkPath;
		}
	}

	// A single global list holding our data
    ArrayList apps = new ArrayList();

   //store Access & Internet Explorer paths
    static final String MSACCESS = "C:\\Program Files\\Microsoft Office\\Office10\\MSACCESS.EXE";
    static final String IE = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";

    public static void main(String []args) {
        new ApplicationList();
    }

    public void actionPerformed(ActionEvent e) {
    JMenuItem source = (JMenuItem)(e.getSource());
    String cmd = source.getText();


    if (cmd.equals ("Exit")){
        System.exit(0);
    }

    String path = null;
	for (int i=0; i < apps.size(); i++) {
		ApplicationsHolder tmp = (ApplicationsHolder)apps.get(i);

		if (tmp.name.equals(cmd)) {
			path = tmp.path;
		}
	}

	if (path == null) {
		// handle error if cannot find the application required
		System.err.println("Cannot find : " +cmd);
		return;
	}


    int pos = path.indexOf("/");
    if (pos == -1) {
        try{
            String [] commands = new String [] {MSACCESS,path};
            Process child = Runtime.getRuntime ().exec (commands);
        }catch (IOException a){
            System.err.println (a.getMessage ());
            }
    }
    //if path has backslash, we know it's a website
    else {
        try{
            String [] commands = new String [] {IE,path};
            Process child = Runtime.getRuntime ().exec (commands);
        }catch (IOException ioe){
            ioe.printStackTrace(System.err);
        }
    }
}

	// This really should be loaded from some lind of config
	// file rather than be hardcoded
	public void initPathData() {
		apps.add(new ApplicationsHolder("CNN", "[URL unfurl="true"]http://cnn.com",[/URL] false));
		apps.add(new ApplicationsHolder("db1", "C:\\java\\ApplicationList.java", true));
		apps.add(new ApplicationsHolder("db2", "C:\\db2.mde", true));
		apps.add(new ApplicationsHolder("db3", "C:\\db3.mde", true));
		apps.add(new ApplicationsHolder("db4", "C:\\java\\ApplicationList.class", true));
		apps.add(new ApplicationsHolder("db5", "C:\\db5.mde", true));
		apps.add(new ApplicationsHolder("db6", "C:\\db6.mde", true));
		apps.add(new ApplicationsHolder("db7", "C:\\db7.mde", true));

	}

    public ApplicationList ()
    {
        super ("Launcher");

		initPathData();

        setSize (300, 400);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        JMenuBar jmb = new JMenuBar ();
        JMenu myMenu  = new JMenu ("Application");
        jmb.add(myMenu);
        JMenuItem [] menulist = new JMenuItem[20];

        for (int i=0; i < apps.size(); i++) {
			ApplicationsHolder tmp = (ApplicationsHolder)apps.get(i);

			// don't add it to the list if cannot find
			if (!new File(tmp.path).exists() && tmp.checkPath) {
				continue;
			}

        	menulist[i] = new JMenuItem(tmp.name);
        	menulist[i].addActionListener(this);
        	myMenu.add(menulist[i]);
        }
        myMenu.addSeparator ();
        JMenuItem quititem = new JMenuItem("Exit");
        myMenu.add(quititem);
        quititem.addActionListener(this);

        setJMenuBar (jmb);
        setVisible (true);
    }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
oh, also, for efficiency, you should probably break from the while loop once you have found the app name :

(see line 49)

Code:
		if (tmp.name.equals(cmd)) {
			path = tmp.path;
                        break;
		}

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks sedj, this is my first app written in Java, so I really appreciate you smoothing out my code...I'll review carefully when I have time to see what you did and why you did it. That's why I love this forum...books can't tell you a more efficient way of writing code once your app is "done".
thanks!
 
sedj, I'm just getting around to reviewing your changes...again, thanks for the input. Question: what do you mean by "This really should be loaded from some lind of config file rather than be hardcoded?" If this is something I can do without hardcoding, then that would be great for future changes.
 
sedj, on the line "if (!new File(tmp.path).exists() && tmp.checkPath)" is the tmp.checkPath checking for True by defualt since you don't state "= True"?
 
For your last question: YES
But it's not a question of default, but of value.

Indeed:
1 <=> True
0 <=> False

And when you write: if (tmp.checkPath), it's the same as if (tmp.checkPath == True)

Tip: if (!tmp.checkPath) is the same as if (tmp.checkPath == False)
 
Hardcode ... sedj is totally right. Hardcode is pork code.
Apparently you don't know what it is.
Hardcode is when you write code lines that are applicable only to your application and won't work in all situations (see other better definition on the Web :)

For example, what you did is typically dirty hardcode:
AppPaths[1] = new File("C:\\db1.mde");
AppPaths[2] = new File("C:\\db2.mde");

Indeed, your application won't work on another machine that has no C:\ drive.
Also, you force the user the have the file: "db2.mde" placed at the root of the C:\ drive.
Also, the file must be named "db2.mde" otherwise it won't work ...
You also force to have 8 files .mde. Always 8 ???

What you should do is: have a text file (edit it with notepad for example) where you write the information like:
- db1.mde
- db2.mde ...
- the path of their file for example, if not in the same directory as your application

Thanks to that, the user don't have to modify your code but just modify the config file.

But you, you have to read this config file, and have a loop that says something like: while there is a file .mde in the config file to take into account, I load it in my program.

You understand a bit the principle ?
All that is just an example, but hardcode is really something to avoid !! Read about it on the Web
Good luck
 
thanks Rodie, I've been coding for years and I certainly know what hardcoding is, but I'm new to Java, so I have to start somewhere. Luckily in the company's systems, these apps are always in the same place on everybody's machine, but I still want to learn how to do it without hardcoding.
 
I suggest:

Code:
for (int i = 1; i < 8; ++i)
        apps.put ("db"+ i, "C:\\db" + i + ".mde");
instead of
Code:
  apps.put ("db1", "C:\\db1.mde");
    apps.put ("db2", "C:\\db2.mde");
    apps.put ("db3", "C:\\db3.mde");
    apps.put ("db4", "C:\\db4.mde");
    apps.put ("db5", "C:\\db5.mde");
    apps.put ("db6", "C:\\db6.mde");
    apps.put ("db7", "C:\\db7.mde");

seeking a job as java-programmer in Berlin:
 
stefanwagner, those aren't the actual application names, I just didn't want to use the real names cuz you never know with company rules, etc. Thanks anyways!
 
My be is in a properties file with the application names.

Take a look at java.util.Properties class.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top