1 2 3 Previous Next 35 Replies Latest reply: Feb 4, 2009 8:00 PM by 807588 RSS

    Returning two arrays and comparing values

    807588
      I am working on an assignment for a class. I have come as far as I can I feel and need help now. I am trying to figure out how to return two arrays in one method and in the other method pass through these 2 arrays to see if the elements within are equal.
      (e.g. 121 144 19 161 19 144 19 11
      11 121 144 19 161 19 144 19 These are equal since same length and same amount of each element like 19 appears 3 times, 144 appears 2 times, etc.)

      Now I take those values and store each line into its own array. In the first method I am supposed to return an int [] array but I cannot figure out how to return both without combining them into one and printing it. In the other method we are to find out if the arrays/strings are equal.

      here is my code thus far(sorry if it looks offset/poorly formatted...copied from textpad to here):
      import java.io.*;
      import java.util.StringTokenizer;
      
      public class StringChecker
      {
          public static void main(String [] args)
          {
              StringChecker sc = new StringChecker();
              String test = "file.txt";
              sc.readInput(test);
          }
      
          public void readInput(String s)
          {
              try
              {
                  FileReader fr = new FileReader(s);
                  LineNumberReader lr = new LineNumberReader(fr);
      
                  // Read from the FileReader.
                  String line = " ";
                  StringTokenizer st = new StringTokenizer(line);
                  int [] nums1 = new int [20];
                  int [] nums2 = new int [20];
                  int [] allNums = new int[nums1.length + nums2.length];
                  int count = 0;
                  int count2 = 0;
      
                   while ((line = lr.readLine()) != null )
                   {
                       st = new StringTokenizer(line);
                      while(st.hasMoreTokens())
                      {
                          if(lr.getLineNumber() == 0)
                          {
                              String text = st.nextToken();
                              nums1[count] =  Integer.parseInt(text);
                              count++;
                          }
                          else
                          {
                              String text = st.nextToken();
                              nums2[count] = Integer.parseInt(text);
                              count++;
                          }
                      }
                  }
      
                  // Close the LineNumberReader and FileReader.
                  fr.close();
                  lr.close();
      
                  int nums1Index = 0;
               int nums1Count = nums1.length;
               int allNumsIndex = allNums.length;
      
                   // Copy over array nums1, but only if the element is less than nums2
                     while (nums1Index < nums1Count)
                     {
                     if(nums1[nums1Index] < nums2[nums1Index])
                    {
                           allNums[nums1Index]= nums1[nums1Index];
                      }
      
                      nums1Index++;
      
                       }
      
      
                        // Copy over nums2 starting where array nums1 left off
                    for(int nums2Index = 0; nums2Index < nums2.length; nums2Index++)
                    {
                          allNums[nums1Index] = nums2[nums2Index];
                        nums1Index++;
                    }
      
                     for (int i = 0; i < allNums.length; i++)
                     {
                         System.out.println(allNums);
                     }

      }


      catch (FileNotFoundException e)
      {
      e.printStackTrace();
      }

      catch (IOException e)
      {
      System.out.println("IOException: " + e.getMessage());
      }

      catch (NumberFormatException e)
      {

      }
      }

      public boolean isSame(int, int[] numsFromS2)
      {
      if(numsFromS1.length != numsFromS2.length)
      {
      return false;
      }
      else
      {
      for (int i = 0; i < numsFromS1.length; i++)
      {
      for (int j = 0; j < numsFromS2.length; j++)
      {
      if (numsFromS1[i] == numsFromS2[j])
      {
      if(numsFromS2[i] != numsFromS2[j])
      {
      return false;
      }
      }
      }
      }
      return true;
      }
      }
      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        • 1. Re: Returning two arrays and comparing values
          699554
          I suggest returning a 2-dimensional array
          private static int[][] doSomething(int[] array1, int[] array2){
            int[][] temp = new int[2][];
            temp[0] = array1;
            temp[1] = array2;
            return temp;
          }
          
          public static void main(String[] args){
            int[] array1 = {1,2,3};
            int[] array2 = {3,4,5};          
            int[][] result = doSomething(array1, array2); //Calls method with two 1 dimensional arrays
          
            for(int i=0; i<result.length; i++){
              for(int j=0; j<result.length; j++){
          System.out.println(result[i][j]); //Iterates through the 2-dimensional array, printing variable
          }
          }
          }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
          • 2. Re: Returning two arrays and comparing values
            699554
            Additional note: if you are only allowed to return a one-dimensional array then i suggest re-reading what the method is suppose to do and what data you need to return.
            • 3. Re: Returning two arrays and comparing values
              807588
              What you're trying to do doesn't seem to make sense.
              1. One method can't return two different values at once. That kinda of defeats the point of method returns.
              2. You only have one method and it's of type boolean. Which means it returns a boolean value. So, where are you supposed to return an int[] ?
              3. You've got at least two syntax errors.
              4. While, yes the two arrays have the same numbers, unless there isn't a reason why you can't, I'd SORT them first. It would make comparing them much easier.
              5. Your isSame method is just wrong. In so many ways.

              I won't point it out what's wrong just yet (and preferably never). You've got learn somehow.

              It sounds like you may be confused on returning values, passing parameters, and possibly, method invocation. Among other things.
              • 4. Re: Returning two arrays and comparing values
                807588
                The instructions are the StringChecker class is to have the following methods:
                int [] readInput(String s)
                boolean isSame(int[] a, int[] b)

                I keep thinking it would work better/needs a two-dimensional array but the instructions show different. The assignment is Write a class namd StringChecker to check if two strings contain the same elements. You are to read two strings from the file. The strings are to contain several numbers separated by space.

                The instructions go on to talk about exceptions we need to incorporate and use StringTokenizer to separate the elements of the strings, covert the elements to integers (parse), and populate 2 arrays with them. We are also asked to note that the two strings may not even be the same length.

                So yea thats about it as far as instructions.
                • 5. Re: Returning two arrays and comparing values
                  699554
                  boolean isSame(int[] a, int[] b)

                  Method takes 2 int[]'s as parameters
                  Method compares the 2 int[]'s
                  Returns true if they are equal, false otherwise

                  Pointless of me writing the code segment for you before, what they are asking is straight forward.
                  • 6. Re: Returning two arrays and comparing values
                    807588
                    my readInput method is suppose to be public int [] readInput(String s) not public void readInput(String s) but i couldnt figure out how to return the values so made it void and printed the values through a loop which is also somewhat broken.

                    Edited by: Lgndryhr on Feb 3, 2009 10:53 PM

                    also, this is only my 2nd semester of programming. my professor is not helpful in any way and teachers very poorly. i am trying to gain a better understanding of all this and hope i can be guided in the right direction.

                    Edited by: Lgndryhr on Feb 3, 2009 10:57 PM
                    • 7. Re: Returning two arrays and comparing values
                      699554
                      Lgndryhr wrote:
                      but i couldnt figure out how to return the values so made it void and printed the values through a loop which is
                      also somewhat broken.>
                      What part of returning values in a method are you stuck with.

                      Edited by: Melanie_Green on Feb 3, 2009 11:02 PM
                      • 8. Re: Returning two arrays and comparing values
                        masijade
                        Well, by the second semester they have definately covered the "return" command, so I don't know how you could not figure out how to return an array. You build an array within the method and return it the "return" instruction while declaring the method to be int[] and not void.

                        And, of course, that would probably mean printing the prompts and getting the input from within that method, and not from main, of course. Although, I would probably create another method that prompts for input and retreives a single value that the first method can use, but that's up to you. Nothing about your assignment say you cannot create additional methods.
                        • 9. Re: Returning two arrays and comparing values
                          807588
                          in the public int [] readInput(String s) method

                          I made it public void readInput(String s) but we are supposed to make it publiv int [] readInput(String s)

                          I am confused what/how to return whatever it is. My impression is we are supposed to return the values of the 2 lines.
                          • 10. Re: Returning two arrays and comparing values
                            699554
                            Lgndryhr wrote:
                            I am confused what/how to return whatever it is. My impression is we are supposed to return the values of the > 2 lines.
                            If you dont know whatever it is then how are we meant to?
                            Heres a second example of returning a value.
                            public class classA{
                              pubic static void main(String[] args){
                                int num1 = 1; 
                                int num2 = 2;
                                int[] result = returnAnArray(num1, num2); //I am passing 2 int parameters to this method, i expect an int[] to be returned
                              }
                             
                              public static int[] returnAnArray(int n1, int n2){
                                int[] tempArray = new int[2]; // I will return this once i populate it with data
                                tempArray[0] = n1;
                                tempArray[1] = n2;
                                return tempArray;  // This is how i return the int[] called tempArray, scroll to the main method and this will be stored in the int[] variable called result.
                              }
                            }
                            • 11. Re: Returning two arrays and comparing values
                              807588
                              read two strings from the file. The strings are to contain several numbers separated by space.
                              You read a string from the file. You use the method
                              public int[] readInput(String s)
                              Now you have an int array. You read the second string from the file. You use the same method, and now you have two int arrays. You compare the int arrays with
                              public boolean isSame(int[] a, int[] b)
                              That seems to be your homework assignment. It tells you that you can use a StringTokenizer in the readInput method to extract the numbers in a string and make an int[] array.
                              • 12. Re: Returning two arrays and comparing values
                                masijade
                                Maxideon wrote:
                                It tells you that you can use a StringTokenizer in the readInput method to extract the numbers in a string and make an int[] array.
                                Not that I would though. A simple "split()" will do the job and StringTokenizer has it's problems (although for a simple ASCII text String it is usable).
                                • 13. Re: Returning two arrays and comparing values
                                  807588
                                  StringTokenizer seems to be the norm for homework assignments. I'm not sure why....
                                  • 14. Re: Returning two arrays and comparing values
                                    807588
                                    So I took a break from this program and have come back to it. Based on people's responses and my re-reading as well as attempt to understand what to do in this program I have come to realize a few things as well as now have more questions.

                                    I understand in the public int [] readInput(String s) I am basically passing through a string (in this case the file.txt), reading the lines of information, storing each line to its own array while parsing each element as I read it, then I take the two arrays and feed them into one big array so I can return the elements read.

                                    In the public boolean isSame(int[] a, int[] b) I am taking the two arrays from readInput, comparing them, and returning whether they are equivalent or not using a boolean.

                                    Now my question(s). How do I take two arrays and store both of their information into one big array so I can return them effectively in readInput? Is there a way to take the two arrays from readInput and pass them through the isSame method to be compared for equivalence?

                                    I am trying to understand how to do this effectively. Yes, I know how to do return statements and basics from first semester. I am just struggling with this assignment and understanding how to do this correctly. I tried meeting with my teacher, but they did little to no help in explaining it.
                                    1 2 3 Previous Next