Results 1 to 6 of 6
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Passing labels to methods?

    So here is my code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class herp extends JFrame {
    	private JTextField password;
    	private JButton insert;
    	public herp(){
    		super("TitleBar");
    		password = new JTextField(20);
    		insert = new JButton("CrackIt");
    		add(password);
    		add(insert);
    		HandlerClass handler = new HandlerClass();
    		insert.addActionListener(handler);
    		setLayout(new FlowLayout());	
    	}
    	private class HandlerClass implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    		Object obj1 = new Object();
    		obj1 = password.getText();
    		String obj2= new String();
    		obj2 = obj1.toString();
    		search2: for(int x=1; x<=6; x++){
    		checkIt(obj2, x, "", search2);
    		}
    		}
    	
    	}
    	public void checkIt(String origPass, int length, String base, Label toBreak)
    	{
    	    search: for (char c = 'A'; c <= 'Z'; c++) {
    			String finalString = "a";
    	        if (length == 1) {
    
    	        	finalString = base+ c;
    	        
    	        }
    	        else {
    	       checkIt(origPass, length - 1, base + c, "search2");
    	        }
    			if(finalString.equals(origPass)){
    				JOptionPane.showMessageDialog(null, String.format("The pw was %s", origPass));
    				break search2;
    			}
    	   
    	    }
    	
    }
    	}
    Basically, I need a variable in my function, and it has to be generated by a for loop. However, when I finish something in my function (in this case cracking the pw) I want to break out of that loop. Is there anyway to do this? I was thinking about passing the label
    search2 into my function, and then breaking out of that. The program cracks passwords, then tells me how long it took (still working on the last part). However, I don't know how long the password is, so i have to use a for loop which keeps incrementing a variable that I then pass in as the password length.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Passing labels to methods?

    Why not simply pass the value back up the stack as a return value? You can check the value to see if you need to keep going. Also, your password cracking routine should be used simply for doing the grunt work. Let the controlling routine do the display of the password.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Passing labels to methods?

    Quote Originally Posted by jim829 View Post
    your password cracking routine should be used simply for doing the grunt work. Let the controlling routine do the display of the password.
    What do you mean by this?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Passing labels to methods?

    Here is a very contrived example of using recursion to reverse a string. There is probably a better recursive method but it illustrates the point.

    Java Code:
    public class ExitExample {
    
       public static void main(String[] args) {
          new ExitExample().start();
       }
       
       public void start() {
          String [] animals = {"giraffe", "elephant", "horse", "lion", "monkey", "chicken"}; 
          for (int j = 0; j < animals.length;j++) {
               String ret = reverse(animals[j]);
               if (ret.equals("noil")) // stop printing after lion
                  break;
               System.out.println(ret);
          }
       }
    
       public String reverse(String str) {
          String v = "";
          int len = 1;
          if (str.length() > 1) {
             len = str.length();
             v = reverse(str.substring(0,len-1));
          }
          return str.substring(len-1, len) + v;
       }
    }
    Regards,
    Jim
    Last edited by jim829; 06-25-2013 at 02:45 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Passing labels to methods?

    I did it like you said... but it is still not quitting!! I don't know why...
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class herp extends JFrame {
    	private JTextField password;
    	private JButton insert;
    	public herp(){
    		super("TitleBar");
    		password = new JTextField(20);
    		insert = new JButton("CrackIt");
    		add(password);
    		add(insert);
    		HandlerClass handler = new HandlerClass();
    		insert.addActionListener(handler);
    		setLayout(new FlowLayout());	
    	}
    	private class HandlerClass implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    		Object obj1 = new Object();
    		obj1 = password.getText();
    		String obj2= new String();
    		obj2 = obj1.toString();
    		search2: for(int x=1; x<=6; x++){
    		if(checkIt(obj2, x, "")==2){
    			break search2;
    		}
    		}
    		}
    	
    	}
    	public int checkIt(String origPass, int length, String base)
    	{
    		int x = 1;
    	    search: for (char c = 'A'; c <= 'Z'; c++) {
    			String finalString = "A";
    	        if (length == 1) {
    	        	finalString = base + c;
    	        }
    	        else {
    	       if(checkIt(origPass, length - 1, base + c)==2){
    	    	   return x;
    	       }
    	        }
    			if(finalString.equals(origPass)){
    				JOptionPane.showMessageDialog(null, String.format("The pw was %s", origPass));
     				        	x = 2;
     				   		return x;
     				   		
    
    }	   
    			
    	    }
    		return x;
    }
    	}
    Last edited by swedishfished; 06-26-2013 at 06:21 AM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Passing labels to methods?

    Are you certain you are returning 2? In recursive programming, the return value can differ depending on where you are in the call stack. The last return at the bottom of the method may not be returning 2. Did you print the return value in your handler class to verify it?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. passing from methods back to main.
    By downWithJava in forum New To Java
    Replies: 5
    Last Post: 04-18-2013, 09:47 PM
  2. Help passing arrays to other methods
    By arfor in forum New To Java
    Replies: 1
    Last Post: 02-03-2013, 10:59 PM
  3. Passing variables to methods
    By KKorn429 in forum New To Java
    Replies: 3
    Last Post: 05-21-2012, 01:40 AM
  4. Arrays and passing values to methods help please.
    By toppcon in forum New To Java
    Replies: 21
    Last Post: 07-28-2011, 06:05 AM
  5. passing arguments to other methods
    By popeyito18 in forum New To Java
    Replies: 2
    Last Post: 07-03-2011, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •