Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Returning two arrays and comparing values

807588Feb 4 2009 — edited Feb 4 2009
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;
}
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2009
Added on Feb 4 2009
35 comments
965 views