Help with Java

Sodalover

Second Lieutenant
|K3| Member
Is there anyone here who is proficient in Java? I have couple of problems that I need help with and if possible, explanation of how to solve these problems. I think they are fairly simple if you're familiar with the language, but I can't for the life of me figure them out.
 

WaLLy

Lieutenant General
|K3| Executive
No clue. However, post some of your problems that way people that may know the answer can help you without asking you about them, like I'm doing right now. :K
 

Storky

Rawr
|K3| Moderator
Recruitment Team
I just happen to be doing assignments/exams on it right now, feel free to post :)
 

Sodalover

Second Lieutenant
|K3| Member
I'm creating a program that take orders for pizza. The program ask the following input questions:
  • their name
  • size of pizza in inches
  • their choice of crust
  • their choice of toppings
First problem: I'm to use an "if" statement that compares the user's String input when they enter their name, and if it happens to be "Mike" or "Diane", they will get a 2$ discount. Not in italic and bold are my own codes:
Scanner keyboard = new Scanner (System.in);
String firstName;
boolean discount = false;
int cost = 12.99;
int minusTwo = 2;


System.out.println("Welcome to Mike and Diane's Pizza");
System.out.print("Enter your first name: ");
firstName = keyboard.nextLine();


if (firstName == "Mike" + "diane" + "Diane")
{
discount = true;
firstName = keyboard.nextLine();
}
if (discount == true)
{
cost = cost - minusTwo;
}

My own code doesn't create problems but it also doesn't work. I think it's not working because I didn't implement it correctly, rather than faulty thought process, but then again I am rather clueless.
 

Storky

Rawr
|K3| Moderator
Recruitment Team
First things first, you never use "==" for string comparisons.. "==" should only be used for integer comparison.
Try
Code:
 if (firstName.equals("Mike") || firstName.equals("diane") || firstName.equals("Diane"))

Also, you don't need the discount variable anyway (but this isn't why your code ISNT working)
Your whole if statement could be just:
Code:
if (firstName.equals("Mike") || firstName.equals("diane") || firstName.equals("Diane")) {
            cost = cost - minusTwo;
            firstName = keyboard.nextLine();
}
 
Last edited:

Kreubs

|K3|Minecraft Admin
|K3| Executive
To add to what Storky said, "==", when not used between primitive types (int, char, double, float, byte, etc.) but with an object, will compare the references. An object, being an instance of a class, will be given memory space with an address upon construction.

String is a class. Therefore, if you make two different strings with their own data, they will have different memory references causing == to return false.

Basically, using == between String objects, or any other object, is like comparing two houses on a road, but determining their equality solely by their street addresses. They could look the same on the outside (same class), but their furnishings may be different (different stored values), but == will always fail because they are at different locations.

Please let me know if something didn't/doesn't make sense.

You can set two objects' references equal to each other, but that is a different story for a different scenario. :)
 
Last edited:

Storky

Rawr
|K3| Moderator
Recruitment Team
I couldn't remember the whole "==" reason but I remembered from class that you never use it for comparing strings as it is not a primitive type lol.
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
Oh, Equals method is capitalized

Never mind, was thinking in C# XD
 
Last edited:

Sodalover

Second Lieutenant
|K3| Member
Thanks guys, work wonderful. Next question: the user is prompted to enter a number for their desired size of pizza, e.g. if you enter 12, your pizza will be 12 inches and so forth. Right now it look like this:

int inches;
double cost = 12.99;
double biggerSize = 2;

System.out.println("Pizza Size (inches) Cost");
System.out.println(" 10 $10.99");
System.out.println(" 12 $12.99");
System.out.println(" 14 $14.99");
System.out.println(" 16 $16.99");
System.out.println("What size pizza would you like?");
System.out.print("10, 12, 14, or 16 (enter the number only): ");
inches = keyboard.nextInt();


Right now, the price is 12.99 no matter which number you enter. I'll just copy and paste the question since I don't know how to word it:

1. Write an if-else-if statement that lets the computer choose which statements to execute by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.

2. The default else of the above if-else-if statement should print a statement that the user input was not one of the choices, so a 12 inch pizza will be made. It should also set the size to 12 and the cost to 12.99.



First, I want to use the scanner class to detect the user's input, but I have no idea how to do that, so if you guys can show this noob how, that would be greatly appreciated. After that, I want it to look like this:

note: input is what the user enters when asked what pizza size they want

if ( input = 10)
{
cost = cost - biggerSize;
}
if ( input = 14)
{
cost = cost + biggerSize * 2;
}

So for every size larger than 12 inches, they are charge 2 extra dollars. Is this viable?
 

Storky

Rawr
|K3| Moderator
Recruitment Team
First things first, don't forget your question is asking you to do an If-else-if statement.
It should be:
Code:
if (input == 10) {
code omitted
}
else if (input == 12) {
code omitted
}
else {                //default else
code omitted
}

I'll let @Kreubs answer the bit about the scanner.
I don't wanna give the answer away. This assignment is for you to learn for yourself.
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
Thanks guys, work wonderful. Next question: the user is prompted to enter a number for their desired size of pizza, e.g. if you enter 12, your pizza will be 12 inches and so forth. Right now it look like this:
Code:
int inches;
double cost = 12.99;
double biggerSize = 2;

System.out.println("Pizza Size (inches)  Cost");
System.out.println("  10  $10.99");
System.out.println("  12  $12.99");
System.out.println("  14  $14.99");
System.out.println("  16  $16.99");
System.out.println("What size pizza would you like?");
System.out.print("10, 12, 14, or 16 (enter the number only): ");
inches = keyboard.nextInt();


Right now, the price is 12.99 no matter which number you enter. I'll just copy and paste the question since I don't know how to word it:

1. Write an if-else-if statement that lets the computer choose which statements to execute by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.

2. The default else of the above if-else-if statement should print a statement that the user input was not one of the choices, so a 12 inch pizza will be made. It should also set the size to 12 and the cost to 12.99.



First, I want to use the scanner class to detect the user's input, but I have no idea how to do that, so if you guys can show this noob how, that would be greatly appreciated. After that, I want it to look like this:

note: input is what the user enters when asked what pizza size they want

if ( input = 10)
{
cost = cost - biggerSize;
}
if ( input = 14)
{
cost = cost + biggerSize * 2;
}

So for every size larger than 12 inches, they are charge 2 extra dollars. Is this viable?
Pro tip, Stackoverflow is your best friend :D
Storky posted while I was typing XD

First of all, is keyboard a Scanner? I see it using the nextInt() method which is a part of Scanner.

1. Write an if-else-if statement that lets the computer choose which statements to execute by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.
Ok, let's start off with a bit of background information. A standard if-else statement has a condition, or group of conditions, that need to be met. If the condition(s) is/are not met, control will be sent to the else statement no matter what, provided it exists.

For an if-else if, you can chain multiple if's together by pairing successive else's with if's. These will be run if and only if the previous statement fails. Of course, the first if will always be checked.

Take this code for example:
Code:
int x = 5;

if(x == 3) {
    System.out.println("x equals 3");
}
if(x == 4) {
    System.out.println("x equals 4");
}
if(x == 5) {
    System.out.println("x equals 5");
}
There are three separate if statements. Therefore, each statement will be checked for validity in the order they appear. In this case, three will be checked, but only one will be true.
A similar situation with if-else if:
Code:
int x = 5;

if(x == 4) {
    System.out.println("x equals 4");
}
else if(x == 5) {
    System.out.println("x equals 5");
}
else if(x == 6) {
    System.out.println("x equals 6");
}
When we add an if to the else statement, we can optionally go to the alternative statement, we aren't forced to go as with a simple else. Here, only two statements will be evaluated with the second statement being true, and the third being skipped altogether. When they are all chained together like this, the chain is broken once one of the statements returns true.

Scanner is simply a class that takes the System input and makes it readable by your program.

Now, we could do something like this for your problem:
Code:
input = keyboard.nextInt(); //will only take an integer, be careful, it may throw an exception for non-integers

if ( input == 10)
{
    //do stuff here
}
else if ( input == 12)
{
    //do stuff here
}
else if ( input == 14)
{
    //do stuff here
}
else if ( input == 16)
{
    //do stuff here
}
else
{
     //do problem 2 stuff here
}
Notice the last else. As I said before, when an else has no condition, it will always be evaluated if it's previous if condition fails. This is why it's called a "default else".

Recap:
1. We use if-else if when we want to stop when we only have one possible match for our input/data.
2. The default else is used when we want a conditional statement to be run no matter what.
 
Last edited:

Sodalover

Second Lieutenant
|K3| Member
So if I understand correctly, the prompt asking for the user's name was set to detect string, while this one is set to detect integer.
 

Kreubs

|K3|Minecraft Admin
|K3| Executive
:yes:
edit:
I just saw in your first post that keyboard is already a scanner.
nextLine() returns a String whereas
nextInt() returns an int
Be careful though when typing. If you enter letters when it asks for numbers, it will throw an exception.
 

Storky

Rawr
|K3| Moderator
Recruitment Team
stack overflow has been my bible for the last 3 years

kreubs if half the posts on stack overflow were as easily readable as yours i would be a programming god by now
 

Sodalover

Second Lieutenant
|K3| Member
Thanks guys. Worked great. It's like this now:

if ( inches == 10)
{
cost = cost - 2;
}
else if ( inches == 12)
{
cost = cost;
}
else if ( inches == 14)
{
cost = cost + 2;
}
else if ( inches == 16)
{
cost = cost + 4;
}
else
{
cost = cost;
System.out.println("You didn't enter what was required. 12 inches coming up.");
}
keyboard.nextLine();

I'm fairly sure the else if ( inches == 12) is redundant, but I'll keep it anyway because I'm paranoid. I didn't much of the next question, so if you guys can explain a bit, that'd be great.

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.


3. Compile, debug, and run. Test your program using the owners’ names (both capitalized and not) as well as a different name. The discount should be correctly at this time.


From what I understand, this question relates a bit to my first one, concerning the discount if the user's name is Mike or Diane. If I followed the assignment to the letter, the code responsible for the discount wouldn't be implement until now, but I've already got it working, so maybe I've already finished and can skip this question?
 
Top Bottom