Method problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ozzyfb
    New Member
    • Nov 2006
    • 2

    Method problem

    Hi,
    As you may figure out from my source code, I dont think i have a really bright future in java... I'm supposed to write this password creating program using the method of ;
    public static String passwordGenerat or(int n, int m)

    i would much appreciate it if someone can have an eye on it..


    Code:
    import javax.swing.JOptionPane;
    public class PasswordMethodA {
    public static void main(String[]args) {
    
    	
    	
    		int option = 0;
    		String pass = "";
    
    		while (option == 0)
    		{
    	// Displaying out the choices user would like to have in creating different types of passwords
    		String choiceStr = JOptionPane.showInputDialog("Welcome to the password generator. Here are types of passwords you can select. "+
    		"\n1 : lower case letters only."+
    		"\n2 : upper case letters only."+
    		"\n3 : numbers only."+
    		"\n4 : characters and symbols."+
    		"\nPlease select one : " );
    	int choice = Integer.parseInt(choiceStr);
    		
    		int pass = passwordGenerator(pass, choice);
    		
    		JOptionPane.showMessageDialog(null,"Your password is " + pass);
    
    
    }
    	public static String passwordGenerator(int pas, int m)
    {
    	String pas = null;
    	int pas = 0;
    	int n = 0;
    	double r = 0;
    	
    		if (m == 1)
    		{
    		JOptionPane.showMessageDialog(null, m);
    			for(int i = 1; i<7; i++){
    			n = (int)(Math.random()*26);
    			pas = pas + "abcdefghijklmqnoprstuvwxyz".charAt(n);
    			}
    		}
    		else if (m == 2)
    // Creating upper case password if the user typed '2'		
    		{
    			JOptionPane.showMessageDialog(null,m);
    			for(int i = 1; i<7; i++){
    			n = (int)(Math.random()*26);
    			pas = pas + "ABCDEFGHIJKLMQNOPRSTUVWXYZ".charAt(n);
    			}
    		}
    // Creating an upper case and numbers password if the user typed '3'		
    		else if(m == 3)
    		{
    			JOptionPane.showMessageDialog(null,m);
    			for(int i = 1; i<7; i++){
    			n = (int)(Math.random()*62);
    			pas = pas + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".charAt(n);
    		}
    		}
    // Creating password using numbers, lowercase, uppercase and 9 symbols if the user typed '4'
    		else if(m == 4)
    		{
    			JOptionPane.showMessageDialog(null, m);
    			String aStr = JOptionPane.showInputDialog("How many digited password do you want to create?");
    			int a = Integer.parseInt(aStr);
    			for(int i = 1; i<a; i++){
    			n = (int)(Math.random()*70);
    			pas = pas + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@?[\\]^-'".charAt(n);
    		}
    // Making sure symbols are added for the last choice.		
    		n = (int)(Math.random()*a);
    		int sym = (int)(Math.random()*8);
    		a = pas.length();
    		pas = pas.substring(0,n)+"@?[\\]^-'".charAt(sym)+pas.substring(n,a);
    	return pas;
    		}
    
    	}
    	// Asking if the user wants to create a new password		
    		JOptionPane.showMessageDialog(null,"If you want to create another password press 0; if not press 1; For cancel press 2." + option);
    
    		pass="";
    		}
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ozzyfb
    Hi,
    As you may figure out from my source code, I dont think i have a really bright future in java... I'm supposed to write this password creating program using the method of ;
    public static String passwordGenerat or(int n, int m)

    i would much appreciate it if someone can have an eye on it..


    Code:
    import javax.swing.JOptionPane;
    public class PasswordMethodA {
    public static void main(String[]args) {
     
     
     
    		int option = 0;
    		String pass = "";
     
    		while (option == 0)
    		{
    	// Displaying out the choices user would like to have in creating different types of passwords
    		String choiceStr = JOptionPane.showInputDialog("Welcome to the password generator. Here are types of passwords you can select. "+
    		"\n1 : lower case letters only."+
    		"\n2 : upper case letters only."+
    		"\n3 : numbers only."+
    		"\n4 : characters and symbols."+
    		"\nPlease select one : " );
    	int choice = Integer.parseInt(choiceStr);
     
    		int pass = passwordGenerator(pass, choice);
     
    		JOptionPane.showMessageDialog(null,"Your password is " + pass);
     
     
    }
    	public static String passwordGenerator(int pas, int m)
    {
    	String pas = null;
    	int pas = 0;
    	int n = 0;
    	double r = 0;
     
    		if (m == 1)
    		{
    		JOptionPane.showMessageDialog(null, m);
    			for(int i = 1; i<7; i++){
    			n = (int)(Math.random()*26);
    			pas = pas + "abcdefghijklmqnoprstuvwxyz".charAt(n);
    			}
    		}
    		else if (m == 2)
    // Creating upper case password if the user typed '2'		
    		{
    			JOptionPane.showMessageDialog(null,m);
    			for(int i = 1; i<7; i++){
    			n = (int)(Math.random()*26);
    			pas = pas + "ABCDEFGHIJKLMQNOPRSTUVWXYZ".charAt(n);
    			}
    		}
    // Creating an upper case and numbers password if the user typed '3'		
    		else if(m == 3)
    		{
    			JOptionPane.showMessageDialog(null,m);
    			for(int i = 1; i<7; i++){
    			n = (int)(Math.random()*62);
    			pas = pas + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".charAt(n);
    		}
    		}
    // Creating password using numbers, lowercase, uppercase and 9 symbols if the user typed '4'
    		else if(m == 4)
    		{
    			JOptionPane.showMessageDialog(null, m);
    			String aStr = JOptionPane.showInputDialog("How many digited password do you want to create?");
    			int a = Integer.parseInt(aStr);
    			for(int i = 1; i<a; i++){
    			n = (int)(Math.random()*70);
    			pas = pas + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@?[\\]^-'".charAt(n);
    		}
    // Making sure symbols are added for the last choice.		
    		n = (int)(Math.random()*a);
    		int sym = (int)(Math.random()*8);
    		a = pas.length();
    		pas = pas.substring(0,n)+"@?[\\]^-'".charAt(sym)+pas.substring(n,a);
    	return pas;
    		}
     
    	}
    	// Asking if the user wants to create a new password		
    		JOptionPane.showMessageDialog(null,"If you want to create another password press 0; if not press 1; For cancel press 2." + option);
     
    		pass="";
    		}
    Future in java is not determined by looking at one's code, but by looking at one's willingness to code.
    Now I've just made it compile and run so that you can better explain where it's not working and what you want this to do



    Code:
     
    import javax.swing.JOptionPane;
    public class PasswordMethodA {
     public static void main(String[]args) {
      
      int option = 0;
      String opt = "";
      while (option == 0) {
       // Displaying out the choices user would like to have in creating different types of passwords
       String choiceStr = JOptionPane.showInputDialog("Welcome to the password generator. Here are types of passwords you can select. "+
       "\n1 : lower case letters only."+
       "\n2 : upper case letters only."+
       "\n3 : numbers only."+
       "\n4 : characters and symbols."+
       "\nPlease select one : " );
       int choice = Integer.parseInt(choiceStr);
       String pass = PasswordMethodA.passwordGenerator(8, choice);
       JOptionPane.showMessageDialog(null,"Your password is " + pass);
       opt = JOptionPane.showInputDialog("How many digited password do you want to create?");
       option = Integer.parseInt(pass);
      }
     }
     public static String passwordGenerator(int pase, int m) {
      String pas = "";
      int n = 0;
      double r = 0;
      if (m == 1) {
       JOptionPane.showMessageDialog(null, m);
    	for(int i = 1; i<7; i++){
    	 n = (int)(Math.random()*26);
    	 pas = pas + "abcdefghijklmqnoprstuvwxyz".charAt(n);
    	}
      }
      else if (m == 2) {
       JOptionPane.showMessageDialog(null,m);
       for(int i = 1; i<7; i++){
    	n = (int)(Math.random()*26);
    	pas = pas + "ABCDEFGHIJKLMQNOPRSTUVWXYZ".charAt(n);
       }
      }
      else if(m == 3) {
       JOptionPane.showMessageDialog(null,m);
       for(int i = 1; i<7; i++){
    	n = (int)(Math.random()*62);
    	pas = pas + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx  yz0123456789".charAt(n);
       }
      }
      else if(m == 4) {
       JOptionPane.showMessageDialog(null, m);
       String aStr = JOptionPane.showInputDialog("How many digited password do you want to create?");
       int a = Integer.parseInt(aStr);
       for(int i = 1; i<a; i++){
    	n = (int)(Math.random()*70);
    	pas = pas + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx  [email="yz0123456789@?[\\]^-'&quot;.charAt(n"]yz0123456789@?[\\]^-'".charAt(n[/email]);
       }
       n = (int)(Math.random()*a);
       int sym = (int)(Math.random()*8);
       a = pas.length();
       pas = pas.substring(0,n)+"@?[\\]^-'".charAt(sym)+pas.substring(n,a);
      }
      // Making sure symbols are added for the last choice.
    
      return pas;
     }
    }

    Comment

    • ozzyfb
      New Member
      • Nov 2006
      • 2

      #3
      Originally posted by r035198x
      Future in java is not determined by looking at one's code, but by looking at one's willingness to code.
      Now I've just made it compile and run so that you can better explain where it's not working and what you want this to do
      I was able to fix the code after ur changes. It runs perfectly now. Thank you very much

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by ozzyfb
        I was able to fix the code after ur changes. It runs perfectly now. Thank you very much
        I'm delighted to hear that. Especially since you managed to get the code to work yourself in the end.

        Comment

        Working...