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 21 days ago.
Improve this question
I'm taking Elementary Programming and I'm trying to get this code to work, and I can only use the first 3 chapters worth of the Java Foundations book, and the book is very complicated and unclear on this part. I can't even tell you if I'm approaching this the right way, but I've been working on it for over a week now and just can't get it.
This is supposed to let the user input the houses cost, tax rate over 5 years, and fuel rate over 5 years for 3 houses, then print them in a side by side table across 4 lines.
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class houseCost {
public static void main(String[] args)
{
double costHouse1, costHouse2, costHouse3, fuelHouse1, fuelHouse2, fuelHouse3, finalHouse1, finalHouse2, finalHouse3, finalFuel1, finalFuel2, finalFuel3, finalTax1, finalTax2, finalTax3, taxHouse1, taxHouse2, taxHouse3;
{
Scanner scan = new Scanner(System.in);
NumberFormat fmt1, fmt2, fmt3 = NumberFormat.getCurrencyInstance();
System.out.print("Enter the value of the first house:");
costHouse1 = scan.nextDouble();
System.out.print("Enter the fuel cost for the first house:");
fuelHouse1 = scan.nextDouble();
System.out.print("Enter the tax rate of the first house:");
taxHouse1 = scan.nextDouble();
System.out.print("Enter the value of the second house:");
costHouse2 = scan.nextDouble();
System.out.print("Enter the fuel cost of the second house:");
fuelHouse2 = scan.nextDouble();
System.out.print("Enter the tax rate of the second house:");
taxHouse2 = scan.nextDouble();
System.out.print("Enter the value of the third house:");
costHouse3 = scan.nextDouble();
System.out.print("Enter the fuel cost of the third house:");
fuelHouse3 = scan.nextDouble();
System.out.print("Enter the tax rate of the third house:");
taxHouse3 = scan.nextDouble();
finalTax1 = ((costHouse1 * taxHouse1) * 5);
finalTax2 = ((costHouse2 * taxHouse2) * 5);
finalTax3 = ((costHouse3 * taxHouse3) * 5);
finalFuel1 = (fuelHouse1 * 5);
finalFuel2 = (fuelHouse2 * 5);
finalFuel3 = (fuelHouse3 * 5);
finalHouse1 = costHouse1 + finalTax1 + finalFuel1;
finalHouse2 = costHouse2 + finalTax2 + finalFuel2;
finalHouse3 = costHouse3 + finalTax3 + finalFuel3;
System.out.printf("Initial House Cost\tAnnual Fuel Cost\tTaxes\t\tTotal Cost\n");
System.out.printf("\t%5d\t\t\t%5d\t\t%5d\t\t%5d\n", costHouse1, finalFuel1, finalTax1, finalHouse1);
System.out.printf("\t%5d\t\t\t%5d\t\t%5d\t\t%5d\n", costHouse2, finalFuel2, finalTax2, finalHouse2);
System.out.printf("\t%5d\t\t\t%5d\t\t%5d\t\t%5d\n", costHouse3, finalFuel3, finalTax3, finalHouse3);
}}}
If I run the program as is (or using DecimalFormat as opposed to NumberFormat), I can enter all the numbers and as soon as the 9th is entered, I get
"Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4442)
at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2963)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2918)
at java.base/java.util.Formatter.format(Formatter.java:2689)
at java.base/java.io.PrintStream.format(PrintStream.java:1209)
at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
at houseCost.main(houseCost.java:74)"
What I have read up on is that the NumberFormat and DecimalFormat are not variables to store data, but how to format it. I MUST use one or both of these in the project. I also have to format the output to look like a table which I think I did right with the printf.
I keep getting differing errors seen below. Where the variable initialization comes in is if I switch the bottom lines of code to something like this:
"
System.out.printf("\t%5d\t\t\t%5d\t\t%5d\t\t%5d\n", costHouse1, finalFuel1, finalTax1, fmt1.format(finalHouse1));
"
So, the ultimate question is, how do I resolve the error and take all the inputted numbers, put them through their calculations, and output them in ("0.##") format? Am I close? Have I really bothced this?
Thanks for your help!
To format the values in your output, using NumberFormat class.
Here's how to do it with your current code:
NumberFormat fmt = NumberFormat.getCurrencyInstance();
To display the formatted currency values in your output, you can use the printf method and include the %s placeholder in your format string, then pass the formatted currency values using the fmt.format() method.
System.out.printf("\t%s\t\t\t%s\t\t%s\t\t%s\n",
fmt.format(costHouse1),
fmt.format(finalFuel1),
fmt.format(finalTax1),
fmt.format(finalHouse1));
Regarding the issue with your current code, you are using the %d placeholder for the printf method, which is used for integers. Since your variables are of type double, you should use the %f placeholder instead.
System.out.printf("\t%5f\t\t\t%5f\t\t%5f\t\t%5f\n",
costHouse1,
finalFuel1,
finalTax1,
finalHouse1);
Since you're using double variables, you have to ensure the correct string format in printf(). We generally use %d od %i to represent integer values, and we use %f (f as in floating point) to represent floating point values.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm new to programming. I have a task to make a division of two arbitrary numbers, and to set arbitrary number of decimals. I was searching on the internet, but not really sure how to set it. If I could get some help, would much appreciate!
Here's the code so far:
int a,b, decimala;
System.out.println("first number: ");
a = unos.nextInt();
System.out.println("second number: ");
b = unos.nextInt();
System.out.println("amount of decimals: ");
decimala = unos.nextInt();
double c;
System.out.println(a);
System.out.println(b);
System.out.println("--------------");
c = (double)a/b;
System.out.println(%.decimala+ c);
If you just want to output them you could try using format
String format = "%" + decimala + "f";
System.out.format(format,a);
Here's a cheat sheet with all the stuff you can do.
https://alvinalexander.com/programming/printf-format-cheat-sheet
Thanks to #AndrewGuerra for pointing out how to format a variable amount of decimals
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have the following problem of a task that I need to make. It's about checking if a bankaccount number is correct or not this is the task:
The first group consists of 3 digits and determines which bank it is .
The second group consists of 7 digits and establishes the customer goes to that bank.
The third group consists of two numbers and a check digit which the validity of the bank account is determined .
The verification is done as follows:
The first and second group together form a number consisting of 10 digits. If you divide this number by 97 then the remainder after division by 97 must be equal to the third group of the bankaccount.
Maybe this is what you are looking for:
private boolean checkNumber(String number) {
//number consists of 12 digits
String firstGroup = number.substring(0, 3);
String secondGroup = number.substring(3, 10);
String thirdGroup = number.substring(10, 12);
int firstSecond = Integer.parseInt(firstGroup + secondGroup);
int third = Integer.parseInt(thirdGroup);
int remainderAfterDevision = firstSecond % 97;
return (remainderAfterDevision == third);
}
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 9 years ago.
Improve this question
I am having an issue with an error I am getting when I compile my program. The only issue is that it needs a double and I have a String. I tried to cast it as a double (both variables), but that didn't seem to work. Any help would be appreciated. Here are the lines of code the issue is coming from. Both qualityPoints and sumOfHours are integers and GPA is a double. The error is on the third line.
DecimalFormat df = new DecimalFormat("0.00");
//GPA = (qualityPoints / sumOfHours);
GPA = df.format (((double)qualityPoints) /(double)(sumOfHours));
Error Code:
GPACalculator.java:123: error: incompatible types
GPA = df.format (((double)qualityPoints) /(double)(sumOfHours));
^
required: double
found: String
1 error
Thanks for any help.
GPA = Double.parseDouble(df.format (((double)qualityPoints) /(double)(sumOfHours)));
int qualityPoints = 0;
int sumOfHours = 0;
double GPA = 0;
DecimalFormat df = new DecimalFormat("0.00");
GPA = Double.parseDouble(df.format (((double)qualityPoints) /(double)(sumOfHours)));
No compilation error for this.
Considering that you have a valid double value in qualityPoints string then you can try to use Double.valueOf(String s) method :
GPA = df.format ((Double.valueOf(qualityPoints)) /(double)(sumOfHours));
Does this work?
GPA = df.format ( Double.valueOf(qualityPoints) / Double.valueOf(sumOfHours) );
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to find the sin of a number that the user inputs say for eg 1.5. I've done it on the calculator and it works but the code is not working.
Here is the code:
package msd1;
import java.util.Scanner;
public class Part3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
double Height = scanner.nextDouble();
double Angle = Height/2;
double asine = Math.asin(Angle);
System.out.println("Arcsine of " + Angle + " = " + asine);
}
}
Your variable names make no sense. A "height" is a length, and dividing a length by 2 doesn't give you an "angle". Furthermore, you don't pass an "angle" to asin, you pass it a number from -1 to +1 and it returns an angle.
In your case, you'd want to want to take the height of the ladder and divide it by its length to give you your sin (between -1 and +1), then take the asin of that value.
Odds are you also want to take the angle returned by asin in radians and convert to degrees.
You might have code like this:
double lengthOfLadder = 2.0;
double height = scanner.nextDouble();
double sine = height / lengthOfLadder;
double angleInRadians = Math.asin(sine);
double angleInDegrees = angleInRadians / Math.PI * 180;