Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-12-2012, 10:02 PM   PM User | #1
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
Problem with .equals()

I can't figure out why my program thinks that the integer 1 is equal to integer 2 (and integer 3 and so on). What am I overlooking here?

Code:

import java.lang.*;

public class MyFour<T>
{
	private T item1, item2, item3, item4;
	private static boolean same;
	private static String word = "word";
	
	// "it's contructor recieves values for setting all four items." 
	public MyFour(T i1, T i2, T i3, T i4)
	{
		item1 = i1;
		item2 = i2;
		item3 = i3;
		item4 = i4;	
	}

	//a method that returns true if all 4 of the parameters are the same
	public static boolean allEqual(MyFour ex)
	{
		same = false;
		
		if((ex.item1).equals(ex.item3)); 
		{
			same = true;
		}
		
		System.out.println(ex.item1);
		System.out.println(ex.item3);
		
		return same;
	}
	
	//a method that shifts all the items up one position 
	public void shiftLeft(T i1, T i2, T i3, T i4)
	{
		T one, two, three, four;
		
		one = i2;
		two = i3;
		three = i4;
		four = i1;
	}
	
	public static void toString(MyFour ex)
	{
		String a = ex.item1.toString();
		String b = ex.item2.toString();
		String c = ex.item3.toString();
		String d = ex.item4.toString();
					
		System.out.println("(" + a + ", " + b + ", " + c + ", " + d + ")");
	}
	
	public static void main(String[] args)
	{
		MyFour<String> ex = new MyFour<String>(word, word, word, word);
		toString(ex);
		System.out.println(allEqual(ex));
		
		MyFour<Integer> ex2 = new MyFour<Integer>(1, 2, 3, 4);
		toString(ex2);
		System.out.println(allEqual(ex2));
	}
}
trantommyd is offline   Reply With Quote
Old 09-12-2012, 10:08 PM   PM User | #2
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
I always figure it out minutes after posting here.....
I believe my problem lies with declaring the boolean value same as a static variable. I rewrote the whole allEquals() method to, very simply:

Code:
	public boolean allEqual(){
	 if(item1.equals(item2)) return true;
	 return false;
	 }
trantommyd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:15 PM.


Advertisement
Log in to turn off these ads.