hi,

I have this method

public void record() 
    {
    	
    	Scanner input = new Scanner( System.in );
    	int sid;
    	
    		
    	for ( int counter = 0; counter < length; counter++ )
    		{
    		
    		
    		System.out.println("Student " + (counter+1) + " Record");
    		System.out.println("****************");
    		System.out.print("Enter SID: ");
           	a[ counter ] = input.nextInt();
           	sid=a[counter];
           	if (isUnique(sid))
    		{
           	System.out.print("Enter SName: ");
           	b[ counter ] = input.next();
           	System.out.print("Enter SGrade: ");
           	c[ counter ] = input.nextInt();
           	System.out.println("****************");
           	}
           	else 
           	{
           		System.out.println("Student id is not unique");
           	}
           }
           

    }

which in part of it call another method which check the if a student id exist in an array or not.

public boolean isUnique(int id)
    {
    	for(int i=0; i<length;i++)
    	{
    		if(id == a[i])
    		{
    			return false;
    		}
    	}
 	return true;
    }

The code works fine but I keep always getting
that the Student id is not unique.

Could anyone here help finding what's wrong with this code.

Thanks in advance.

What is the "length" variable? You should be looping to "a.length" over that array. If "length" is not the same as the array length, you are missing parts of your array in the loop.

What is the "length" variable? You should be looping to "a.length" over that array. If "length" is not the same as the array length, you are missing parts of your array in the loop.

It is a variable for the length of the array

Here the full code

I use two classes this the first one

/**
 * @(#)operationTest.java
 *
 *
 * @author : Fahad Althubaity
 * @version 1.00 2008/6/2
 */
 import java.util.Scanner;

public class operationTest {

    public static void main( String args[] ) 
    {
    	Scanner input = new Scanner( System.in );
    	operation o = new operation();
    	int select=0;
    
    	
    	while (select != 4){
    	o.menu();
    	
    	select = input.nextInt();
    	
    	switch(select)
    	{
    		case 1:	
					o.record();
					break;
    	
    	
    		case 2:
					o.printall();
					break;			
    	
    	
    		case 3:
					o.sturecord();
					break;
					
			case 4:
    				
					o.exit();
					break;
    	
    	
    		default:
					System.out.println("sorry try again");
					break;
		}
		
    	}
    	
    }
    
}

and this the second one

/**
 * @(#)operation.java
 *
 *
 * @author : Fahad Althubaity
 * @version 1.00 2008/6/2
 */
import java.util.Scanner;

public class operation {
	final int length = 4;
	int a[] = new int[ length ]; 
    String b[] = new String[ length ]; 
    int c[] = new int[ length ];
     
	public void menu()
	{
		System.out.println();
    	System.out.println( "Welcome to Project5 by Fahad Althubaity." );
    	System.out.println();
    	System.out.println( "1) Enter all student records." );
    	System.out.println( "2) Print all student records." );
    	System.out.println( "3) Print a student record" );
    	System.out.println( "4) Exit." );
    	System.out.println();
    	System.out.print( "Please choose one from the above menu or 4 to Exit : " );
	}

    public void record() 
    {
    	
    	Scanner input = new Scanner( System.in );
    	int sid;
    	
    		
    	for ( int counter = 0; counter < length; counter++ )
    		{
    		
    		
    		System.out.println("Student " + (counter+1) + " Record");
    		System.out.println("****************");
    		System.out.print("Enter SID: ");
           	a[ counter ] = input.nextInt();
           	sid=a[counter];
           	if (isUnique(sid))
    		{
           	System.out.print("Enter SName: ");
           	b[ counter ] = input.next();
           	System.out.print("Enter SGrade: ");
           	c[ counter ] = input.nextInt();
           	System.out.println("****************");
           	}
           	else 
           	{
           		System.out.println("Student id is not unique");
           	}
           }
           

    }
    	
    	
    	

    
    public void printall() 
    {
    	for (int i=0; i<length;i++)
    	{
    		System.out.println("Student " + (i+1) + " Record");
    		System.out.println("SID: " + a[i]);
    		System.out.println("SName: " + b[i]);
    		System.out.println("SGrade: " + c[i]);
    		System.out.println("****************");
    		
       	}
    }
    
    
    public void sturecord() 
    {
    	Scanner input = new Scanner( System.in );
    	System.out.print("Enter Student ID: ");
    	
    	int stuid = input.nextInt();
    	
    	for(int i=0 ; i<length ; i++)
    	{
    		if(stuid == a[i])
    		{
    			System.out.println("SID: " + a[i]);
    			System.out.println("SName: " + b[i]);
    			System.out.println("SGrade: " + c[i]);
    			break;
    		}
    	}
    	
	
    }
    
    public void exit() 
    {
    	System.out.println( "Bye." );
    }
    
    public boolean isUnique(int id)
    {
    	for(int i=0; i<length;i++)
    	{
    		if(id == a[i])
    		{
    			return false;
    		}
    	}
 	return true;
    }        
}

I hope this clears the problem

You are putting the id into the array before you check if it's unique, so of course the id will be found in the array

a[ counter ] = input.nextInt();
           	sid=a[counter];
           	if (isUnique(sid))

I tried this

a[ counter ] = input.nextInt();
           	//sid=a[counter];
           	if (a[ counter ] )

and it is the same problem

How I can get the value of a[counter] and put it in the variable sid to check it?

Have you tried

sid = input.nextInt();
if (isUnique(sid)){
  a[counter] = sid;
 ...

The problem is just that you are putting the id in the array and then searching that array to see if that id is already there. Search first, then add it if it's not found.

Thank you very much

the problem has been solved

I really appreciate your quick replies to all my questions.

thanks again Ezzaral

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.