Multiplying a string containing numbers with another string containing same? [closed] - java

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 2 years ago.
Improve this question
Suppose I have a string
String string = "1.18";
I want to multiply the above string with another string that is
String f1= "2.54";
What would I have to do to multiply these two values? I looked this up online and came across a function called Integer.ParseInt() that converts the string to it's numerical values, so i tried
System.out.println(Integer.parseInt(string) * Integer.parseInt(f1));
It doesn't work. I want to know what am I doing wrong. What's the proper way of doing it? I would find it very helpful if someone could help me understand, thanks!

Try Double.parseDouble(string)

Integer is not for decimal number. Use Double.parseDouble() or use BigDecimal type if you need exact result.

Related

Format a fraction in 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 4 years ago.
Improve this question
Is there any way to print a fraction in any form? I'm working on a math program and a fraction is much easier to read than 1/2.
If there is any API that can do written math, I would also be glad.
Example:
Instead of 1/2
There isn't a one-liner answer for your question, so try approaching it from a different angle. You could write a method that takes the numerator and denominator as arguments and then return an output string in any format you want, such as String.format("%d\n---\n%d",numerator,denominator);
if your printing it without quotes your pretty much just turning it into a decimal because your dividing the 2. An example to avoid this:
System.out.println("1/2");

Get a Java string of form (num/num), to become two double values [closed]

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 6 years ago.
Improve this question
For example, if I have a string (24/25), how would I go about being left with two double values of 24 and 25. My objective to to eventually, given a set of number of that form, divide each of them, and add to get the average. I'm fairly new to Java and I honestly am so confused as where to even begin. Thank you!
As Andreas said. First you need to take the substring between brackets. Use String.substr
Now split 24/25 to 24 and 25. You can use String.split
Then you can parse them with Double.parse

What is Move the decimal value and correct value in 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 an amount of 12.35 like this:
Double x = 12.35
And I would like to change the amount to 35.12 like this:
println x >> 35.12
I am not able to find a solution in Java, where I could do that.
What you're trying to do isn't a mathematical operation, so you really shouldn't try to treat it as one. Instead, treat it as a string manipulation.
Convert the number to a String, and split it up at the decimal point. Then recombine the string with the before and after parts swapped.
String[] xParts = Double.toString(x).split("[.]");
System.out.println(xParts[1] + "." + xParts[0]);

Trying to pull out certain numbers from a string [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 8 years ago.
Improve this question
I have a string of numbers which looks like this
["100000100685716-2","603834770-2", "604544970-3"]
can someone help me with a regular expression to match each long (first number before the "-") so i can add it to an array?
The following regex should work: (\d+)-
You do not need a regex here:
Use int pos = str.indexOf('-') to get the location of the dash
Use str.substring(0, pos) to get the initial portion of the string.
If dashes in the input strings are optional, you would need to add a check of pos to be non-negative.
(?:\"\d+)
This should do exactly what you want
String s = "[\"100000100685716-2\",\"603834770-2\", \"604544970-3\"]";
System.out.println(s.split("-")[0].substring(2));

How do you add JTextFields? [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 8 years ago.
Improve this question
I am making a GUI in Java, and I was wondering how do you add user inputs (integers) in a JTextField? If so, please post with some code so I can understand. (I am just a beginner programmer)
What all you need to do:
jTextField.setText(String.valueOf(intValue));
Because JTextField takes a String to set its value you have to convert the int to String.
See This for more details.
A JTextField contains text. You get this text using textField.getText(), which returns a String. This String might happen to represent a valid integer. If you want to get the value of the integer, you need to transform the String into an int. That's done using the Integer.parseInt() method:
int sum = Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText());
Of course, if one of the text fields contains text that doesn't represent an integer, you'll get an exception, as documented in the Integer.parseInt() javadoc.
Given your question, it seems you don't really understand basic notions such as types, and basic classes such as String and Integer. My advice would be to forget about Swing for the moment, and exercise with basic programs not involving any UI.

Categories