Results 1 to 16 of 16
  1. #1
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Help with code ... newbie

    Java Code:
    import javax.swing.JOptionPane;
    
    import java.util.Arrays;
    
    public class IdenticalArrays {
    
    public static boolean equals (boolean[][] m1, boolean[][] m2){
    	    if (m1.length != m2.length) return false;
    	    for (int i = 0; i < m1.length; i++) {
    	      if (m1[i].length != m2[i].length) return false;
    	      for (int j = 0; j < m1[i].length; j++) {
    	        boolean b1 = m1[i][j];
    	        boolean b2 = m2[i][j];
    	        if (b1 != b2) return false;
    	      }
    	   }
    	   return true;
    	 }
    		
    public static void main (String[] args, boolean equals)
    		{
        	
    	String display1=" ";
    	String display2=" ";
    	
    			JOptionPane.showMessageDialog(null,"This program compares 2 lists of numbers" +  
    			"\nMake sure you put a space between numbers that you enter" + "\nFor example 12 23 45");
    			String input1 = JOptionPane.showInputDialog(null,"Input list1 with 1 space between numbers");
    			display1 = display1 + input1 + " ";		        
    			
    			String input2 = JOptionPane.showInputDialog(null,"Input list2 with 1 space between numbers");
    			display2 = display2 + input2 + " ";	
    
    			JOptionPane.showMessageDialog(null,
    	                "The system will now compare "
    	                + "\nlist1 - " + display1 
    	                + "\n to list 2 - "  + display2);
    			
    			Integer[] m1 = new Integer[input1.length()];
    			Integer[] m2 = new Integer[input2.length()];
    			
    			//boolean equals = Arrays.equals(m1, m2);  			
    	    	if(equals == false)
    	    	JOptionPane.showMessageDialog(null,"list1 -" + display1 + "\n" + "list2 -" 
    	    			+ display2 + "\nThe two arrays are not identical");
    	    	else
    	    	JOptionPane.showMessageDialog(null,"list1 -" + display1 + "\n" + "list2 -" 
    	    			+ display2 + "\nThe two arrays are identical");
    }//main
    }//class
    how do I pass parameters to main from Boolean header?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    The args for THE main() method are defined to be a String array only. The java command passes the contents of the command line as Strings in that array.

    What are you trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Re: Help with code ... newbie

    Im try to match arrays using the Boolean header

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    What is a "Boolean header"?

    Why does the code change the standard main() method's args? If you want to have a method for your purposes, give it another name. main() has a special usage.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Re: Help with code ... newbie

    The two-dimensional arrays m1 and m2 are identical if they have the same contents. Write a method that returns true if m1 and m2 are identical, using the following header:

    public static boolean equals(int[][] m1, int[][] m2)

    Write a test program that prompts the user to enter two lists of integers and displays whether the two are identical. Here are the sample runs.

    Enter list1: 51 25 22 6 1 4 24 54 6
    Enter list2: 51 22 25 6 1 4 24 54 6
    The two arrays are identical

    Enter list1: 51 5 22 6 1 4 24 54 6
    Enter list2: 51 22 25 6 1 4 24 54 6
    The two arrays are not identical

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    If the numbers are entered in one String: "1 2 3 4" the String class has a method to split the numbers at the spaces and put them into an array.

    equals is a bad choice of name for your method. The equals() method is another special and important method. You should choose a different name for your method.

    What is the equals() method supposed to do? Its args are strange. Can you explain them?

    two lists of integers
    There is some confusion here. How do you make a 2 dim array from a list of integers?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Re: Help with code ... newbie

    I agree about the syntax using equal .. that's what the problem is. However the parameters for m1 and m2 from the Boolean test are to be passed to main so if you undo the comment n line 42 it works independently of the Boolean header. remove Boolean equal from main as well - line 20

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    to be passed to main
    Here's an example of passing values to main() with a command line:
    >java TheProgram 1 2 3 4

    The Strings: "1", "2", "3", "4" are passed via the String array to the main() method in the class: TheProgram


    the Boolean header
    What is that?
    Last edited by Norm; 03-17-2014 at 02:37 AM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Re: Help with code ... newbie

    The two-dimensional arrays m1 and m2 are identical if they have the same contents. Write a method that returns true if m1 and m2 are identical, using the following header:

    public static boolean equals(int[][] m1, int[][] m2)

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    I'd call that a method header for a method that takes two 2D int arrays and returns a boolean value.

    What are the steps the program must take to solve this project? These steps will be used directly to write the code for the program. Each step should convert to one or more lines of code.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Re: Help with code ... newbie

    here is another example that works

    Java Code:
    import java.util.Arrays;
    public class ComparaisonTableau {
    /*
     * This function returns true
     * if two array are identical
     */
      public static boolean arrayIsEquals(final Object[][] array1,
                                  final Object[][] array2) {
          if (array1 == null) {
              return (array2 == null);
          }
          if (array2 == null) {
              return false;
          }
          if (array1.length != array2.length) {
              return false;
          }
          for (int i = 0; i < array1.length; i++) {
              if (!Arrays.equals(array1[i], array2[i])) {
                  return false;
              }
          }
          return true;
      }
        public static void main (String[] argv)
      	{
      	  Integer tab1[][]=	{{1, 2, 3},{4, 5, 6}};
      	  Integer tab2[][]=	{{-1, -2, 3},{ -4, -5,-6}};
      	  Integer tab3[][]=	{{1, 2, 3},{4, 5, 6}};
      	  // Tester les tableaux
      	  System.out.println ("Tab1 == Tab2? "+arrayIsEquals(tab1,tab2));   //false
      	  System.out.println ("Tab1 == Tab3? "+arrayIsEquals(tab1,tab3));   //true
        }
    }

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    that works
    Does that mean you have solved your problem?
    If not, please explain.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Re: Help with code ... newbie

    nope ... it doesn't work with my code and I cant see the problem for some reason the parameter equals is not passed back to main()

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    the parameter equals is not passed back to main()
    Do you mean this: the boolean value is not returned to where the method was called in the main() method?
    Vocabulary is important.

    Here is what you said:
    Java Code:
      public Object aMethod(Object equals) {
           ....
           return equals;    //  return the parameter equals to the caller
       }
    it doesn't work with my code
    Please explain and post the code you are talking about.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    navpilot is offline Member
    Join Date
    Mar 2014
    Location
    Forida
    Posts
    8
    Rep Power
    0

    Default Re: Help with code ... newbie

    the Boolean method header does not work when requested by line 43 ... if(equals == false). Sorry for my vocab I'm a newbie trying to do complex double array matching.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Help with code ... newbie

    When you say "Boolean header"do you mean the equals() method?
    Java Code:
    //boolean equals = Arrays.equals(m1, m2);
    does not work when requested by line 43
    What do you mean by "does not work"?
    Is there an error?
    Are the results different from what you expected?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Java Newbie NEED HELP with simple CODE
    By xcaldk74 in forum New To Java
    Replies: 6
    Last Post: 01-17-2012, 01:48 AM
  2. Some code help for a newbie?
    By atac57 in forum New To Java
    Replies: 3
    Last Post: 01-03-2012, 06:06 AM
  3. newbie needs help w/ code in android
    By mailboxo71 in forum Android
    Replies: 3
    Last Post: 01-21-2011, 06:49 AM
  4. Newbie need help w/ Java code :)
    By mailboxo71 in forum New To Java
    Replies: 5
    Last Post: 01-21-2011, 06:48 AM
  5. [newbie] messed up code
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-31-2009, 04:29 PM

Posting Permissions

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