Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is not give me all personelles
what can i do?
Quote:
Girilecek Departman Sayısı >> 2
DEPARTMAN NO >> 123123
DEPARTMAN ADI >> QWEQWE
QWEQWE Departmanındaki Personel Sayısı >> 2
Personel ADI >> A
Personel SOYADI >> B
Personel YASI >> 1
Personel ADI >> D
Personel SOYADI >> E
Personel YASI >> 2
DEPARTMAN NO >> 321321
DEPARTMAN ADI >> TTTT
TTTT Departmanındaki Personel Sayısı >> 2
Personel ADI >> E
Personel SOYADI >> F
Personel YASI >> 4
Personel ADI >> Y
Personel SOYADI >> H
Personel YASI >> 4
-----------------------------
1- Tüm Departman Bilgilerini Listele
2- Departman Adına Göre Arama Yap
3- Personel Adına Göre Arama Yap
4- Servis Güzergâhına Göre Arama Yap
5- Yaşı En Büyük ve En Küçük Personel/Personelleri Bul
6- Çıkış
-----------------------------
İşlem Numarasını Girin >> 1
-----------------------------
Departman No: 123123 Departman Ad: QWEQWE
1. Personel 2 D E 2
1. Personel 2 D E 2
Departman No: 321321 Departman Ad: TTTT
-----------------------------
-----------------------------



And this another error


Quote:
Girilecek Departman Sayısı >> 3
DEPARTMAN NO >> 123
DEPARTMAN ADI >> AAA
AAA Departmanındaki Personel Sayısı >> 1
Personel ADI >> ASD
Personel SOYADI >> DSA
Personel YASI >> 1
DEPARTMAN NO >> 321
DEPARTMAN ADI >> BBB
BBB Departmanındaki Personel Sayısı >> 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at odev.AnaSayfa.main(AnaSayfa.java:31)
java:31
departmanlar[i].personeller[i] = new Personel(departmanlar[i].getdepNo());


What I have tried:

Java
package odev;

import java.util.Scanner;

public class AnaSayfa {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		System.out.print("Girilecek Departman Sayısı >> ");
		int length = sc.nextInt();

		if (length > 0) {

			Departman[] departmanlar = new Departman[length];

			for (int i = 0; i < departmanlar.length; i++) {

				System.out.print("DEPARTMAN NO >> ");
				int dn = sc.nextInt();
				System.out.print("DEPARTMAN ADI >> ");
				String da = sc.next();
				departmanlar[i] = new Departman(dn, da);

				System.out.print(da + " Departmanındaki Personel Sayısı >> ");
				int ps = sc.nextInt();
				if (ps > 0) {
					departmanlar[i].createPerList(ps);
					
					while (ps != 0) {
						departmanlar[i].personeller[i] = new Personel(departmanlar[i].getdepNo());
						System.out.print("Personel ADI >> ");
						String pa = sc.next();
						departmanlar[i].personeller[i].setperAd(pa);
						System.out.print("Personel SOYADI >> ");
						String psa = sc.next();
						departmanlar[i].personeller[i].setperSoyad(psa);
						System.out.print("Personel YASI >> ");
						int py = sc.nextInt();
						departmanlar[i].personeller[i].setperYas(py);
						
					ps--;
					}

					

				} else {
					continue;
				}

			} // for
			while (true) {
				System.out.println("-----------------------------");
				System.out.println("1- Tüm Departman Bilgilerini Listele");
				System.out.println("2- Departman Adına Göre Arama Yap");
				System.out.println("3- Personel Adına Göre Arama Yap");
				System.out.println("4- Servis Güzergâhına Göre Arama Yap");
				System.out.println("5- Yaşı En Büyük ve En Küçük Personel/Personelleri Bul");
				System.out.println("6- Çıkış");
				System.out.println("-----------------------------");
				System.out.print("İşlem Numarasını Girin >> ");
				int giris = sc.nextInt();

				if (giris == 1) {
					System.out.println("-----------------------------");
					int ct = 0;
					
					for (int i = 0; i < departmanlar.length; i++) {
						departmanlar[i].showDepartmanData();
						while (ct != departmanlar[i].personeller.length) {
							departmanlar[i].personeller[i].showPerData(i);
							ct++;
						}
					} // for departman

					System.out.println("-----------------------------");

				} else if (giris == 2) {
					// for(int i=0;i<departmanlar[i].personeller.length;i++) {
					// System.out.println(departmanlar[i].personeller[i].getperdepNo());
					// }

				} else if (giris == 3) {
					for (int i = 0; i < departmanlar.length; i++) {
						System.out.println(departmanlar[i].getdepAd() + " Departmanındaki Personel Sayısı: "
								+ departmanlar[i].personeller.length);
					}

					// System.out.println(asd.getPerdepNo());

				} else if (giris == 4) {
					// System.out.println(depOBJ.getLength2());

				} else if (giris == 5) {
					System.out.println("5 numaralı tuşa baştınız");

				} else if (giris == 6) {
					System.out.println("Çıkış Yapıldı.");
					break;
				} else if (giris < 1 || giris > 6) {
					System.out.println("Hatalı Giriş!");
					System.out.print("Devam için birsey yazıp Enter Basınız >>");
					sc.next();

				}
			} // while

		} // length if bloğu
		else {
			System.out.println("Negatif Sayı veya Sıfır Giremezsiniz!");
		}
	}// main bloğu
}// class bloğu

Java
package odev;

public class Personel {
	
	private int depNo;
	private int perID;
	private String perAd;
	private String perSoyad;
	private int perYas;
	private static int perSayisi = 0;
	
	
	
//Constructor
public Personel(int depNo) {
	this.depNo = depNo;
	perID = ++perSayisi;
}
//setter
public void setperAd(String perAd) {this.perAd = perAd;}
public void setperSoyad(String perSoyad) {this.perSoyad=perSoyad;}
public void setperYas(int perYas) {this.perYas = perYas;}

//getter
public int getperdepNo() {return depNo;}

public String getperAd() {return perAd;}

public String getperSoyad() {return perSoyad;}

public int getperYas() {return perYas;}


public int getID() {return perID;}

public void showPerData(int sayi) {
	 System.out.println((sayi+1)+". Personel "+this.getID()+" "+this.getperAd()+" "+this.getperSoyad()+" "+this.getperYas());
}//showPerData


}//CLASS

Java
package odev;

public class Departman {
	private int depNo;
	private String depAd;
	public Servis servis[];
	public Personel personeller[];

	public Departman() {

	}

	public Departman(int depNo, String depAd) {
		this.depNo = depNo;
		this.depAd = depAd;
	}

	public void createPerList(int n) {
		this.personeller = new Personel[n];
	}

	public void setdepNo(int depNo) {
		this.depNo = depNo;
	}

	public void setdepAd(String depAd) {
		this.depAd = depAd;
	}

	public int getdepNo() {
		return depNo;
	}

	public String getdepAd() {
		return depAd;
	}

	public void showDepartmanData() {
		System.out.println("Departman No: " + this.getdepNo() + " Departman Ad: " + this.getdepAd());
	}

}
Posted
Updated 14-Nov-20 22:31pm
v4

Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at odev.AnaSayfa.main(AnaSayfa.java:31)
java:31
departmanlar[i].personeller[i] = new Personel(departmanlar[i].getdepNo());

No matter what is the detail, the message means that you try to do something in a part of an array/list that does not exist.
One can suspect that the usage of same index for 2 parts of the code is the problem
Java
departmanlar[i].personeller[i] = new Personel(departmanlar[i].getdepNo());
             ^              ^

The only way to know the detail is to use the debugger to inspect the variables at error.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
You are using the wrong indexer for the personeller array. It should be using ps, the number of personeller entries, not i which is the number of departmanlar entries.
Java
departmanlar[i].createPerList(ps); // ps is the count of personnel entries for this department

while (ps != 0) {
    departmanlar[i].personeller[i] = new Personel(departmanlar[i].getdepNo()); // using the wrong index value for personneller
    System.out.print("Personel ADI >> ");
    String pa = sc.next();
    departmanlar[i].personeller[i].setperAd(pa);  // still using the wrong index value for personneller
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900