how to find already values in the array element

Tags: ,



I need to insert a student ID to the id array and find out whether or not that number already exists. So now when i run this program first it asks me to input student ID then check the whole ID array to check if any other numbers equal that ID. If the ID doesn’t already exist, it is stored, after which the program asks for the student name.

Next round if I give an ID that’s already stored, it gives me a message “Already Exists” and then tells me “input Student id” again and I want to give it a number that it doesn’t already have. So if the ID doesn’t already exist, then it should store it at the index i in the array. However, this does not work properly as I want.

This is what I want the input and output to look like

Input the id for 0 : s001
Enter Student name : suraj
Input the id for 1 : s002
Enter Student name : sanuka
Input the id for 2 : s002
this id is already exists. Input the id for 2 :

However, this is what it gives me

Input the id for 0 : s001
Enter Student name : suraj
Input the id for 1 : s002
Enter Student name : sanuka
Input the id for 2 : s002
this id is already exists.( this is what i want)
Input the id for 2 : s003
Enter Student name : s002
Input the id for 3 : s001 (but some times when i input already stored number,it doesn’t work like this line)
this id is already exists.
Enter Student name :(if that already exists, “Enter student name ” have displayed for me.)

My code

class Demo2{
        public static void main(String args[]){
            Scanner input=new Scanner(System.in);

            String [] id=new String[5];
            String [] name=new String[id.length];

            L1: for (int i = 0; i < id.length; i++){
                //id[0]="d";
                System.out.print("Input the id for  "+i+" : ");
                String tempory=input.next();

                L2: for (int j = 0; j < id.length; j++){
                    if(tempory.equals(id[j])){

                        System.out.println("this id is already exists.");
                        i--;
                        break L2;
                    }

                }

                if(!tempory.equals(id[i])){
                    id[i]=tempory;

                    System.out.print("Enter Student name : ");
                    name[i]=input.next();

                }
            }
            System.out.println(Arrays.toString(id));
            System.out.println(Arrays.toString(name));
        }

}

please solve me this problem

Answer

You’re checking if tempory.equals(id[j]) and if so confirming the studentId exists. Within this your then doing i-- (too early). Use a variable and update it to true when a match is found. Something like:

import java.util.*;

class Demo2 {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);

        String[] id = new String[5];
        String[] name = new String[id.length];

        for (int i = 0; i < id.length; i++) {
            //id[0]="d";
            System.out.print("Input the id for  " + i + " : ");
            String tempory = input.next();

            // check if ID exists?
            boolean studentIdFound = false; // try to make it true if there's a match
            for (int j = 0; j < id.length; j++) {
                if (tempory.equals(id[j])) {
                    System.out.println("this id is already exists.");
                    studentIdFound = true;  // <<< removed i-- here as it was too early
                    break;
                }
            }

            // either the studentId was found, or not found so...

            if (!studentIdFound) {
                id[i] = tempory;

                System.out.print("Enter Student name : ");
                name[i] = input.next();
            } 
            if (studentIdFound) {
                i--; // retry
            }
        }
        System.out.println(Arrays.toString(id));
        System.out.println(Arrays.toString(name));
    }

}


Source: stackoverflow