user2808951
user2808951

Reputation: 25

Program does not run properly

I'm trying to write a program for my programming class, and the program is supposed to run like an online shopping store, and so at one point the program is supposed to ask the user if they have a promotional code, and if they put in 'y' for yes, it asks for a six-character promotional code.

Criteria for the promotional code is:

For some reason, my program doesn't seem to be running properly. It compiles fine, but when it reaches the if statement that checks the promotional code, it always prints out the line that tells you your promotional code is wrong. I even changed it so that the criteria was just that one of the characters in the code needed to be 7, and then entered '777777' as the promotional code, but it still printed out as if the promotional code were wrong.

I can't figure out why it isn't running properly. Here's the original code:

Scanner pro=new Scanner(System.in);
out.println("\nDo you have a promotional code? Enter Y for yes, N for no: ");
String pc=pro.nextLine();
char promo=pc.charAt(0);

int o=7;

switch(promo)
{
    case 'y':
    case 'Y':
         Scanner mo=new Scanner(System.in);
         System.out.println("Please input the six character promotional code here: ");
         String prm=mo.nextLine();
         char code=prm.charAt(2);

         if(code==7&&prm.substring(4).equals("bG"))
         {
              ordp=ordp*.9;
              System.out.println("\nYou have recieved ten percent off your order! Your order subtotal is now "+ordp+".");
         }
         else
         {
              System.out.println("\nYour promotional code is not valid.");
         }
    case 'n':
    case 'N':
         break;
}

I apologize for the code block not being formatted correctly. I can't seem to figure out how to format it correctly...sorry! I really did try, and can't get it right, apparently, so I did the best I could. In any case, that's the relevant code. Help is much appreciated.

Upvotes: 0

Views: 83

Answers (3)

Henry
Henry

Reputation: 332

char code=prm.charAt(2);

if(code==7&&prm.substring(4).equals("bG"))
{
    ordp=ordp*.9;
    System.out.println("\nYou have recieved ten percent off your order! Your order subtotal is now "+ordp+".");
}
else
{
    System.out.println("\nYour promotional code is not valid.");
}

Here you are comparing a char to int, you want to compare it to '7'. So you could use this instead.

if(code=='7'&&prm.substring(4).equals("bG"))

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93872

The data type of code is char. But the char value of '7' is not 7 but 55.

So change the if like this :

if(code=='7'&&prm.substring(4).equals("bG"))

Few notes to add :

  1. If you want to keep using your switch, don't forget the break statements.
  2. But you could improve it by using a simple if and Character.toLowerCase
  3. Why create a new mo scanner ? Use only the pro scanner object

   char promo= Character.toLowerCase(pc.charAt(0));
   if(promo == 'y'){
      /**
       * For yes
      */
   } else {
       /*
        * For no or different input
       */
   }

Upvotes: 2

rgettman
rgettman

Reputation: 178293

When comparing char values, it's confusing to compare a char to an int value as you've done here:

if(code==7&&prm.substring(4).equals("bG"))  // specifically, code==7

You need to find the character '7', not the ASCII character whose int value is 7, "bel". Try

if(code == '7' && prm.substring(4).equals("bG"))

You'll want to add an additional restriction that prm.length() == 6, to enforce that the promotion code must be 6 characters long.

Upvotes: 2

Related Questions