4 Replies - 269 Views - Last Post: 03 January 2013 - 07:49 AM Rate Topic: -----

#1 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

While loop not working correctly?

Posted 02 January 2013 - 10:17 PM

Hello peeps. I have a problem with some code I'm working on. Every thing is working pretty well except for this while loop I'm doing. What the while loop is testing is whether the the two elements in the array are equal to each other. When I test the code, they are indeed equal, but the while loop doesn't compute. I even step through in debug mode; they are indeed equal. Here's my code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;


public class Internship2  {
	static BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
	
	public static void main(String[] args) throws IOException {
		ArrayList<Integer> employeeID, lonEmployeeID, stockEmployeeID;
		Set<Integer> set;
		final int MAX_TEAM = 2;
		String[] team = new String[MAX_TEAM];
		 int numTeams, count = 0;
		
		System.out.println("Number of teams? ");
		numTeams = Integer.parseInt(r.readLine());
		
		while(numTeams < 1 || numTeams > 10000){
			System.out.println("Number of teams cannot be less than 1 or greater than 10,000. Try again.");
			System.out.println("Number of teams? ");
			numTeams = Integer.parseInt(r.readLine());
		}
		
		employeeID = new ArrayList<Integer>(numTeams);
		lonEmployeeID = new ArrayList<Integer>(numTeams);
		stockEmployeeID = new ArrayList<Integer>(numTeams);
		
		
	
		
		for(int i = 0; i < numTeams; i++){
			System.out.println("Enter the employee ID for Stockholm & London team " + Integer.toString(i));
			team = r.readLine().split(" ");
			
			/**
			 * Doesn't compute? Why?
			 */
			while(team[0] == team[1]){
				System.out.println("Employee numbers cannot be the same. Try again.");
				System.out.println("Enter the employee ID for Stockholm & London team " + Integer.toString(i));
				team = r.readLine().split(" ");
			}
			
			/**
			 * 
			 */
			employeeID.add(Integer.parseInt(team[0]));
			employeeID.add(Integer.parseInt(team[1]));
			
	
		
			
			while(Integer.parseInt(team[0]) < 1000 || Integer.parseInt(team[0]) > 1999 ){
				System.out.println("Stockholm employee ID cannot be less than 1,000 or greater than 1,999. Re-enter Stockholm employee ID: ");
				team[i] = Integer.toString(Integer.parseInt(r.readLine()));
				employeeID.add(i, Integer.parseInt(team[0]));
			}
			while(Integer.parseInt(team[1]) < 2000 || Integer.parseInt(team[1]) > 2999){
				System.out.println("London employee ID cannot be less than 2000 or greater than 2999. Re-enter London employee ID: ");
				team[i+1] = Integer.toString(Integer.parseInt(r.readLine()));
				employeeID.add(i+1, Integer.parseInt(team[1]));
			}
			
						
					}
		for(int j = 0; j < numTeams; j++){
			stockEmployeeID.add(j, employeeID.get(j*2));
			lonEmployeeID.add(j, employeeID.get((j*2) +1));
		}
		set = new HashSet<Integer>(employeeID);
		
		
	       //This is totally wrong, but how do I count or do something for the duplicats in the array list.
			if(set.size() < employeeID.size()){
				System.out.println("Duplicate");
			}
			
			
		}

		}
		



Any help would be much appreciated as usual.

Is This A Good Question/Topic? 0
  • +

Replies To: While loop not working correctly?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: While loop not working correctly?

Posted 03 January 2013 - 12:29 AM

while(team[0] == team[1]){

That is because you are using == to compare string objects, you cant compare object values using == sign. It returns false because == compare the references not the value.
You have to use the string class function equals() for strings...

This post has been edited by smohd: 03 January 2013 - 07:51 AM
Reason for edit:: Made corrections, thanks to pbl

Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: While loop not working correctly?

Posted 03 January 2013 - 06:01 AM

View Postsmohd, on 03 January 2013 - 03:29 AM, said:

You have to use the string class function equalTo() for strings...

The actual method name is equals() not equalsTo()
and for String there is also a equalsIgnoreCase() method so:

    while(team[0].equalsIgnoreCase(team[1])) {


Was This Post Helpful? 4
  • +
  • -

#4 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Re: While loop not working correctly?

Posted 03 January 2013 - 06:50 AM

Ah yes. Thank you. I am familiar with that method. Just forgot my arrays were strings haha. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: While loop not working correctly?

Posted 03 January 2013 - 07:49 AM

View Postpbl, on 03 January 2013 - 06:46 PM, said:

View Postsmohd, on 03 January 2013 - 03:29 AM, said:

You have to use the string class function equalTo() for strings...

The actual method name is equals() not equalsTo()
and for String there is also a equalsIgnoreCase() method so:

    while(team[0].equalsIgnoreCase(team[1])) {


Oh Thanks for the correction, I dont know what I was thinking about when I was posting
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1