Java:prg suivant me donne aucun output

domxaline -  
domxaline Messages postés 188 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,
import java.io.*;
public class StuDetail 
{
  int RollNo;
}
class StudMarks extends StuDetail
{
	float sub1;
	float sub2;
	float sub3;
	float TotalMarks;
	
	void getData()
	{
		System.out.println("\nRecording Entry");
		try
		{
			BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			String sRNo,sSub1,sSub2,sSub3;
			
			System.out.println("Enter Roll No:");
			System.out.flush();
			sRNo=obj.readLine();
			RollNo=Integer.parseInt(sRNo);
			
			System.out.println("Enter Marks for Subject 1:");
			System.out.flush();
			sSub1=obj.readLine();
			sub1=Float.parseFloat(sSub1);
			
			System.out.println("Enter Marks for Subject 2:");
			System.out.flush();
			sSub2=obj.readLine();
			sub2=Float.parseFloat(sSub2);
			
			System.out.println("Enter Marks for Subject 3:");
			System.out.flush();
			sSub3=obj.readLine();
			sub3=Float.parseFloat(sSub3);
			
			TotalMarks=sub1+sub2+sub3;
		}
		catch(Exception e)
		{}	
		}
	    void display()
	    {
	    	System.out.println("\nDisplaying Record");
	    	System.out.println("Roll No:"+RollNo);
	    	System.out.println("Subject 1:"+sub1);
	    	System.out.println("Subject 1:"+sub2);
	    	System.out.println("Subject 1:"+sub3);
	    	System.out.println("Total Marks:"+TotalMarks);
	    }
	    void TotalMarks()
	    {
	    	System.out.println("\nDisplaying Record");
	    	System.out.println("Roll No:"+RollNo);	
	    	System.out.println("Total Marks:"+TotalMarks);
	    }
	}
class StudentDatabase
{
 public static void main(String []args)
 {
	 System.out.println("\n======STUDENT DATABASE=====\n");
	 
	 float HighMark_sub1;
	 float HighMark_sub2;
	 float HighMark_sub3;
	 float StudHightMark;
	 int No_of_stud=3,choice;
	 String str;
	 char c;
	 
	 try
	 {
		BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
		System.out.print("\nEnter number of students:");
		System.out.flush();
		str=obj.readLine();
		No_of_stud=Integer.parseInt(str);
		StudMarks SMobj[]=new StudMarks[No_of_stud];
		
		while(true)
		{
			System.out.println("\nChoose your choice....");
			System.out.println("1)PASS ENTRY");
			System.out.println("2)DISPLAY ALL RECORDS");
			System.out.println("3)DISPLAT TOTAL MARKS for all students");
			System.out.println("4)DISPLAT HIGHEST MARKS in each subject");
			System.out.println("5 DISPLAY student who recived highest total marks");
			System.out.println("6 Exit");
			System.out.println("ENTER YOUR CHOICE:");
			System.out.flush();
			str=obj.readLine();
			choice=Integer.parseInt(str);
			
			switch(choice)
			{
			 case 1:
				 for(int i=0; i<No_of_stud; i++)
					 SMobj[i].getData();
				 break;
			 case 2:
				 for(int i=0; i<No_of_stud; i++)
					 SMobj[i].display();
				 break;
			 case 3:
				 for(int i=0; i<No_of_stud; i++)
					 SMobj[i].TotalMarks();
				 break;
			 case 4:break;
			 case 5:break;
			 case 6:System.out.println("\nThanks for visting.....");
			 System.exit(1);	 
			}
		} 
	 }
	 catch(Exception e)
	 {}
 }
}

en exécutant mon prg,rien se passe ,aidez moi svp

A voir également:

25 réponses

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
La méthode main doit être dans une classe public sinon elle ne sera pas exécutée.
0
domxaline
 
ok j'ai comme vs avez dit
affiche le resultat suivante:
======STUDENT DATABASE=====


Enter number of students:5

Choose your choice....
1)PASS ENTRY
2)DISPLAY ALL RECORDS
3)DISPLAT TOTAL MARKS for all students
4)DISPLAT HIGHEST MARKS in each subject
5 DISPLAY student who recived highest total marks
6 Exit
ENTER YOUR CHOICE:
1

après enter your choice:
j'ai tapé le chiffre 1
et j'essaie rentrer le data mais ça se passe rien,aidez moi svp
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
C'est parce que tu as court-circuité l'affichage des exceptions, du coup tu ne vois pas que ça plante !

catch(Exception e)
{
    e.printStackTrace();
}

L'erreur venant du fait que SMobj[i] n'existe pas, il est donc impossible de faire SMobj[i].getData()
C'est à ce moment là que normalement on utilise des constructeurs : SMobj[i] = new SMobj(...);
0
domxaline
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StudentDatabase 
{
	public static void main(String []args)
	 {
		 System.out.println("\n======STUDENT DATABASE=====\n");
		 
		 float HighMark_sub1;
		 float HighMark_sub2;
		 float HighMark_sub3;
		 float StudHightMark;
		 int No_of_stud=3,choice;
		 String str;
		 char c;
		 
		 try
		 {
			BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			System.out.print("\nEnter number of students:");
			System.out.flush();
			str=obj.readLine();
			No_of_stud=Integer.parseInt(str);
			StudMarks SMobj[]=new StudMarks[No_of_stud];
			
			while(true)
			{
				System.out.println("\nChoose your choice....");
				System.out.println("1)PASS ENTRY");
				System.out.println("2)DISPLAY ALL RECORDS");
				System.out.println("3)DISPLAT TOTAL MARKS for all students");
				System.out.println("4)DISPLAT HIGHEST MARKS in each subject");
				System.out.println("5 DISPLAY student who recived highest total marks");
				System.out.println("6 Exit");
				System.out.println("ENTER YOUR CHOICE:");
				System.out.flush();
				str=obj.readLine();
				choice=Integer.parseInt(str);
				
				switch(choice)
				{
				 case 1:
					 for(int i=0; i<No_of_stud; i++)
					 SMobj[i] = new SMobj(i);
					 SMobj[i].getData();
					 break;
				 case 2:
					 for(int i=0; i<No_of_stud; i++)
						 SMobj[i].display();
					 break;
				 case 3:
					 for(int i=0; i<No_of_stud; i++)
						 SMobj[i].TotalMarks();
					 break;
				 case 4:break;
				 case 5:break;
				 case 6:System.out.println("\nThanks for visting.....");
				 System.exit(1);	 
				}
			} 
		 }
		 catch(Exception e)
		 { e.printStackTrace();}
	 }
}

j'ai erreur suivantes
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
SMobj cannot be resolved to a type
i cannot be resolved to a variable

at StudentDatabase.main(StudentDatabase.java:44)

lignes 44 est le suivant:
SMobj[i] = new SMobj(i);
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Si l'erreur est bien celle que tu donnes, la ligne 44 devrait plutôt être celle-ci :

SMobj[i].getData();
En effet, i, est une variable locale à la boucle for, qui n'autorise qu'une seule instruction, donc i n'est plus connu pour les lignes suivantes. Il faudrait que tu mettes des accolades à ta boucle for :

for(int i=0; i<No_of_stud; i++)
{
    SMobj[i] = new SMobj(i);
    SMobj[i].getData();
}
0
domxaline
 
switch(choice)
 {
   case 1:
   for(int i=0; i<No_of_stud; i++)
   {
     SMobj[i] = new SMobj(i);
     SMobj[i].getData();
     break;
  }

j'ai essayé,j'ai erreur suivante:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
SMobj cannot be resolved to a type

new SMobj(i);
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Tu ne peux mettre new SMobj(i) que si tu as un constructeur SMobj(int n), ce qui n'est probablement pas le cas...

Remarque : il ne faut pas mettre le break du switch à l'intérieur de la boucle for, sinon il va arrêter la boucle et non le switch !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
domxaline
 
Tu ne peux mettre new SMobj(i) que si tu as un constructeur SMobj(int n), ce qui n'est probablement pas le cas... 

alors quel est la solution,svp
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Ce qu'il faut, c'est faire un constructeur avec un paramètre int, ou alors supprimer l'argument i, pour utiliser le constructeur par défaut, mais visiblement tu ne sais pas faire la différence !
0
domxaline
 
j'ai corrigé mais le résultat n'es pas bon
import java.io.*;
public class StuDetail 
{
  int RollNo;
}
class StudMarks extends StuDetail
{
	float sub1;
	float sub2;
	float sub3;
	float TotalMarks;
	StudMarks(int SMobj)
	{
		
	}
	
	void getData()
	{
		System.out.println("\nRecording Entry");
		try
		{
			BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			String sRNo,sSub1,sSub2,sSub3;
			
			System.out.println("Enter Roll No:");
			System.out.flush();
			sRNo=obj.readLine();
			RollNo=Integer.parseInt(sRNo);
			
			System.out.println("Enter Marks for Subject 1:");
			System.out.flush();
			sSub1=obj.readLine();
			sub1=Float.parseFloat(sSub1);
			
			System.out.println("Enter Marks for Subject 2:");
			System.out.flush();
			sSub2=obj.readLine();
			sub2=Float.parseFloat(sSub2);
			
			System.out.println("Enter Marks for Subject 3:");
			System.out.flush();
			sSub3=obj.readLine();
			sub3=Float.parseFloat(sSub3);
			
			TotalMarks=sub1+sub2+sub3;
		}
		catch(Exception e)
		{}	
		}
	    void display()
	    {
	    	System.out.println("\nDisplaying Record");
	    	System.out.println("Roll No:"+RollNo);
	    	System.out.println("Subject 1:"+sub1);
	    	System.out.println("Subject 1:"+sub2);
	    	System.out.println("Subject 1:"+sub3);
	    	System.out.println("Total Marks:"+TotalMarks);
	    }
	    void TotalMarks()
	    {
	    	System.out.println("\nDisplaying Record");
	    	System.out.println("Roll No:"+RollNo);	
	    	System.out.println("Total Marks:"+TotalMarks);
	    }
	}



import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StudentDatabase 
{
	public static void main(String []args)
	 {
		 System.out.println("\n======STUDENT DATABASE=====\n");
		 
		 float HighMark_sub1;
		 float HighMark_sub2;
		 float HighMark_sub3;
		 float StudHightMark;
		 int No_of_stud=3,choice;
		 String str;
		 char c;
		 
		 try
		 {
			BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			System.out.print("\nEnter number of students:");
			System.out.flush();
			str=obj.readLine();
			No_of_stud=Integer.parseInt(str);
			StudMarks SMobj[]=new StudMarks[No_of_stud];
			while(true)
			{
				System.out.println("\nChoose your choice....");
				System.out.println("1)PASS ENTRY");
				System.out.println("2)DISPLAY ALL RECORDS");
				System.out.println("3)DISPLAT TOTAL MARKS for all students");
				System.out.println("4)DISPLAT HIGHEST MARKS in each subject");
				System.out.println("5 DISPLAY student who recived highest total marks");
				System.out.println("6 Exit");
				System.out.println("ENTER YOUR CHOICE:");
				System.out.flush();
				str=obj.readLine();
				choice=Integer.parseInt(str);
				
				switch(choice)
				{
				 case 1:
					 for(int i=0; i<No_of_stud; i++)
					 {
					 SMobj[i] = new StudMarks(i);
					 SMobj[i].getData();
					 }
					 break;
				 case 2:
					 for(int i=0; i<No_of_stud; i++)
						 {
						 SMobj[i] = new StudMarks(i);
						 SMobj[i].display();
						 }
					 break;
				 case 3:
					 for(int i=0; i<No_of_stud; i++)
					 {
						 SMobj[i] = new StudMarks(i);
						 SMobj[i].TotalMarks();
					 }
					 break;
				 case 4:break;
				 case 5:break;
				 case 6:System.out.println("\nThanks for visting.....");
				 System.exit(1);	 
				}
			} 
		 }
		 catch(Exception e)
		 { e.printStackTrace();}
	 }
}

======STUDENT DATABASE=====


Enter number of students:3

Choose your choice....
1)PASS ENTRY
2)DISPLAY ALL RECORDS
3)DISPLAT TOTAL MARKS for all students
4)DISPLAT HIGHEST MARKS in each subject
5 DISPLAY student who recived highest total marks
6 Exit
ENTER YOUR CHOICE:
1

Recording Entry
Enter Roll No:
10
Enter Marks for Subject 1:
3
Enter Marks for Subject 2:
4
Enter Marks for Subject 3:
5

Recording Entry
Enter Roll No:
11
Enter Marks for Subject 1:
2
Enter Marks for Subject 2:
4
Enter Marks for Subject 3:
5

Recording Entry
Enter Roll No:
12
Enter Marks for Subject 1:
6
Enter Marks for Subject 2:
7
Enter Marks for Subject 3:
8

Choose your choice....
1)PASS ENTRY
2)DISPLAY ALL RECORDS
3)DISPLAT TOTAL MARKS for all students
4)DISPLAT HIGHEST MARKS in each subject
5 DISPLAY student who recived highest total marks
6 Exit
ENTER YOUR CHOICE:
2

Displaying Record
Roll No:0
Subject 1:0.0
Subject 1:0.0
Subject 1:0.0
Total Marks:0.0

Displaying Record
Roll No:0
Subject 1:0.0
Subject 1:0.0
Subject 1:0.0
Total Marks:0.0

Displaying Record
Roll No:0
Subject 1:0.0
Subject 1:0.0
Subject 1:0.0
Total Marks:0.0

Choose your choice....
1)PASS ENTRY
2)DISPLAY ALL RECORDS
3)DISPLAT TOTAL MARKS for all students
4)DISPLAT HIGHEST MARKS in each subject
5 DISPLAY student who recived highest total marks
6 Exit
ENTER YOUR CHOICE:
3

Displaying Record
Roll No:0
Total Marks:0.0

Displaying Record
Roll No:0
Total Marks:0.0

Displaying Record
Roll No:0
Total Marks:0.0

Choose your choice....
1)PASS ENTRY
2)DISPLAY ALL RECORDS
3)DISPLAT TOTAL MARKS for all students
4)DISPLAT HIGHEST MARKS in each subject
5 DISPLAY student who recived highest total marks
6 Exit
ENTER YOUR CHOICE:
0
domxaline
 
methode void display et void TotalMarks fonctionnent pas
aidez moi svp
void display()
	    {
	    	System.out.println("\nDisplaying Record");
	    	System.out.println("Roll No:"+RollNo);
	    	System.out.println("Subject 1:"+sub1);
	    	System.out.println("Subject 1:"+sub2);
	    	System.out.println("Subject 1:"+sub3);
	    	System.out.println("Total Marks:"+TotalMarks);
	    }
	    void TotalMarks()
	    {
	    	System.out.println("\nDisplaying Record");
	    	System.out.println("Roll No:"+RollNo);	
	    	System.out.println("Total Marks:"+TotalMarks);
	    }
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
De toute évidence, le problème ne vient pas de ces deux méthodes ! La vérité est ailleurs...
0
domxaline
 
La vérité est ailleurs...

viens d' où, expliquez moi svp
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Si tu exécutes ces méthodes elles fonctionneront, le problème vient donc que tu ne les appelles pas !!!
0
domxaline
 
 case 2: 
      for(int i=0; i<No_of_stud; i++) 
       { 
       SMobj[i] = new StudMarks(i); 
       SMobj[i].display(); 
       } 
      break; 

je les appelle ici vs voyes
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Ici je ne vois que display(), pas TotalMarks(), et elle ne sera appelé que si l'on rentre dans la boucle, ce qui n'est pas le cas si No_of_Student<=0

Tu pourrais faire :

case 2:
    System.out.printf("#%d\n",No_of_stud);
    
    for(int i=0; i<No_of_stud; i++)
    {
        SMobj[i] = new StudMarks(i); 
        SMobj[i].display();
    }
    
    break;

Cependant, si ton "case 2" est censé faire l'affichage, je ne vois pas pourquoi tu construis à nouveau SMobj[i] alors que le getData() est fait dans le "case 1", comme tu l'as fait, tu vas tout effacer !
0
domxaline
 
case 2:
	 System.out.printf("#%d\n",No_of_stud);
	 for(int i=0; i<No_of_stud; i++)
	{
	  SMobj[i] = new StudMarks(i);
	  SMobj[i].display();
	}
	 break;

j'avais fait comme vs avez dit mais le résultat est le suivant
Enter number of students:2

Choose your choice....
1)PASS ENTRY
2)DISPLAY ALL RECORDS
3)DISPLAT TOTAL MARKS for all students
4)DISPLAT HIGHEST MARKS in each subject
5 DISPLAY student who recived highest total marks
6 Exit
ENTER YOUR CHOICE:
1

Recording Entry
Enter Roll No:
1
Roll No:1
Enter Marks for Subject 1:
11
Enter Marks for Subject 2:
12
Enter Marks for Subject 3:
13
TotalMarks36.0

Recording Entry
Enter Roll No:
2
Roll No:2
Enter Marks for Subject 1:
21
Enter Marks for Subject 2:
23
Enter Marks for Subject 3:
25
TotalMarks69.0

Choose your choice....
1)PASS ENTRY
2)DISPLAY ALL RECORDS
3)DISPLAT TOTAL MARKS for all students
4)DISPLAT HIGHEST MARKS in each subject
5 DISPLAY student who recived highest total marks
6 Exit
ENTER YOUR CHOICE:
2
#2

Displaying Record
Roll No:0
Subject 1:0.0
Subject 2:0.0
Subject 3:0.0
Total Marks:0.0

Displaying Record
Roll No:0
Subject 1:0.0
Subject 2:0.0
Subject 3:0.0
Total Marks:0.0

normalement il doit s'afficher comme suivant mais s'affiche pas pourquoi?
Displaying Record
Roll No:1
Subject 1:11.0
Subject 2:12.0
Subject 3:13.0
Total Marks:36.0

Displaying Record
Roll No:2
Subject 1:21.0
Subject 2:23.0
Subject 3:25.0
Total Marks:69.0
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Parce que, comme je l'ai dit tout à l'heure, tu réinitialises les valeurs de ton tableau lors de l'appel du constructeur dans le case 2 alors qu'il faudrait conserver les valeurs du case 1 (en supposant que l'on choisisse le 1 avant le 2, ce qui n'est pas évident)
0
domxaline
 
je comprend toujours pas,excusez moi svp
alors quel est la solution svp
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Enlèves cette ligne : SMobj[i] = new StudMarks(i); car elle modifie la valeur obtenue avec le getData dans le case 1. Cependant ce n'est pas aussi simple que ça, car le case 2 (et peut être d'autres) vont planter si le case 1 n'a pas été exécuté au moins une fois avant !
0
domxaline
 
j'ai fait comme vous m'avais conseillé
case 1:
	 for(int i=0; i<No_of_stud; i++)
	 {
	   //SMobj[i] = new StudMarks(i);
	   SMobj[i].getData();
	 }
	 break;
	 case 2:
	 System.out.printf("#%d\n",No_of_stud);
	 for(int i=0; i<No_of_stud; i++)
	{
	  SMobj[i] = new StudMarks(i);
	  SMobj[i].display();
	}
       break;

mais il plante pour la premier choice
ENTER YOUR CHOICE:
1
java.lang.NullPointerException
at StudentDatabase.main(StudentDatabase.java:45)
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Le truc c'est que tu ne comprends rien à ce que je t'explique !!!
C'est dans case 2 qu'il y a un problème, donc moi je te propose des solutions pour le case 2, mais toi tu laisses le case 2 identique et tu modifies le case 1, comment veux-tu que ça arrange quoi que ce soit ?

case 1:
    for(int i=0; i<No_of_stud; i++)
    {
        SMobj[i] = new StudMarks(i);
        SMobj[i].getData();
    }
    break;

case 2:
    for(int i=0; i<No_of_stud; i++)
    {
        //SMobj[i] = new StudMarks(i);
        SMobj[i].display();
    }
    break;
0
domxaline
 
mer
merci beaucoup,ça marche
0
domxaline
 
on a réussi pour display et total marks

maintenant je besoin vos conseil pour
-highest marks pour chaque sujet, et
-élève qui a reçu highest marks

pouvez vs me donnez quelque idées svp
0
domxaline
 
j'ai ces variables à ma disposition
float HighMark_sub1;
float HighMark_sub2;
float HighMark_sub3;
float StudHightMark;
il faut que je commence avec for et if
for(int i=0;i<3;i++)
{
if( )
{

}
}
après j'hésite aidez moi
0
domxaline Messages postés 188 Date d'inscription   Statut Membre Dernière intervention   10
 
il n'y a personne pour m'aider
0
domxaline Messages postés 188 Date d'inscription   Statut Membre Dernière intervention   10
 
void HighestMarks()
	    {
	    	for(int i=0;i<3;i++)
	    	{
	    		if (sub1>max1)
	    		{
	    			max1=sub1;
	    		}
	    		if (sub2>max2)
	    		{
	    			max2=sub2;
	    		}
	    		if (sub3>max3)
	    		{
	    			max3=sub3;
	    		}
	    	}
	    	System.out.println("");
	    	System.out.println("*******Highest marks in each subject with roll no******");
	    	for(int i=0;i<?.length;i++)
	    	{
	    		
	    	}

for(int i=0;i<;i++)
je ne sais pas comment remplir mon for < ?.length
veuillez m'aidez svp
0
domxaline
 
il n'y a personne pour m'aider svp
0
domxaline
 

void HighestMarks()
	    {
	    	for(int j=0;j<RollNo;j++)
	    	{
	    	  max=0;
	    	  for(int i=0;i<3;i++)
	    	  {
	    		if (RollNo>max)
	    		{
	    			max1=RollNo;
	    		}
	    		
	    	}

il n'ya personne pour me aider
où sont parti tous les génie informatique?
0
domxaline
 
void HighestMarks()
	    {
	    	
	    	for(int j=0;j<RollNo;j++)
	    	{
	    	  for(int i=0;i<3;i++)
	    	  {  
	    		  if (sub1 > max1) 
	    			  max1 = sub1;
	    		  if(sub2>max2)
	    			  max2=sub2;
	    		  if(sub3>max3)
	    			  max3=sub3;
	    		  System.out.println(max1);
	    	}
	    	System.out.println("");

en ecrivant ainsi le resultat est:
ENTER YOUR CHOICE:
4
#2
10.0
10.0
10.0

*******Highest marks in each subject with roll no******
14.0
14.0
14.0

*******Highest marks in each subject with roll no******
14.0
14.0
14.0

*******Highest marks in each subject with roll no*****

veuillez m'aidez svp
0