Help with Java

Storky

Rawr
|K3| Moderator
Recruitment Team
I think it's just asking you to try out your program with several different input scenarios.

E.g. It asks you to try the name "Mike"
then it asks you to try "mike" note M isn't capital anymore
see if this works, and if not then why not?


P.s. on the redundant ( input == 12 ) thing, it's good programming practice to have it in there anyway to "protect" your program in case an error happens. Other people might argue that this reduces program efficiency, but what would you rather have.. software that runs fast or software with no errors
 
Last edited:

Kreubs

|K3|Minecraft Admin
|K3| Executive
Thanks guys. Worked great. It's like this now:
1. Write an if statement that uses the flag as the condition. Remember that the flag is a Boolean variable, therefore is true or false. It does not have to be compared to anything.

2. The body of the if statement should contain two statements:

a) A statement that prints a message indicating that the user is eligible for a $2.00 discount.

b) A statement that reduces the variable cost by 2.


note: the above are pretty easy with println, so I crossed them out.
You've pretty much done this already as well.
Code:
if (discount == true)
{
    cost = cost - minusTwo;
}

Everything within the parentheses of an if statement will produce a boolean value, a flag, in the end.
Just so you can see a working example, try this:
Code:
boolean test1 = false, test2 = false;
 
test1 = (5 == 5);
test2 = (4 == 5);
 
System.out.println(test1);
System.out.println(test2);
output:
true
false

Now that we've seen the way it boils down, why don't we just place a true/false value placeholder right in the parentheses?
Code:
if (discount)
{
    cost = cost - minusTwo;
    //print some happy pay less money message :3
}
Writing a boolean all by itself is enough. When the program is run, discount will be replaced with true, as set before, making it actually read true == true, which is redundant.
If we expect a value to be false, we use the logical negation operator '!'.
Code:
if (someBoolean)
{
    //do something if true
}
if (!someBoolean)
{
    //do something if false
}
 
Last edited:

Sodalover

Second Lieutenant
|K3| Member
I think it's just asking you to try out your program with several different input scenarios.
P.s. on the redundant ( input == 12 ) thing, it's good programming practice to have it in there anyway to "protect" your program in case an error happens. Other people might argue that this reduces program efficiency, but what would you rather have.. software that runs fast or software with no errors

That's what I thought too.


So far, the program is working wonderfully. Respond as is expected. You can only break it if it ask for int and you enter string (and vice versa). Everything has a default else statement that would give them a default value if the user didn't input anything or choose a value that's not presented. I think all that's left is maybe a bit of cleaning up and optimize the code, making it a bit more "elegant" (and maybe learn something new too) Almost done. You guys have been God sent!
 

Sodalover

Second Lieutenant
|K3| Member
It could be sometimes, especially when I'm not provide some sort of sandbox to mess around in, and even then, I might not remember everything I learned.
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
It can suck at times when you really hit a dead end, or spend 3 hours (or 3 days) debugging the wrong thing. And like any other job, it can involve blood, sweat, and tears. XD I guess it's just that feeling when you you look at the final product and watch it work that really makes up for it. :D
 

Sodalover

Second Lieutenant
|K3| Member
So Storky code works, but not quite as expected. I just checked recently trying to break my program and found it:

if (firstName.equals("Mike") || firstName.equals("diane") || firstName.equals("Diane")) {
cost = cost - minusTwo;
f
irstName = keyboard.nextLine();
}

This is the code that's supposed to give you a discount if your name is Mike or Diane. However, it just gives it out no matter the input. So I'm thinking I should just go back to the last question I posted and do it.


Edit: So now it looks something like this:

if (firstName.equals("mike") || firstName.equals("diane"));
{
discount = true;
}

...much further down...

if (discount)
{
cost = cost - 2;
System.out.println("HAPPY BIRTHDAY");
}

So I'm guessing the underlined discount variable is the flag I've asked about earlier. Problem with this one is it still gives people the discount no matter what name they enter. I'm at a loss for how it does that, because Storky's firstname.equal() shouldn't permit the discount unless the string entered is Mike or Diane. Did I miss something important?
 
Last edited:

stevo

eh
Former Krew Member
This is your error you had me going this morning lol

if (firstName.equals("mike") || firstName.equals("diane")); <------------Notice the semi column
{
discount = true;
}

Notice the semi column, so you are ending that if statement when you put a semi column so the stuff in the brackets are always being called. Solution remove the semi column.

Should be
if (firstName.equals("mike") || firstName.equals("diane"))
{
discount = true;
}
 

Storky

Rawr
|K3| Moderator
Recruitment Team
Code:
if (firstName.equals("Mike") || firstName.equals("diane")) {
cost = cost - minusTwo;
firstName = keyboard.nextLine();
}
is correct. There is obviously something else in your code making it do the discount anyway.
For example, make sure that when you initialise "discount" as boolean, you initialise it as false!

Another scenario where it will always give discount: When you first order a pizza and the person ordering gets a discount, "discount" is set to True right? Before the program finishes/loops back, does discount get changed back to false?
 

stevo

eh
Former Krew Member
Code:
if (firstName.equals("Mike") || firstName.equals("diane")) {
cost = cost - minusTwo;
firstName = keyboard.nextLine();
}
is correct. There is obviously something else in your code making it do the discount anyway.
For example, make sure that when you initialise "discount" as boolean, you initialise it as false!

Another scenario where it will always give discount: When you first order a pizza and the person ordering gets a discount, "discount" is set to True right? Before the program finishes/loops back, does discount get changed back to false?
Thanks for ignoring my post lol unwanted semi column in the if statement.
 

Storky

Rawr
|K3| Moderator
Recruitment Team
Thanks for ignoring my post lol unwanted semi column in the if statement.
Yeah you are right but i was just stating that the first one i gave him in the first place.. ^^ this IS correct.

It's semicolon by the way :p[DOUBLEPOST=1390303227][/DOUBLEPOST]heres a solution:
Code:
if (firstName.equals("Mike") || firstName.equals("diane")) {
      discount = true;
      firstName = keyboard.nextLine();
}
else {
      discount = false;
      firstName = keyboard.nextLine();
}
 
if (discount) {
blah blah blah
This will ONLY make the discount true IF their name is "Mike" or "diane" and if not, it'll make sure discount is NOT true.
 
Last edited:

Sodalover

Second Lieutenant
|K3| Member
Goddamnit...one of those things I should have known but instead didn't. As for the name enter, isn't there some way to detect their name in a non-case sensitive way? Like regardless of Mike, mike, MIkE or miKE, the discount would still apply.
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
You could just convert the user input to lower case and compare it to strictly lower case strings.
If the user enters "MiKe", "MIKE", or any other combination of upper and lower case letters, the string still gets converted to "mike".
Code:
firstName = firstName.toLowerCase();

if (firstName.equals("mike") || firstName.equals("diane")) {
    discount = true;
    firstName = keyboard.nextLine();
} //rest of code
 
Last edited:

Sodalover

Second Lieutenant
|K3| Member
Sheeeeit...That's crazy useful. Would I be right to think that programs like mine (i.e. asks for input from user for output) utilizes the firstName.toLowerCase(); a lot, just in case people enter something like miKE?
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
Sheeeeit...That's crazy useful. Would I be right to think that programs like mine (i.e. asks for input from user for output) utilizes the firstName.toLowerCase(); a lot, just in case people enter something like miKE?
Well, in this case, it's just another form of error checking. In the future, you may encounter the need for case sensitivity, but for something as small as this, it's no big deal.
 
Top Bottom