Reputation: 31
My first for loop is not working. I tried while loop to but its also not worked. What is wrong in this code İnside of for loop program create an object for the inside of object array and take variables from the user
package kisi_ödev;
import java.util.Scanner;//input almak için kullanılacak olan sınıf için çağrılan kütüphana class kisi //kisi sınıfı { public long kn; //private değişkenkleri public yaptım public String ad, soyad; public int yas; } /** * * @author OZAN */ public class Kisi_ödev {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int size_of_objectList=1;//nesne dizisinin boyutu
Scanner input=new Scanner(System.in);//Scanner sınıfı yeni nesnesi
kisi bilgiler[]=new kisi[size_of_objectList];
for(int i=0;i<bilgiler.length;i++)//This is not working
{
bilgiler[i]=new kisi();
System.out.println("kimlik numarasını giriniz:");
bilgiler[i].kn=input.nextLong();
if(bilgiler[i].kn==0){
for(int j=0;j<bilgiler.length;j++){
if(bilgiler[j]!=null){
System.out.println("****************");
System.out.print("kimlik numrası: ");
System.out.print(bilgiler[j].kn+" ");
System.out.print("İsim: ");
System.out.print(bilgiler[j].soyad+", ");
System.out.print(bilgiler[j].ad+" ");
System.out.println(bilgiler[j].yas+" ");
System.out.println("****************");
}
}
System.out.println("shutdown");
break;
}
System.out.println("İsminizi giriniz:");
bilgiler[i].ad=input.next();
System.out.println("Soyisminizi giriniz:");
bilgiler[i].soyad=input.next();
System.out.println("Yaşınızı giriniz:");
bilgiler[i].yas=input.nextInt();
}
}
}
Upvotes: 0
Views: 61
Reputation: 4487
You must define the class, and you should ensure inputs are long, int ... like you want; otherwise, your program will end on exception.
This is a fix version of your source code:
package kisi_ödev;
import java.util.Scanner;
public class LoopTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int size_of_objectList = 2;//nesne dizisinin boyutu
Scanner input = new Scanner(System.in);//Scanner sınıfı yeni nesnesi
kisi bilgiler[] = new kisi[size_of_objectList];
try {
for (int i = 0; i < bilgiler.length; i++)//This is not working
{
bilgiler[i] = new kisi();
System.out.println("kimlik numarasını giriniz:");
bilgiler[i].kn = input.nextLong();
if (bilgiler[i].kn == 0) {
for (int j = 0; j < bilgiler.length; j++) {
if (bilgiler[j] != null) {
System.out.println("****************");
System.out.print("kimlik numrası: ");
System.out.print(bilgiler[j].kn + " ");
System.out.print("İsim: ");
System.out.print(bilgiler[j].soyad + ", ");
System.out.print(bilgiler[j].ad + " ");
System.out.println(bilgiler[j].yas + " ");
System.out.println("****************");
}
}
System.out.println("shutdown");
break;
}
System.out.println("İsminizi giriniz:");
bilgiler[i].ad = input.next();
System.out.println("Soyisminizi giriniz:");
bilgiler[i].soyad = "Yaşınızı giriniz:";
bilgiler[i].yas = input.nextInt();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Because I don't have a kisi class, I created a sample one:
class kisi {
public long kn;
public String ad;
public String soyad;
public int yas;
}
You defined the count of loop with your size_of_objectList
variable; I updated it to 2 in my version.
This is an output sample:
kimlik numarasını giriniz:
1
İsminizi giriniz:
2
Soyisminizi giriniz:
3
Yaşınızı giriniz:
4
kimlik numarasını giriniz:
0
****************
kimlik numrası: 0 İsim: 3, 2 4
****************
****************
kimlik numrası: 0 İsim: 3, 2 4
****************
shutdown
We can perfectly see the 2 loops, and the shutdown when answering 0 to the first input of a loop.
Thanks to the try/catch, you will see that parsing exception makes your program fails; for instance typing 'not a numerical value':
kimlik numarasını giriniz:
not a numerical value
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextLong(Scanner.java:2222)
at java.util.Scanner.nextLong(Scanner.java:2182)
at kisi_ödev.LoopTest.main(LoopTest.java:20)
Upvotes: 1