Converting String to Double in Java [closed] - java

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) );

Related

Java Double formatting String output [closed]

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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Not suitable for this site
Improve this question
I'm having some problem formatting decimals of a double. The only error is:
String output = DecimalFormat = (((P * t) / 360) * 0.025);
DecimalFormat df = new DecimalFormat(" ###,###.00");
String output = DecimalFormat = (((P * t) / 360) * 0.025);
DecimalFormat df = new DecimalFormat("###,###.00");
String output = df.format(((30 * 45) / 360) * 0.025);
In your second line, you have DecimalFormat = which should be replaced with df.format The DecimalFormat class doesn't let you format decimals, it's the df object that you created which can format decimals by calling the format method.

How to calculate gravitational force? [closed]

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 2 years ago.
Improve this question
I am getting the wrong output for the code below. G = 6.674 x 10^-11. I am supposed to get 3.559466666666664e+22 but I am getting 8.0088E44. Can someone please explain what I have wrong on my code? I will appreciate it. The inputs are 2e30 6e24 1.5e11
import stdlib.StdOut;
public class GraviForce {
public static void main(String[] args) {
double m1 = Double.parseDouble(args [0]);
double m2 = Double.parseDouble(args [1]);
double r = Double.parseDouble(args [2]);
double G = 6.674e-11;
double f = G * (m1 * m2) / r * r;
StdOut.println(f);
A simple operator associativity mistake - missing parens:
double f = G * (m1 * m2) / (r * r);

Put point in string value (android java) [closed]

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 6 years ago.
Improve this question
I have BMI calculator app that takes some input and puts it in "EditText". Here is what I am trying to do:
If the input is 170, it will become 1.70.
If the input is 1.70, it will not change.
This is the code I have:
String weight = editText.getText().toString();
Cant you convert the string to an integer and take modulus 100 to the cms and divide by 100 to get in meters?
You can convert the String weight to int like this
int wgt = Integer.parseInt(weight);
Then separating meters and centimetres.
int mtrs = wgt / 100;
int cms = wgt % 100;
Then combining both
String result = mtrs + "." + cms;
try something like this
float value = Float.parse("170");
editText.setText(String.Format(Locale.ENGLISH,"%,02f", value/100f))
I found this way works: I take the number that the user put, and then "170/100" + "0" gives me 1.70

error: cannot find symbol input.nextdouble [closed]

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
Cant figure out why I keep getting this error. I'm trying to write a program that will add the sum of a 2 digit number.
SplitNum.java:9: error: cannot find symbol
double digit = input.nextdouble();
^
symbol: method nextdouble()
location: variable input of type Scanner
1 error
Code:
import java.util.Scanner;
public class SplitNum
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter 2 digit number");
double digit = input.nextdouble();
double tens = digit / 10.0;
double ones = digit % 10.0;
double sum = tens+ones;
System.out.println(sum);
}//end main
}//end class
Change the method name
nextdouble() to
nextDouble()
It's supposed to be input.nextDouble() instead of input.nextdouble()
In Java, coding conventions say that for method names, the first letter of every word, excluding the first one, must be capitalized.

find sin-1 of user input for ladder hight [closed]

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;

Categories

Resources