0

I've looked the documentation on the subject and some exemples but I kind seem to find why the program doesn't work has intended.

I need to make a program with 6 choice, I'm using a Clavier.class which is a school made and approved class for reading user entry.

So fare I only made the first option, but when testing the program, if the input is 1, it doesn't go for the 1st loop, the program only stops after out-printing the user entry.

Here is the code so far(It's in french):

public class Facturation {
    public static void main (String [] args) {

char Choix ;
int random;
final String MSG_PRESENTATION = "Programme de facturation a la minute pour"
+ "\n" + "la location de vehicules electriques. ";
final String MENU = "----" 
+ "\n" + "MENU" 
+ "\n" + "----"
+ "\n" + "1. Louer un vehicule"
+ "\n" + "2. Facturer la remise d'un vehicule"
+ "\n" + "3. Annuler une location"
+ "\n" + "4. Afficher le montant des recettes"
+ "\n"+ "5. Reinitialiser le montant des recettes"
+ "\n" + "6. Quitter le programme"
+ "\n" + "\n"+ "Entrez votre choix";
final String MSG1 = "LOCATION" ;
final String NOCAR1 = "Il n'y a plus de véhicules disponibles.";

System.out.println (MSG_PRESENTATION);
System.out.print (MENU + "\n") ;
Choix = Clavier.lireChar();

     while (Choix >=1 && Choix <=6) ;
     {
     if (Choix == 1) { 
        System.out.println(MSG1);
        double randomDouble = Math.random();
    randomDouble = randomDouble * 4 + 1;
    int randomInt = (int) randomDouble;
    System.out.println(randomInt);

    }

}
}
}

The first option generates a number from 1 to 4. I know the problem isn't with the number generator since I tried it in a separate class and it worked. The code compiles. I tried with switch/case and it didn't solve the problem. It will only show the menu at the beginning, and once the user types in "1", the program output 1 and stops.

Thanks.

MaelPJ
  • 115
  • 5
  • 3 problems 1) `while (Choix >='1' && Choix <='6') ;` same for `if (Choix == '1')` | 2) the semi-colon after the while `while (Choix >='1' && Choix <='6')` 3) `Choix` is not being changed/read in loop; will loop forever (or never) – user85421 Oct 05 '19 at 07:20

2 Answers2

0

The problem here is you are getting choice as a char and comparing it to expected int values. Simply convert choix char to it's numerical value before using in comparison.

int choixNumeric = Character.getNumericValue(Clavier.lireChar());

Converting characters to integers in Java

Martin'sRun
  • 522
  • 3
  • 11
  • Exactly what I thought, but in the homework instruction, they ask to not use INT Note that I canno't declare the variable representing the user choice has an int., as asked in the instructions for the project, to prevent the program from bugging if the entry include character, ex: 1dt... I need to use char. Here is the instruction(translated in english): "The program can't fail if the user enter a non-number entry. As such, treat menu entry as numerical number instead of int. value." – MaelPJ Oct 05 '19 at 06:48
  • The Character.getNumeric returns -1 if for non numeric entries, your code will not fail. Refer to https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getNumericValue(int) – Martin'sRun Oct 05 '19 at 07:00
  • why not just compare `char` to a `char` like in `Choix >= '1'`? `'1'` has the same integral value then the read character if it was an `1` – user85421 Oct 05 '19 at 07:26
0

I changed and for int, and now run into compiling error. I added number 2 and used switch:


   public class Facturation {
       public static void main (String [] args) {

   int Choixmenu ;
   int random;
   final String MSG_PRESENTATION = "Programme de facturation a la minute pour"
   + "\n" + "la location de vehicules electriques. ";
   final String MENU = "----" 
   + "\n" + "MENU" 
   + "\n" + "----"
   + "\n" + "1. Louer un vehicule"
   + "\n" + "2. Facturer la remise d'un vehicule"
   + "\n" + "3. Annuler une location"
   + "\n" + "4. Afficher le montant des recettes"
   + "\n"+ "5. Reinitialiser le montant des recettes"
   + "\n" + "6. Quitter le programme"
   + "\n" + "\n"+ "Entrez votre choix";
   final String MSG_ERREUR = "Ceci n'est pas une entree valide.";
   final String MSG1 = "LOCATION" ;
   final String NOCAR1 = "Il n'y a plus de véhicules disponibles.";
   final String MSG2 = " Entrer le numero du vehicule retourne" ;
   System.out.println (MSG_PRESENTATION);
   System.out.print (MENU + "\n") ;
   int Choixmenu = Clavier.lireInt();

        Switch (Choixmenu) {
        case 1:  
           System.out.println(MSG1);
           double randomDouble = Math.random();
       randomDouble = randomDouble * 4 + 1;
       int randomInt = (int) randomDouble;
       System.out.println(randomInt);
       break;
        case 2:
       System.out.println(MSG2);
       break;
       default:
       System.out.println (MSG_ERREUR);
       break;

       }

   }
   }


MaelPJ
  • 115
  • 5