JAVA calling methods from other classes

Associate
Joined
19 Jul 2006
Posts
1,847
I have a few classes with in my package, with help from others on here

Code:
package gmailcsvapp;

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Random;

public class PasswordGenerator {

	private CSVParser user;
	private CSVParser pass;

	public  PasswordGenerator()
	{
		try {
			user = new CSVParser("export.csv");
			pass = new CSVParser("passwordlist.txt");
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (EmptyFileException e) {
			System.out.println(e.getMessage());
		}
                generateFirstnames();
                generateLastnames();
		generateUsernames();
		generatePasswords();

		try {
			user.toFile("exported.csv");
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}

	public void generateUsernames()
	{
		for (int i = 0; i < user.getRowCount(); i++)
		{
			String[] row = user.getRow(i);
			String[] newRow = Arrays.copyOfRange(row, 1, row.length);
			String username = row[0].substring(0,1) + "." + row[1];
			newRow[0] = username.toLowerCase();
			user.setRow(i, newRow);
		}
	}

        public void generateFirstnames()
        {
            for (int i = 0; i <user.getRowCount(); i++)
            {
                String[] row =user.getRow(i);
                String[] newRow = Arrays.copyOfRange(row,1,row.length);
                String firstname = row[0];
                user.appendToRow(i,firstname);
            }
        }

        public void generateLastnames()
        {
            for (int i = 0; i <user.getRowCount(); i++)
            {
                String[] row =user.getRow(i);
                String[] newRow = Arrays.copyOfRange(row,1,row.length);
                String lastname = row[1];
                user.appendToRow(i,lastname);
            }
        }
      


	public void generatePasswords()
	{
		for (int i = 0; i < user.getRowCount(); i++)
		{
			user.appendToRow(i, getPassword());
		}
	}

	private String getPassword()
	{
		String[] passwords = pass.getCol(0);
		Random rand = new Random();
		String pass = passwords[rand.nextInt(passwords.length-1)];
		return pass.toLowerCase() + rand.nextInt(100);
	}

	public static void main(String[] args) {
		new Bodyitems();
//* this worked when this was new PasswordGenerator();


	}
}

and Bodyitems ( which is part of the GUI )
Code:
package gmailcsvapp;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * 
 */
public class Bodyitems extends menuitems{

    public Bodyitems ()
    {
        super();
        JButton openButton = new JButton("Open Import");
        JLabel Importfilelb = new JLabel("Hello there");
        JLabel blank = new JLabel("Enter output file name");
        JTextField outputfiletxt = new JTextField("Outputfilename.csv");
        JButton outputButton = new JButton("Create New CSV");

        outputButton.addActionListener(new outputWatcher());



        Container cp = getContentPane();
        cp.setLayout(new GridLayout(3, 2));
	cp.add(openButton);
        cp.add(Importfilelb);
        cp.add(blank);
        cp.add(outputfiletxt);
        cp.add(outputButton);
        setVisible(true);
    }


    private class outputWatcher implements ActionListener
    {

        public void actionPerformed (ActionEvent a)
        {
             new PasswordGenerator();
        }
    }



}

Im trying to get it so that the text in the output file becomes the name of the exported.csv file

And i want to have some kind of way of navigating to the file to get the name of the import file using filechooser



TIA
 
Last edited:
Thanks both im using Netbeans. I'm still really new to this. so not sure what using a none static method means

So just to get this clear in my head were creating an new instance of PasswordGenerator when the program starts. That loads the password file and the import file ?
Then when the button is pressed that is when the following is run
Code:
generateFirstnames();
    generateLastnames();
    generateUsernames();
    generatePasswords();

    try {
      user.toFile("exported.csv");
    } catch (FileNotFoundException e) {
      System.out.println(e.getMessage());
    }
  }

Will this not make it hard to change the imported file name that will be generated from the bodyitems class.

I take it to update the exported.csv file. i would use something like
Code:
public String getexportfile()
	{
		return outputfiletxt.getText() ;
	}

which brings up a cannot find symbol error?
 
Last edited:
Thanks again Rob,

The compiler is not happy with
Code:
package gmailcsvapp;




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 *
 */
public class Bodyitems extends menuitems{

  private PasswordGenerator gen;

  public Bodyitems ()
  {
    super();
    JButton openButton = new JButton("Open Import");
    JLabel Importfilelb = new JLabel("Hello there");
    JLabel blank = new JLabel("Enter output file name");
    JTextField outputFilenameField = new JTextField("Outputfilename.csv");
    JButton outputButton = new JButton("Create New CSV");

    outputButton.addActionListener(new outputWatcher());

    Container cp = getContentPane();
    cp.setLayout(new GridLayout(3, 2));
    cp.add(openButton);
    cp.add(Importfilelb);
    cp.add(blank);
    cp.add(outputFilenameField);
    cp.add(outputButton);
    setVisible(true);
  }

  private class outputWatcher implements ActionListener
  {
    public void actionPerformed (ActionEvent a)
    {
      String in = Bodyitems.cp.inputFilenameField.getText();
      gen = new PasswordGenerator(in);

      // Note, the code below could go on a completely different button
      String out = [COLOR="Red"]outputFilenameField[/COLOR].getText();
      gen.saveToFile(out);
    }
  }

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

cannot find symbol?

I modified it slightly as the password file is going to be static and not change.

I will want to implimet a file chooser to open a inputfile when the user clicks on the openButton
 
Back
Top Bottom