Results 1 to 4 of 4
  1. #1
    viperlasson is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default Comparing two Char arrays

    Hello, this is a method that is part of my own string class that I am making for a project. The method compares two character arrays, and returns an int based on a few criteria.

    A 0 if they are equal
    A (+) number if the current instance is lexicographically greater than the one in the parameter
    A (-) number if the current instance is lexicographically less than the one in the parameter

    This is what I have so far.

    ***Note - this is put into a test main file, it isn't part of my class yet. I'm just using it to test stuff quickly.

    Java Code:
    char array1 []= new char []{'A','p','p','E'};
    	char array2 []= new char []{'A','p','p','E'};
    	int value = -9999;
    	boolean getOut = false;
    
    	for(int i=0; i<array1.length; i++)
    	{
    		while (getOut != true)
    		{
    			if (array1[i] == array2[i])
    				value = 0;
    			else if (array1[i] > array2[i])
    			{
    				getOut = true;
    				value = i+1;
    			}
    			else if (array1[i] < array2[i])
    			{
    				getOut = true;
    				value = -1*(i+1);
    			}
    		}
    	}
    	
    	System.out.println(value);
    When I try to run it my CPU goes to 100% and stays there so I might have made something bad... But wouldn't this do what I want it to do?

    ***Another note - the getOut boolean is supposed to terminate the for loop when it first finds two chars that are different and return the value.

    Thanks for any help!

    Edit - WOOPS I did make an infinite loop. Since array1 and array2 are equal right now in the code, it never exits the while loop. hmmm.
    Edit2 - Well, I tried doing it with loops and conditions, but never thought of just using "break;" in the two if statements

    All is well now :D
    Last edited by viperlasson; 01-28-2010 at 09:39 PM.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Your code doesn't make sense (i.e. it's not very intuitive). First off, is this an assignment for a class? If so, are there any limitations? If not, why not just convert the char arrays to Strings and compare the strings?

    However, let's presume it's for a class. How would you go about comparing two strings? I ask this because the same logic can be used to compare two char arrays (since, internally, a String is nothing more than a wrapper for a char array).

    You have a for loop, which is good. However, I don't get the while loop - what does it do? Can you explain the basic idea you are going for?

    I have an idea what you want, but your code doesn't look like you know what you are trying to do. So the first step is understanding "what you want to do". Then, you can write code that does it.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    In post #1, he wrote:

    Edit - WOOPS I did make an infinite loop. Since array1 and array2 are equal right now in the code, it never exits the while loop. hmmm.
    Edit2 - Well, I tried doing it with loops and conditions, but never thought of just using "break;" in the two if statements

    All is well now :D
    This thread should be marked [SOLVED] to reduce confusion.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    viperlasson is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default

    Sorry for the confusion. The conditions for the assignments was to only work with character arrays. I knew the while loop soon didn't make sense, I was thinking of a way to exit this for loop prematurely if any character was different between the two arrays. It was a method part of a larger class that was meant to 'mimic' the String class. IE we cant use string methods of any sort.

    Here's the final code.
    Java Code:
    	int compare(MyString ms1)
    	{
    		/*this method will compare the char's from one instance of MyString
    		 *to the one stored in the parameters and will output a: 
    		 *negative number telling the current instance is less than the one given in the parameter
    		 *0 if the two instances are similar
    		 *positive number if the instance stored in the parameter is more than the current instance 
    		 *The number returned is the position (starting at one) at which the two arrays varied
    		 */
    
    		int value = -999;
    		//it wanted a value so i put -999
    		
    		//these two if statements are for if the two char arrays are not equal in size, the larger one
    		//is always "bigger" and the value return is the length of the smaller array
    		if (userArray.length > ms1.userArray.length)
    			value = ms1.userArray.length+1;
    		else if (userArray.length < ms1.userArray.length)
    			value = -1*(userArray.length+1);
    		else
    		{	
    			for(int i=0; i<userArray.length; i++)
    			{		
    					//if they are equal
    					if (userArray[i] == ms1.userArray[i])
    						value = 0;
    					//current instance is larger
    					if (userArray[i] > ms1.userArray[i])
    					{
    						value = i+1;
    						break;
    					}
    					//parameter is larger, negative output
    					else if (userArray[i] < ms1.userArray[i])
    					{
    						value = -1*(i+1);
    						break;
    					}
    		
    			}
    		}
    		
    		return value;
    	}
    Last edited by viperlasson; 01-30-2010 at 08:11 AM.

Similar Threads

  1. need in help in comparing Strings
    By jaq in forum New To Java
    Replies: 1
    Last Post: 11-25-2009, 01:06 PM
  2. comparing arrays..
    By circuspeanuts in forum New To Java
    Replies: 5
    Last Post: 05-25-2009, 07:05 AM
  3. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  4. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM
  5. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 09:40 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
  •