Pb ave nextChar?

Résolu
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 - 8 juil. 2010 à 11:06
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 - 10 juil. 2010 à 10:25
Bonjour,
J'ai écrit ce prg
mais nextChar(); marche pas quel est la solution? aidez moi s'il vous plaît

package Textprg;
import java.util.Scanner;
public class Calculteur 
{
public static void main (String[] args)
{
double firstnb;
double secondnb;
char operator;
double value;
System.out.println("Entrez expressions suc as 2+2 or 34.2*7.81");
System.out.println("To end ,enter a 0");

while(true)
{
System.out.println("Entrez votre premier Nb:");
Scanner sc=new Scanner(System.in);
firstnb=sc.nextInt();
if(firstnb==0)
break;
System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.nextChar();
System.out.println("Entrez second Nb");
Scanner sc2=new Scanner(System.in);
secondnb=sc2.nextInt();

switch(operator)
{
case'+':
value=firstnb+secondnb;
break;
case'-':
value=firstnb-secondnb;
break;
case'*':
value=firstnb*secondnb;
break;
case'/':
value=firstnb/secondnb;
break;
default:
System.out.println("operator inconnue"+operator);
continue;
}
System.out.println("vlaue is "+value);

}
      System.out.println("Good Bye");
}
}

12 réponses

HFanny Messages postés 699 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 13 mai 2011 20
9 juil. 2010 à 18:38
Ce serait mieux d'écrire un if avec des parenthèses :
if (test) action;


De plus, pour tester l'égalité de deux variables, il faut utiliser et non pas (un seul = équivaut à l'assignation d'une valeur pour une variable).

Puis, pour tester l'égalité d'objets String, equalsIgnoreCase() est plus approprié :
if (operator.equalsIgnoreCase("+")) oper = 0;


Il y a aussi une autre erreur dans ce code :
value = firstNum + secondNum;


Tes variables sont nommées plus haut :
double firstnb;
double secondnb;


Le copier/coller c'est mal si on ne modifie pas les noms des variables avec celles utilisées ^^
(Donc n'oublie pas de vérifier que les variables sont bien nommées correctement)

Fanny
3
Utilisateur anonyme
9 juil. 2010 à 10:40
Salut,

switch(expression): expression doit être un entier, soit de type char, byte, short ou int.
Cet entier sera implicitement converti en int.

Dans ton cas. expression devra être une valeur numérique entière, PAR EXEMPLE 0 pour '+', 1 pour '-', 2 pour '*' et 3 pour '/'.

Exemple:
if operator '+' operator '0';
else
if operator '-' operator '1';
else
if operator '*' operator '2';
else
if operator '/' operator '3';
else
operator = '4';
switch(operator)

et remplacer case('+') par case 0:
case '-' par case 1:
etc...
pour ne pas avoir à modifier le reste de ton code, mais conserver la saisie de +, -, *, et / ce qui est plus "parlant" que 0, 1, 2 ou 3.

Cordialement,

...\ Dan /...
0
Utilisateur anonyme
9 juil. 2010 à 11:29
Re,

J'ai validé trop vite:

Il faut que tu remplaces
char operator; par String operator;
nextChar() n'existant pas

Et:
		System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.next();


Et ma réponse corrigée est la suivante (si tu veux toujours utiliser switch)

switch(expression): expression doit être un entier, soit de type char, byte, short ou int.
Cet entier sera implicitement converti en int.

Dans ton cas. expression doit être une valeur numérique entière, PAR EXEMPLE 0 pour "+", 1 pour "-", 2 pour "*" et 3 pour "/".

Exemple:

int oper = 4;
if operator "+" oper 0;
else
if operator "-" oper 1;
else
if operator "*" oper 2;
else
if operator "/" oper 3;
switch(oper)



et remplacer case('+') par case 0:
case '-' par case 1:
etc...
pour conserver la saisie de +, -, *, et / ce qui est plus "parlant" que 0, 1, 2 ou 3.


...\ Dan /...
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
9 juil. 2010 à 12:07
j'ai fait comme tu m'a dit
package Textprg;
import java.util.Scanner;
public class Calculteur 
{
public static void main (String[] args)
{
double firstnb;
double secondnb;
String operator;
double value;
System.out.println("Entrez expressions suc as 2+2 or 34.2*7.81");
System.out.println("To end ,enter a 0");

while(true)
{
System.out.println("Entrez votre premier Nb:");
Scanner sc=new Scanner(System.in);
firstnb=sc.nextInt();
if(firstnb==0)
break;

System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.next();


System.out.println("Entrez second Nb");
Scanner sc2=new Scanner(System.in);
secondnb=sc2.nextInt();

int oper = 4;
if operator "+" oper 0;
else
if operator "-" oper 1;
else
if operator "*" oper 2;
else
if operator "/" oper 3;
switch(oper)

        {
        case '0':
           value = firstNum + secondNum;
           break;
        case '1':
           value = firstNum - secondNum;
           break;
        case '2':
           value = firstNum * secondNum;
           break;
        case '3':
           value = firstNum / secondNum;
           break;
        default:
        	System.out.println("Unknown operator: " + operator);
           continue;  
    } 
    System.out.println("Value is " + value);
    System.out.println();              
} 
System.out.println("Good bye");
} 
}  

en compilant j'ai erreur suivantes
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "if", invalid Type
Syntax error, insert ";" to complete LocalVariableDeclarationStatement
Syntax error on tokens, delete these tokens
Syntax error, insert ";" to complete Statement
Syntax error on tokens, delete these tokens
Syntax error, insert ";" to complete Statement
Syntax error on tokens, delete these tokens
Syntax error, insert ";" to complete Statement

at Textprg.Calculteur.main(Calculteur.java:32)

en mettant ;
j'ai erreur suivantes:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "if", invalid Type
Syntax error on tokens, delete these tokens
Syntax error on tokens, delete these tokens
Syntax error on tokens, delete these tokens

at Textprg.Calculteur.main(Calculteur.java:32)

if et else sont soulignés
merci de m'aider
0

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

Posez votre question
Utilisateur anonyme
9 juil. 2010 à 13:21
Re,

Tu as laissé:

		switch(oper)

        {
        case '0':
           value = firstNum + secondNum;
           break;
        case '1':
           value = firstNum - secondNum;
           break;
        case '2':
           value = firstNum * secondNum;
           break;
        case '3':
           value = firstNum / secondNum;
           break;
        default:
        	System.out.println("Unknown operator: " + operator);
           continue;  
    } 
    System.out.println("Value is " + value);
    System.out.println();              
} 
System.out.println("Good bye");
} 
}  


Il faut:

		switch(oper)

        {
        case 0:
           value = firstNum + secondNum;
           break;
        case 1:
           value = firstNum - secondNum;
           break;
        case 2:
           value = firstNum * secondNum;
           break;
        case 3:
           value = firstNum / secondNum;
           break;
        default:
        	System.out.println("Unknown operator: " + operator);
           continue;  
    } 
    System.out.println("Value is " + value);
    System.out.println();              
} 
System.out.println("Good bye");
} 
}  



...\ Dan /...
0
Utilisateur anonyme
9 juil. 2010 à 13:52
remplace aussi:
System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.next();

par:
System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.nextLine();



...\ Dan /...
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
9 juil. 2010 à 13:55
package Textprg;
import java.util.Scanner;
public class Calculteur 
{
public static void main (String[] args)
{
double firstnb;
double secondnb;
String operator;
double value;
System.out.println("Entrez expressions suc as 2+2 or 34.2*7.81");
System.out.println("To end ,enter a 0");

while(true)
{
System.out.println("Entrez votre premier Nb:");
Scanner sc=new Scanner(System.in);
firstnb=sc.nextInt();
if(firstnb==0)
break;

System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.next();


System.out.println("Entrez second Nb");
Scanner sc2=new Scanner(System.in);
secondnb=sc2.nextInt();

int oper = 4;
if operator "+" ;oper 0;
else
if operator "-";oper 1;
else
if operator "*" ;oper 2;
else
if operator "/" ;oper 3;

switch(oper)
{
case 0:
   value = firstNum + secondNum;
   break;
case 1:
   value = firstNum - secondNum;
   break;
case 2:
   value = firstNum * secondNum;
   break;
case 3:
   value = firstNum / secondNum;
   break;
default:
System.out.println("Unknown operator: " + operator);
   continue;  
} 
System.out.println("Value is " + value);
System.out.println();              
} 
System.out.println("Good bye");
} 
}  


en compilant j'ai erreur suivantes:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "if", invalid Type
Syntax error on tokens, delete these tokens
Syntax error on tokens, delete these tokens
Syntax error on tokens, delete these tokens

at Textprg.Calculteur.main(Calculteur.java:32)
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
9 juil. 2010 à 13:58
même en changeant

System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.nextLine();

j'ai même erreur
0
Utilisateur anonyme
9 juil. 2010 à 14:09
Pourquoi as tu ajouté un ;
int oper = 4;
if operator "+" ;oper 0;
else
if operator "-";oper 1;
else
if operator "*" ;oper 2;
else
if operator "/" ;oper 3;


Il faut:
int oper = 4;
if operator "+" oper 0;
else
if operator "-" oper 1;
else
if operator "*"  oper 2;
else
if operator "/"  oper 3;


...\ Dan /...
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
9 juil. 2010 à 14:29
package Textprg;
import java.util.Scanner;
public class Calculteur 
{
public static void main (String[] args)
{
double firstnb;
double secondnb;
String operator;
double value;
System.out.println("Entrez expressions suc as 2+2 or 34.2*7.81");
System.out.println("To end ,enter a 0");

while(true)
{
System.out.println("Entrez votre premier Nb:");
Scanner sc=new Scanner(System.in);
firstnb=sc.nextInt();
if(firstnb==0)
break;

System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.nextLine();

System.out.println("Entrez second Nb");
Scanner sc2=new Scanner(System.in);
secondnb=sc2.nextInt();

int oper = 4;
if operator "+" oper 0;
else
if operator "-" oper 1;
else
if operator "*"  oper 2;
else
if operator "/"  oper 3;

switch(oper)

{
case 0:
   value = firstNum + secondNum;
   break;
case 1:
   value = firstNum - secondNum;
   break;
case 2:
   value = firstNum * secondNum;
   break;
case 3:
   value = firstNum / secondNum;
   break;
default:
System.out.println("Unknown operator: " + operator);
   continue;  
} 
System.out.println("Value is " + value);
System.out.println();              
} 
System.out.println("Good bye");
} 
}  

en compilant j'ai erreur suivant:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "if", invalid Type
Syntax error, insert ";" to complete LocalVariableDeclarationStatement
Syntax error on tokens, delete these tokens
Syntax error, insert ";" to complete Statement
Syntax error on tokens, delete these tokens
Syntax error, insert ";" to complete Statement
Syntax error on tokens, delete these tokens
Syntax error, insert ";" to complete Statement

at Textprg.Calculteur.main(Calculteur.java:31)
0
Utilisateur anonyme
9 juil. 2010 à 14:43
C'est ma faute désolé:

Il faut:
int oper = 4;
if (operator.equals("+")) oper = 0;
else
if (operator.equals("-")) oper = 1;
else
if (operator.equals("*")) oper = 2;
else
if (operator.equals("/")) oper = 3;


...\ Dan /...
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
10 juil. 2010 à 10:25
merci beaucoup
j'ai corrigé mon programme
ça marche, merci encore
0
Rejoignez-nous