I just coded to put an array of double values in the JsonObject. But, all my double values are converted to int values, when i print it. can someone help me understand what is happening behind? Please let me know the best way to put primitive arrays in JsonObject
public class JsonPrimitiveArrays {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
double[] d = new double[]{1.0,2.0,3.0};
jsonObject.put("doubles",d);
System.out.println(jsonObject);
}
}
Output:
{"doubles":[1,2,3]}
Its in real not getting converted into int. Only thing happening is as JS Object its not showing .0 which is not relevant.
In your sample program, change some of the value from double[] d = new double[]{1.0,2.0,3.0} to
double[] d = new double[]{1.0,2.1,3.1} and run the program.
You will observer its in real not converting into int. The output you will get is {"doubles":[1,2.1,3.1]}
Looking at toString of net.sf.json.JSONObject it eventually calls the following method to translate the numbers to String (source code here):
public static String numberToString(Number n) {
if (n == null) {
throw new JSONException("Null pointer");
}
//testValidity(n);
// Shave off trailing zeros and decimal point, if possible.
String s = n.toString();
if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
while (s.endsWith("0")) {
s = s.substring(0, s.length() - 1);
}
if (s.endsWith(".")) {
s = s.substring(0, s.length() - 1);
}
}
return s;
}
It clearly tries to get rid of the trailing zeroes when it can, (s = s.substring(0, s.length() - 1) if a String is ending in zero).
System.out.println(numberToString(1.1) + " vs " + numberToString(1.0));
Gives,
1.1 vs 1
All numbers are floats in Javascript. So, 1.0 and 1 are the same in JS. There is no differenciation of int, float and double.
Since JSON is going to end up as a JS object, there is no use in adding an extra '.0' since '1' represents a float as well. I guess this is done to save a few bytes in the string that is passed around.
So, you will get a float in JS, and if you parse it back to Java, you should get a double. Try it.
In the mean time, if you are interested in the way it displays on screen, you can try some string formatting to make it look like '1.0'.
Related
I need something to turn somthing like this: 29939299322 into 299392.99322 automatically as it has to do this alot.
i have found things like format but it only seems to be able to add things in front of the number but this has to happen after the sixth number.
preferably from an int directly into a double. but i haven't even found a way t do it from a string.
so if any of u know if this is even possible please help me do this.
and it has to be java. if u also know any documentation which can help me also post it,
thanks in advance
Well, if you are using Strings, you could use regex (Not really efficient but clean and readable:P) :
public static void main(String[] args) {
String s1 = "29939299322";
System.out.println(s1.replaceAll("(\\d{6})(\\d+)", "$1.$2"));
}
O/P :
299392.99322
Code:
public static void main(String args[]) {
int intInput = 123456789; // I am using 123456789 because you want to
// input an integer, and "29939299322" is
// greater than the maximum value for an
// integer: "2147483647"
String input = intInput + ""; // Converts the input into a String
String output = ""; // This is the output String, it will later be
// converted into a double
for (int i = 0; i < input.length(); i++) { // Loops through the input
if (i == input.length() - 5) { // If the index is at the certain
// point in the String...
output += "."; // Adds "."
}
output += input.charAt(i) + ""; // Adds the character into the
// output String after converting it
// into a String
}
double outputDouble = Double.parseDouble(output); // Parses the output
// into a Double
System.out.println(outputDouble); // Outputs Double
}
Output:
1234.56789
I'm new to Java so I'm trying to write random programs to figure it out. I'm trying to write something that takes as user-input a quadratic equation like so: x^2 + 3x -1
Maybe this is too advanced (or maybe it isn't) but I'm wondering how to extract the characters one-by-one in a loop. If it was all digits I think I could use .isDigit() and save them to an array, but because they're different data types I'm not sure how to go about doing this. My 'code' so far looks like this
import java.lang.String;
import java.lang.StringBuffer;
import java.util.Scanner;
import java.lang.Character;
public class Lab
{
public static void main(String[] args)
{
Scanner user_input = new Scanner(System.in);
System.out.print("Please input the quadratic equation (ex: 2x^2 + 3x - 2): ");
String request = user_input.nextLine();
int myArr[];
String lettArr[];
for (int i = 0; i <= request.length(); i++)
{
String c = request.charAt(i);
if (request.isDigit(c))
{
myArr[1] += c;
}
if(request.isLowerCase(c))
{
lettArr[1] += c;
}
}
System.out.println(myArr[0]);
}
}
my .isDigit() and .isLowerCase() methods are not working. I think I'm using them in the right sense. This is pretty complex for my level and I'm wondering if this is a dead-end or an acceptable strategy.
Thanks.
I think what your are trying to do is to extract the coefficients from the user input. Your approach might work but there would be many case that you have to consider (+/- signs for example). Instead why don't you try Java's regular expressions
String input = "2x^2 - 4x + 1";
input = input.replaceAll("\\s", ""); //removes all whitespaces
Pattern p = Pattern.compile("(-?\\d+)x\\^2((\\+|-)\\d+)x((\\+|-)\\d+)");
Matcher m = p.matcher(input);
if (!m.matches()) {
System.out.println("Incorrect input");
return;
}
int a, b, c;
a = Integer.parseInt(m.group(1));
b = Integer.parseInt(m.group(2));
c = Integer.parseInt(m.group(4));
System.out.println(String.format("a=%d, b=%d, c=%d", a, b, c));
You can adapt this fragment and use it in your code. I , however, supposed that your coefficients are integer numbers. If you need them, instead, to be double you have to change the format of the given regex and also to change Integer.parseInt to Double.parseDouble. I could write this in more details if you are interested.
There are a few things wrong with your code:
public class Lab
{
public static void main(String[] args)
{
Scanner user_input = new Scanner(System.in);
System.out.print("Please input the quadratic equation (ex: 2x^2 + 3x - 2): ");
String request = user_input.nextLine();
int myArr[]; //not initialized
String lettArr[]; //should be a character type & not initialized
for (int i = 0; i <= request.length(); i++)
{
String c = request.charAt(i); // returns a char
if (request.isDigit(c))
{
myArr[1] += c; // not right, myArr is ints and c is a char
}
if(request.isLowerCase(c))
{
lettArr[1] += c; // not right
}
}
System.out.println(myArr[0]); //only prints one char (you might want this
}
}
1.
You are extracting a character from the input string and trying to add it to the second entry in an uninitialized array. You're line in code is:
myArr[1] += c;
myArr is an integer array and c is a character. You can't do that in java. What's more, you are trying to add a char to an int, which was not initialized in the first place!! The type of everything in an array must be the same. This gets more complicated when it comes to inheritance and such, but for now just know that you can't do that. If you wanted the Integer value of a character you can use:
Integer.parseInt(c)
I'm not sure what you are trying to do with your statement, but I'm 90% sure that it's not trying to do what you want it to. For reference:
myCharArr[i] = c;
assigns the i-th element (starting from 0) to the value of c. So if i=1 and myCharArr was initialized to 3 elements long, it would look like this:
[ ? | c | ?]
where ? is just a garbage value.
2.
In java you need to initialize your arrays, or use a more dynamic List object. The thing with primitive arrays is that their size cannot change, i.e. when an primitive array is initialized:
int arr[] = new int[5];
it stays the same size (in this case 5). If you use something like an ArrayList, you can add as many things as you want. The way you would initialize ArrayLists would be like:
ArrayList<Integer> intArr = new ArrayList<Integer>();
ArrayList<Character> charArr = new ArrayList<Character();
and with those initialized you can do:
intArr.add(someInt);
charArr.add(someChar);
You can use primitive arrays for this problem but it will save you a bit of trouble if you use Lists.
Read up on arrays.
public static double convertWeight(String value, String value2) {
double lbs = 0;
double ounces = 0;
if (!value.equals("")) {
lbs = Double.parseDouble(value);
}
if (!value2.equals("")) {
ounces = Double.parseDouble(value2) * 0.062500;
}
double grams = (lbs + ounces) / 0.0022046;
return grams;
}
I have this piece of code, where at sometimes i get pounds or oz value empty. The above piece of code works fine, but i am not happy the way i have written the code. Can anyone tell me a better alternate way.
I have to convertWeight. So from my service i get sometimes values or sometimes just empty strings. If its empty i ensure that i pass lbs or ounces "Zero" converWeight(value1, value2);
Java 6 implemented a new String method, isEmpty(). As long as you're running 6 or above, you can rewrite your code using this method. It's a bit more readable.
try {
if (!isEmpty(value)) {
lbs = Double.parseDouble(value);
}
if (!isEmpty(value2)) {
ounces = Double.parseDouble(value2) * 0.062500;
}
} catch (NumberFormatException nfe) {
//Do some notification
System.err.println("Invalid input.");
}
As an added suggestion .. You may want to wrap this code in a try/catch block. If you don't, your program will crash with an uncaught exception if it tries to parse anything other than a valid double for either values.
I usually do it this way
if (value != null && !value.trim().isEmpty())
Null check to avoid an NPE
Followed by a trim() to strip any extraneous spaces
Followed by isEmpty() that returns true for length > 0
I recommend using the Apache StringUtils.isEmpty(), .isNotEmpty(), .isBlank() and .isNotBlank(). Keeping Apache Commons around is useful for a lot of Java quirks, not just Strings.
http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html
use String.isEmpty method
public static double convertWeight(String value, String value2) {
double lbs = (value.isEmpty())?0:Double.parseDouble(value);
double ounces = (value2.isEmpty())?0:Double.parseDouble(value2) * 0.062500;
return (lbs + ounces) / 0.0022046;
}
I have Strings (from DB), which may contain numeric values. If it contains numeric values, I'd like to remove trailing zeros such as:
10.0000
10.234000
str.replaceAll("\\.0*$", ""), works on the first one, but not the second one.
A lot of the answers point to use BigDecimal, but the String I get may not be numeric. So I think a better solution probably is through the Regex.
there are possibilities:
1000 -> 1000
10.000 -> 10 (without point in result)
10.0100 -> 10.01
10.1234 -> 10.1234
I am lazy and stupid, just
s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
Same solution using contains instead of indexOf as mentioned in some of the comments for easy understanding
s = s.contains(".") ? s.replaceAll("0*$","").replaceAll("\\.$","") : s
Use DecimalFormat, its cleanest way
String s = "10.1200";
DecimalFormat decimalFormat = new DecimalFormat("0.#####");
String result = decimalFormat.format(Double.valueOf(s));
System.out.println(result);
Kent's string manipulation answer magically works and also caters for precision loss, But here's a cleaner solution using BigDecimal
String value = "10.234000";
BigDecimal stripedVal = new BigDecimal(value).stripTrailingZeros();
You can then convert to other types
String stringValue = stripedVal.toPlainString();
double doubleValue = stripedVal.doubleValue();
long longValue = stripedVal.longValue();
If precision loss is an ultimate concern for you, then obtain the exact primitive value. This would throw ArithmeticException if there'll be any precision loss for the primitive. See below
int intValue = stripedVal.intValueExact();
String value = "10.010"
String s = new DecimalFormat("0.####").format(Double.parseDouble(value));
System.out.println(s);
Output:
10.01
I find all the other solution too complicated. Simply
s.replaceFirst("\\.0*$|(\\.\\d*?)0+$", "$1");
does the job. It tries the first alternative first, so that dot followed by all zeros gets replaced by nothing (as the group doesn't get set). Otherwise, if it finds a dot followed by some digits (as few as possible due to the lazy quantifier *?) followed by some zeros, the zeros get discarded as they're not included in the group. It works.
Warning
My code relies on my assumption that appending a unmatched group does nothing. This is true for the Oracle implementation, but not for others, including Android, which seem to append the string "null". I'd call the such implementations broken as it just may no sense, but they're correct according to the Javadoc.
The following works for all the following examples:
"1" -> "1"
"1.0" -> "1"
"1.01500" -> "1.015"
"1.103" -> "1.103"
s = s.replaceAll("()\\.0+$|(\\..+?)0+$", "$2");
What about replacing
(\d*\.\d*)0*$
by
\1
?
You could replace with:
String result = (str.indexOf(".")>=0?str.replaceAll("\\.?0+$",""):str);
To keep the Regex as simple as possible. (And account for inputs like 1000 as pointed out in comments)
My implementation with possibility to select numbers of digits after divider:
public static String removeTrailingZero(String number, int minPrecise, char divider) {
int dividerIndex = number.indexOf(divider);
if (dividerIndex == -1) {
return number;
}
int removeCount = 0;
for (int i = dividerIndex + 1; i < number.length(); i++) {
if (number.charAt(i) == '0') {
removeCount++;
} else {
removeCount = 0;
}
}
int fracLen = number.length() - dividerIndex - 1;
if (fracLen - removeCount < minPrecise) {
removeCount = fracLen - minPrecise;
}
if (removeCount < 0) {
return number;
}
String result = number.substring(0, number.length() - removeCount);
if (result.endsWith(String.valueOf(divider))) {
return result.substring(0, result.length() - 1);
}
return result;
}
In addition to Kent's answer.
Be careful with regex in Kotlin. You have to manually write Regex() constructor instead of a simple string!
s = if (s.contains("."))
s.replace(Regex("0*\$"),"").replace(Regex("\\.\$"),"")
else s
Try to use this code:
DecimalFormat df = new DecimalFormat("#0.#####");
String value1 = df.format(101.00000);
String value2 = df.format(102.02000);
String value3 = df.format(103.20000);
String value4 = df.format(104.30020);
Output:
101
102.02
103.2
104.3002
Separate out the fraction part first. Then you can use the below logic.
BigDecimal value = BigDecimal.valueOf(345000);
BigDecimal div = new BigDecimal(10).pow(Integer.numberOfTrailingZeros(value.intValue()));
System.out.println(value.divide(div).intValue());
i have a string like this
106.826820,-6.303850,0
which i get from parsing the google Maps KML Document.
now i wanna parse its string to Double
this is my code :
public Double getLatitude(){
for(int posisi = GPS_NAME.indexOf(","); posisi > GPS_NAME.length(); posisi++){
TEMP_LAT = "" + GPS_NAME.indexOf(posisi);
}
GPS_LATITUDE = Double.valueOf(TEMP_LAT);
return GPS_LATITUDE;
}
public Double getLongitude(){
int posisiakhir = GPS_NAME.indexOf(",");
for(int i = 0; i < posisiakhir; i++){
TEMP_LON = "" + GPS_NAME.indexOf(i);
}
GPS_LONGITUDE = Double.valueOf(TEMP_LON);
return GPS_LONGITUDE;
}
but when i try to run it i got error like this
could somebody help me solving my problems >_<
and also confirm GPS_LATITUDE = Double.valueOf(TEMP_LAT); TEMP_LAT is not null there - as exception is NullPointerException not NumberFormatException.
You have null pointer exception so you should to it like this:
public Double getLatitude(){
for(int posisi = GPS_NAME.indexOf(","); posisi > GPS_NAME.length(); posisi++){
TEMP_LAT = "" + GPS_NAME.indexOf(posisi);
}
if (TEMP_LAT != null) {
GPS_LATITUDE = Double.parseDouble(TEMP_LAT);
}
return GPS_LATITUDE;
}
And for converting to double rather you should use Double.parseDouble() or also you can use new Double(TEMP_LAT).doubleValue() but first approach is cleaner.
Besides the probable causes for the NPE, I don't really get your logic; it looks completely dodgy to me.
Just some examples:
GPS_NAME.indexOf(",") will either return -1 or an index that is smaller than the length of the string in which is being searched. Then why have a condition that checks if it is larger than the length? posisi > GPS_NAME.length() will never be true, hence the for loops are useless...
Then inside the loops you do TEMP_LAT = "" + GPS_NAME.indexOf(posisi). From the earlier remark we know that posisi is either -1 or some other number that is smaller than the length of the string. So GPS_NAME.indexOf(posisi) will try to find a character repesented by the integer posisi (which will be a rather small number) in the string. How does that make sense?
I'd like to advise you to rethink your logic - perhaps String.split(",") is a good starting point.
Use Double.parseDouble(TEMP_LAT);