<java>Exception in thread "main" j
domxaline
-
domxaline -
domxaline -
Bonjour,
j'ai essayé le prg suivant:
en compilant j'ai erreur suivante
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at exercice.Clavier.lireInt(Clavier.java:53)
at exercice.premier.main(premier.java:13)
la ligne Clavier.java:53 est
la ligne premier.java:13 est
aidez moi s'il vous plaît
j'ai essayé le prg suivant:
package exercice;
import exercice.Clavier;
public class premier
{
public static void main(String[]args)
{
int i,n = 0,som;
som=0;
for(i=0;i<4;i++)
{
System.out.println("donnez un entier");
n=Clavier.lireInt();
som+=n;
}
System.out.println("Somme:"+som);
}
}
import java.io.* ;
public class Clavier
{
public static String lireString () // lecture d’une chaine
{
String ligne_lue = null ;
try
{
InputStreamReader lecteur = new InputStreamReader (System.in) ;
BufferedReader entree = new BufferedReader (lecteur) ;
ligne_lue = entree.readLine() ;
}
catch (IOException err)
{
System.exit(0) ;
}
return ligne_lue ;
}
//Annexe D
//La classe Clavier
public static float lireFloat () // lecture d’un float
{
float x=0 ; // valeur a lire
try
{
String ligne_lue = lireString() ;
x = Float.parseFloat(ligne_lue) ;
}
catch (NumberFormatException err)
{
System.out.println ("*** Erreur de donnee ***") ;
System.exit(0) ;
}
return x ;
}
public static double lireDouble () // lecture d’un double
{
double x=0 ; // valeur a lire
try
{
String ligne_lue = lireString() ;
x = Double.parseDouble(ligne_lue) ;
}
catch (NumberFormatException err)
{
System.out.println ("*** Erreur de donnee ***") ;
System.exit(0) ;
}
return x ;
}
public static int lireInt () // lecture d’un int
{
int n=0 ; // valeur a lire
try
{
String ligne_lue = lireString() ;
n = Integer.parseInt(ligne_lue) ;
}
catch (NumberFormatException err)
{ System.out.println ("*** Erreur de donnee ***") ;
System.exit(0) ;
}
return n ;
}
// programme de test de la classe Clavier
public static void main (String[] args)
{ System.out.println ("donnez un flottant") ;
float x ;
x = Clavier.lireFloat() ;
System.out.println ("merci pour " + x) ;
System.out.println ("donnez un entier") ;
int n ;
n = Clavier.lireInt() ;
System.out.println ("merci pour " + n) ;
}
}
en compilant j'ai erreur suivante
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at exercice.Clavier.lireInt(Clavier.java:53)
at exercice.premier.main(premier.java:13)
la ligne Clavier.java:53 est
public static int lireInt () // lecture d’un int
la ligne premier.java:13 est
n=Clavier.lireInt();
aidez moi s'il vous plaît
Configuration: Windows Vista Firefox 3.5.7
8 réponses
-
Salut,
J'ai compilé les 2 sources:
-premier (ce serait mieux de la nommer Premier) sans package exercice; et import exercice.Clavier;
-puis Clavier
Et ca fonctionne très bien.
Cordialement,
Dan -
j'ai écris comme tu m'a dit
import exercice.Clavier; public class Premier { public static void main(String[]args) { int i,n = 0,som; som=0; for(i=0;i<4;i++) { System.out.println("donnez un entier"); n=Clavier.lireInt(); som+=n; } System.out.println("Somme:"+som); } }
maintenant j'ai erreur suivant:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at exercice.Premier.main(Premier.java:5)
la ligne 5 est:public static void main(String[]args)
-
-
j'enlevais import exercice.Clavier toujours fait même erreur
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at exercice.Premier.main(Premier.java:5)
//package exercice; //import exercice.Clavier; public class Premier { public static void main(String[]args) { int i,n = 0,som; som=0; for(i=0;i<4;i++) { System.out.println("donnez un entier"); n=Clavier.lireInt(); som+=n; } System.out.println("Somme:"+som); } }
merci de m'aider -
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question -
RE,
J'ai de nouveau copié/coller les 2 classes (Premier et clavier) que j'avais déjà supprimées.
J'ai compilé ces 2 classes(séparément), testé les 2 et tout est ok.
Vérifie bien que Clavier est bien sans la partie Premier comme ceci:
import java.io.* ; public class Clavier { public static String lireString () // lecture d’une chaine { String ligne_lue = null ; try { InputStreamReader lecteur = new InputStreamReader (System.in) ; BufferedReader entree = new BufferedReader (lecteur) ; ligne_lue = entree.readLine() ; } catch (IOException err) { System.exit(0) ; } return ligne_lue ; } //Annexe D //La classe Clavier public static float lireFloat () // lecture d’un float { float x=0 ; // valeur a lire try { String ligne_lue = lireString() ; x = Float.parseFloat(ligne_lue) ; } catch (NumberFormatException err) { System.out.println ("*** Erreur de donnee ***") ; System.exit(0) ; } return x ; } public static double lireDouble () // lecture d’un double { double x=0 ; // valeur a lire try { String ligne_lue = lireString() ; x = Double.parseDouble(ligne_lue) ; } catch (NumberFormatException err) { System.out.println ("*** Erreur de donnee ***") ; System.exit(0) ; } return x ; } public static int lireInt () // lecture d’un int { int n=0 ; // valeur a lire try { String ligne_lue = lireString() ; n = Integer.parseInt(ligne_lue) ; } catch (NumberFormatException err) { System.out.println ("*** Erreur de donnee ***") ; System.exit(0) ; } return n ; } // programme de test de la classe Clavier public static void main (String[] args) { System.out.println ("donnez un flottant") ; float x ; x = Clavier.lireFloat() ; System.out.println ("merci pour " + x) ; System.out.println ("donnez un entier") ; int n ; n = Clavier.lireInt() ; System.out.println ("merci pour " + n) ; } }
Dan -
1) Dans la déclaration de ta méthode main met un espace entre String[] et args.
2) La classe Clavier est obsolète depuis quelques temps, je te conseille plutôt d'utiliser un Scanner.
3) Voici un corriger de ton exercice qui fonctionne très bien sans la classe Clavier :
import java.util.Scanner; public class Premier { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int somme = 0; for(int i = 0; i < 4; i++) { System.out.print("Donnez un entier : "); int n = sc.nextInt(); somme += n; } System.out.println("Somme = " + somme); } } -
public static void main(String[]args)
Effectivement, il manque un epace entre String[] et args ! (ou bien String et args[], vive le java!) -
C'est vraiment nimporte quoi:
On peut écrire main(String[]args) ou main(String[] args) et pourquoi pas main(String[] args) et aussi main(String[] toto).
Une autre forme d'écriture: main(String args[]) cette fois exige l'espace entre String et args et là on peut aussi écrire:
main(String args[]) ou main(String toto[])
C'est la (les) syntaxe(s) pour déclarer un tableau: type[] nom; ou type nom[];
Dan