Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So I am working with this code but the end result should look something like this.
EX.
People: 4
Pizza: 1
Cost: $14.95
My code looks like this -
import java.util.*;
public class Pizza {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int people = keyboard.nextInt();
int pizza = people * 2 / 12 + 1;
double cost = pizza * 14.95;
System.out.println("People: " + people + "Pizzas: " + pizza + "Cost: $" + cost + );
}
}
The int people = keyboard.nextInt(); line allows me to input a integer value of people and the program then will take people * 2 and then divide by 12 (for the number of slices in a pizza) and + 1 to give me the pizzas I need. The cost will then be the pizza number * 14.95. I want the amount of people, pizzas, and cost to be displayed in the System.out.println method on the bottom. I do not know what I did wrong? Shouldn't + people + display the people in that line? Same with + pizza + and + cost + ?
The + operator joins strings (and other variables converted to strings). Just like in maths, you write it between two elements, so the final + was superfluous (and erroneous).
System.out.println("People: " + people + " Pizzas: " + pizza + " Cost: $" + cost);
When learning Java, you might want to use an IDE, which would point out this mistake for you, or just read the errors the compiler gives you.
You shouldn't have that last + at the end of your print statement, since there's nothing after it. When it's removed your code seems to run fine, albeit with a slight whitespace issue which you can fix by adding spaces to the print statement.
12
People: 12Pizzas: 3Cost: $44.849999999999994
System.out.println("People: " + people + "Pizzas: " + pizza + "Cost: $" + cost + );
You have put extra + at the end. Remove that.
String concatenation like str1 + str2 is OK, but pay attention to correctly print double value: you need only two fractional digits:
try (Scanner keyboard = new Scanner(System.in)) {
int people = keyboard.nextInt();
int pizza = people * 2 / 12 + 1;
double cost = pizza * 14.95;
System.out.printf(Locale.US, "People: %d, Pizzas: %d, Cost: $%.2f\n", people, pizza, cost);
}
Output:
10
People: 10, Pizzas: 2, Cost: $29.90
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm writing a program called SalesTax.java. The program will create an output that tells the sales tax on a $50 purchase, when the sales tax changes depending on the day in the month of January. I like to save my program and run it often, so I can see that I got that section right. When I run the stuff I have done, I get a super weird error and I have no idea what I'm doing wrong.
I've tried to create the syntax the compile message error told me, where I get more errors: SalesTax.java:6: error: variable N is already defined in method main(String[]) and
SalesTax.java:6: error: lambda expression not expected here.
public class SalesTax {
public static void main(String[] args) {
int N = 1;
int Item = 50;
int st = N;
System.out.println("On January " + (N)", The Sales Tax will be " + (st / 100) * Item);
}
}
I expect the output to be: On January 1, The Sales Tax will be 50.5. However, instead I'm getting this:
SalesTax.java:6: error: cannot find symbol
System.out.println("On January " + (N)", The Sales Tax will be " + (st / 100) * Item);
^
symbol: class N
location: class SalesTax
1 error
Any ideas on how to fix it?
You need a + after (N) to concatenate it with the next string:
System.out.println("On January " + (N) + ", The Sales Tax will be " + (st / 100) * Item);
You forgot a plus after (N). Try:
System.out.println("On January " + N + ", The Sales Tax will be " + (st / 100) * Item);
also note that in java, 1 / 100 (st is 1, so, that's what (st / 100) ends up being) is straight up 0, and not 0.01; integer math just rounds. If you want floating point stuff, you need double and not int. So, try: double st = N; instead.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am creating a simple program to calculate the cost of running a car. The program works just fine but I wanted to see if I could get the final answer to 2 decimal places. I tried using the '%8.2f' thing but it said that no method could be found for println(string, double)
This is my code:
/* Program to calculate the running cost of car */
import java.util.Scanner;
public class RunningCosts {
public static void main(String[] args) {
final int TOTAL_DISTANCE = 100000;
Scanner in = new Scanner(System.in);
System.out.print("Enter the car cost: ");
double carCost = in.nextDouble();
System.out.print("Enter the service cost: ");
double serviceCost = in.nextDouble();
System.out.print("Enter the service interval: ");
double serviceInterval = in.nextDouble();
System.out.print("Enter km per litre: ");
double kmPerLitre = in.nextDouble();
System.out.print("Enter the fuel cost per litre: ");
double fuelCostPerLitre = in.nextDouble();
double serviceTotal = (TOTAL_DISTANCE/serviceInterval) * serviceCost;
System.out.println( "Service Total: " + serviceTotal); //Here
double fuelCost = (TOTAL_DISTANCE/kmPerLitre) * fuelCostPerLitre;
System.out.println( "Fuel Cost: " + fuelCost); //Here
double totalCost = carCost + fuelCost + serviceTotal;
System.out.println( "Estimated Cost: " + totalCost); //And here
}
}
The commented lines are what I would like to be formatted to 2 decimal places
Either use printf as #shmosel suggested, or use String.format:
System.out.println( "Service Total: " + String.format("%.2f", serviceTotal));
See this page for more usage examples.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have an assignment that requires me to make the SalesByQtr java program i've gotten most of the program done but I have a couple of problems
Firstly: My total is being displayed as a double which I know I need printf to add a decimal place. The same thing is happening for Average.
Second: My division with the highest sales is saying Division 1 has the highest sales in qtr 1 and it repeats this in each qtr also Division 1 does not have the highest sales
Finally: My assignment requires that in qtrs 2-4 to display the change in sales and I can't figure out how to do that.
Here is the example output my professor is looking for
Example Output (bullet points represent spaces)
Q1:
D1:·1000.00↵
D2:·1700.00↵
D3:·300.00↵
D4:·2000.00↵
D5:·1400.00↵
D6:·500.00↵
Total:·6900.00↵
Average·sales:·1150.00↵
Division·with·highest·sales:4↵
Q2:
D1:·2500.00,·Change:·1500.00↵
D2:·500.00,·Change:·-1200.00↵
D3:·700.00,·Change:·400.00↵
D4:·2500.00,·Change:·500.00↵
D5:·1450.00,·Change:·50.00↵
D6:·1050.00,·Change:·550.00↵
Total:·8700.00,·Change:·1800.00↵
Average·sales:·1450.00↵
Division·with·highest·sales:1↵
Q3:
D1:·3000.00,·Change:·500.00↵
D2:·1000.00,·Change:·500.00↵
D3:·120.00,·Change:·-580.00↵
D4:·2700.00,·Change:·200.00↵
D5:·980.00,·Change:·-470.00↵
D6:·750.00,·Change:·-300.00↵
Total:·8550.00,·Change:·-150.00↵
Average·sales:·1425.00↵
Division·with·highest·sales:1↵
Q4:
D1:·2700.00,·Change:·-300.00↵
D2:·1300.00,·Change:·300.00↵
D3:·155.00,·Change:·35.00↵
D4:·3500.00,·Change:·800.00↵
D5:·1450.00,·Change:·470.00↵
D6:·1023.00,·Change:·273.00↵
Total:·10128.00,·Change:·1578.00
Average·sales:·1688.00
Division·with·highest·sales:4
Here's my code:
My Code
public static void main(String[] args) {
int divs = 6;
int qtrs = 4;
double errorCheck;
double[][] sales = new double[divs][qtrs];
double[] qtrsales = new double[qtrs];
int highestDiv = 0;
int[] highestDivi = new int[qtrs];
Scanner keyboard = new Scanner(System.in);
for (int div = 0; div < divs; div++) {
for (int qtr = 0; qtr < qtrs; qtr++) {
System.out.printf("Enter sales figures for division %d, quarter %d:", (div + 1), (qtr + 1));
errorCheck = keyboard.nextDouble();
while (errorCheck < 0) {
System.out.printf("Enter sales figures for division %d, quarter %d:", (div + 1), (qtr + 1));
errorCheck = keyboard.nextDouble();
}
sales[div][qtr] = errorCheck;
}
}
for (int qtr = 0; qtr < 4; qtr++) {
System.out.printf("Q%d:\n", (qtr + 1));
for (int div = 0; div < divs; div++) {
qtrsales[qtr] += sales[div][qtr];
System.out.printf("\tD%d: %.2f\n", (div + 1), sales[div][qtr]);
}
for (int qtrS = 0; qtrS < 1; qtrS++) {
System.out.printf("Total:" + qtrsales[qtr] + ", Change:");
System.out.printf("Average sales:" + (qtrsales[qtr] / divs));
}
for (int div = 0; div < 1; div++) {
highestDiv = 0;
if (sales[highestDiv][qtr] < sales[(div + 1)][qtr]) {
highestDiv = (div + 1);
}
highestDivi[qtr] = highestDiv;
System.out.println("Division with the highest sales:" + highestDivi[qtr]);
}
}
}
}
So here is the output that I think you want:
Q1:
D1: 1234.45
D2: 1232.54
D3: 23987.54
D4: 1233.56
D5: 234.56
D6: 1234.45
Total: 29157.10
Average sales: 4859.52
Division with the highest sales: 3
Q2:
D1: 234.56
D2: 1234.45
D3: 1232.54
D4: 23987.54
D5: 1233.56
D6: 234.56
Total: 28157.21 (change: 28157.21)
Average sales: 4692.87
Division with the highest sales: 4
Q3:
D1: 1233.56
D2: 234.56
D3: 1234.45
D4: 1232.54
D5: 23987.54
D6: 1233.56
Total: 29156.21 (change: 29156.21)
Average sales: 4859.37
Division with the highest sales: 5
Q4:
D1: 23987.54
D2: 1233.56
D3: 234.56
D4: 1234.45
D5: 1232.54
D6: 23987.54
Total: 51910.19 (change: 51910.19)
Average sales: 8651.70
Division with the highest sales: 1
IMPORTANT: If that's not what you want, then I've managed to fail at following directions and you probably shouldn't listen to anything else I have to say.
This output was had by making a number of small changes. Of course, I'm not going to hand them over to you (that would be doing your homework for you, and for that I would need to charge you an exorbitant sum of money) so instead I'll just point you in the right direction.
To start with, there is this bit of code:
for (int qtrS = 0; qtrS < 1; qtrS++)
Study that line of code carefully and discover what it is actually doing. I'll give you a hint: it isn't wrong necessarily, but there is something you could do to improve it.
You have a similar loop further down:
for (int div = 0; div < 1; div++)
Study this one more carefully - it may appear to have the same problem as the previous, but it does not. Once you solve this riddle, your remaining issues will present themselves more clearly. (Note: if your 'fix' causes your program to crash - good. Keep going, you're on the right track.)
In addition, I'd like to offer the following advice for that final loop:
Your data uses a zero-based index. Looking at your code, you already know this. Your output in some cases is one-based (you print Q1, not Q0). Again, looking at your code, you already know this. My advice is to be consistent about how you deal with this indexing discrepancy. Specifically, always index your data using zero-based indexes and only + 1 those indices when printing the output for the user.
Now... about that Change calculation. If you don't know how to do something, try explaining the problem to yourself in very clear and precise terms (like writing a program, but without the overhead of language syntax, array indexing and all that other complicated mumbo-jumbo.) If you did, it might sound something like this:
"I need to calculate the change for quarters 2-4. This means that I need to print a change for each of those quarters (Q2, Q3, Q4). More specifically, I'll need three "change values". How would a change value be calculated? Starting at one quarter and figuring out how much changed to the next? That's just a subtraction. So all I really need is find a way to get those two values (for each quarter) so I can perform that subtraction. Oh, and I have to make sure to only do it for those quarters (2-4)."
Remember, the trick is to bob your head while talking to yourself in the car so people think you're singing along.
Good luck.
This question was asked before (Formatting Decimal Number) without an answer as to how one can do such WITHOUT showing the decimal. I have been searching hours for an answer to no avail. Thanks in advance!
Example:
System.out.println("It would take " + oneMileMins + " minutes and " + oneMileDfSecs.format(oneMileSecs) + " seconds for the person to run one mile.");
Which outputs:
It would take x minutes and .yy seconds for the person to run one mile.
I would like the .yy to just be yy
Just change your output to oneMileDfSecs.format(oneMileSecs).replace(".", "")
EDIT: Rodney said that "He did not ask for the proper equation he only asked how to remove the decimal.", so I strike out the following , to respect him.
ADDITIONAL NOTE:
just like #Alan said , oneMileSecs should be equal to (int)((oneMile % 1)*60), in this case, the way you get rid of the decimal sign is little bit different :
1). If you declare :
double oneMileSecs = (int)((oneMile % 1)*60)
then change your output to :
String.valueOf(oneMileSecs).substring(0,String.valueOf(oneMileSecs).indexOf("."))
2). If you declare :
int oneMileSecs = (int)((oneMile % 1)*60)
then just output oneMileSecs directly as it's an int, it won't produce decimal sign
Not sure this is the answer you're looking for, as it doesn't answer the question in the title (Fev's answer does that), but I think this should give a correct result for the specific example:
private static void calcOneMile(double mph)
{
double oneMile = 60 / mph;
int oneMileMins = (int)oneMile;
double oneMileFraction = oneMile % 1;
int oneMileSecs = (int)(oneMileFraction * 60);
System.out.println("It would take " + oneMileMins + " minutes and " + oneMileSecs + " seconds for the person to run one mile.");
}
Or simplified to:
private static void calcOneMile(double mph)
{
int secondsToRunMile = (int)(1.0 / (mph / 3600));
System.out.println("It would take " + (secondsToRunMile / 60) + " minutes and " + (secondsToRunMile % 60) + " seconds for the person to run one mile.");
}
total in this case is 500. Trying to make a calculator, but not everything's adding up. It seems to skip the multiplication and just display total*amount. Is there something I'm doing wrong? EDIT: Discount: in the example, .92. I get 455000 if amount is 1000.
if (wShipping==true){
if (GroundShipping.isSelected()){
if (amount<=99) {
shipping=1.05;
output.setText(output.getText() + amount + "\t" + total*1.05*amount*discount + "\n");
}
else{
output.setText(output.getText() + amount + "\t" + total*amount*discount + "\n");
}
}
if (AirShipping.isSelected()){
shipping=1.1;
output.setText(output.getText() + amount + "\t" + total*amount*1.1*discount + "\n");
}
if (FedexShipping.isSelected()){
shipping=1.25;
output.setText(output.getText() + amount + "\t" + (total*amount*discount)*(1.25) + "\n");
}
}
You should consider the following things--
1) Why is the variable shipping needed if you are directly using the value in the set statement
2) Use else if statement since all the options are exclusive
3) You might want to check the initial values for variables and the formula for calculating the price. Taking the initial values as given, the lowest possible price is-
Price = 1000*500*0.92 = 460000 (total x amount x discount)
Hence there must be something amiss with your initial values
Maybe, just maybe is the first rule of currency calculations:
why not use double or float to represent currency