fractions changing to decimal - fractions

this is my code but when it shows up on my android display it changes my fractions to a decimal how do i keep it in this format.
public void readyForJavascript(final String arg) {
webView.loadUrl("javascript:SpinningWheel.addSlot({ " +
"1:1, 2: 5/16, 3: 3/8, 4: 7/16, 5: 1/2, 6: 9/16," +
"7: 5/8, 8: 3/4, 9: 7/8, 10: 1, 11: 1/18, 12: 1/4 });");
webView.loadUrl("javascript:SpinningWheel.addSlot({ " +
"1: 'AM', 2: 'PM'});");
webView.loadUrl("javascript:SpinningWheel.addSlot({ " +
"1: 'AM', 2: 'PM'});");
webView.loadUrl("javascript:SpinningWheel.open();");
}

You need to enclose your fractions in single quotes:
"1:1, 2: '5/16', 3: '3/8' .......
I think at some point your Javascript sees 3/8 and evaluates it as a calculation.

Related

Using Switch and JOptionPane

Problem: Ask the user to enter 0-9999 and convert it into words using switch case and JOptionPane...
My question: I figure out how to convert the other numbers but if I entered 11-19, the output for example I enter 11 - "Ten One" how can I get the right output for this?
public class NumbertoWords {​​
/**
* #param args the command line arguments
*/
public static void main(String[] args) {​​​​​​​​​
// TODO code application logic here
int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a Number from [0-9999]"));
int thou=num/1000;
int temp=num%1000; // 9999
int hun=temp/100; //999 -> 9
int temp1=temp%100;
int tens=temp1/10; //99
int ones=temp%10;
String display="";
switch(thou){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=" Nine Thousand"; break;
case 8: display=" Eight Thousand"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
switch (hun){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=display+ " Nine Hundred"; break;
case 8: display=display+ " Eight Hundred"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
switch (tens){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=display+ " Ninety"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
switch (ones){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=display+ " Nine"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
JOptionPane.showMessageDialog(null, display,"Number to Words",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

How can add a "less than" statement into this program

I've been tasked with creating a program using a switch statement to print the month names according to the exact number of days in the month. The program also needs to display an "error" message if the number of days entered does not correspond to any month. My code achieves these two goals. However i am wondering if there is shorter way to get the "error" message using a "less than" statement for when the user enters a number less than 28
import java.util.Scanner;
public class months {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numb_days = 0;
System.out.println("Enter the # of days");
numb_days = keyboard.nextInt();
//This program will print 'May' when you run it.
String monthName;
switch (numb_days) {
case 30: monthName = "April, June, September, November ";
break;
case 31: monthName = "January, March, May, July, August, November, October, December ";
break;
case 28: monthName = "February";
break;
case 29: monthName = "February (only on leap years)";
break;
case 1: monthName = "Unknown";
break;
case 2: monthName = "Unknown";
break;
case 3: monthName = "Unknown";
break;
case 4: monthName = "Unknown";
break;
case 5: monthName = "Unknown";
break;
case 6: monthName = "Unknown";
break;
case 7: monthName = "Unknown";
break;
case 8: monthName = "Unknown";
break;
case 9: monthName = "Unknown";
break;
case 10: monthName = "Unknown";
break;
case 11: monthName = "Unknown";
break;
case 12: monthName = "Unknown";
break;
case 13: monthName = "Unknown";
break;
case 14: monthName = "Unknown";
break;
case 15: monthName = "Unknown";
break;
case 16: monthName = "Unknown";
break;
case 17: monthName = "Unknown";
break;
case 18: monthName = "Unknown";
break;
case 19: monthName = "Unknown";
break;
case 20: monthName = "Unknown";
break;
case 21: monthName = "Unknown";
break;
case 22: monthName = "Unknown";
break;
case 23: monthName = "Unknown";
break;
case 24: monthName = "Unknown";
break;
case 25: monthName = "Unknown";
break;
case 26: monthName = "Unknown";
break;
case 27: monthName = "Unknown";
break;
default: monthName = "Unknown";
break;
}
System.out.println(monthName);
}
}
The default statement is sufficient for handling all other cases that aren't caught by the specific cases for 28-31. So you can remove all other cases 1-27.
If you are required to use a less than statement you can use it before using the switch statement:
if (numb_days < 28) {
month_name = "unknown";
} else {
// Switch statement goes here
}

how to update nested document using condition in mongodb with node js

I am using mongodb to store time series data in following way
values: {
0: { 0: 999999, 1: 999999, …, 59: 1000000 },
1: { 0: 2000000, 1: 2000000, …, 59: 1000000 },
…,
58: { 0: 1600000, 1: 1200000, …, 59: 1100000 },
59: { 0: 1300000, 1: 1400000, …, 59: 1500000 }
}
But now i want to update specific range value
FOR EXAMPLE. i want to set all values to 0 greater than values.30.40 where VALUES is the main node, 30 is MINUTES and 40 is SECOND
Would it be possible to change the inner Json to an array ?
That way, you can access and modify using indexes.
It would also help when you do not have values at some indexes.
values: {
0:{[12,25,36,........,12]},
.
.
.
}

How to substitute an empty string (or null) with a default string concisely in Scala

I have a method that returns a String. I want to substitute it with a default value such as "<empty>" if it returns an empty string or null. Let's assume its name is getSomeString, it is an expensive operation so I can call it only once, and I can't change its return type to Option[String]. For now, I'm doing the following:
val myStr = {
val s = getSomeString
if (s == null || s.isEmpty) "<empty>" else s
}
Is there a simpler way to achieve the same thing?
val myStr = Option(getSomeString).filterNot(_.isEmpty).getOrElse("<empty>")
Updated
I posted this code because I think the intent in this code is clear than if/else or pattern matching version, but I didn't consider the performance issue.
As the others in comments mentioned, this code is much slower than simple if / else or pattern matching(this line will create a lot new objects which is an expensive operation), so please do not use this code when performance is an issue.
Given an expensive function:
scala> def s(i: Int): String = i match { case 0=>null case 1=>"" case 2=>"hi" }
s: (i: Int)String
I think this is easy to read and free of overhead, cf this in the wild:
scala> def q(i: Int) = s(i) match { case ""|null => "<empty>" case x => x }
q: (i: Int)String
scala> q(0)
res3: String = <empty>
scala> q(1)
res4: String = <empty>
scala> q(2)
res5: String = hi
To my eyes, this is not as expressive, even with minimalist punctuation:
scala> Option(s(0)) filterNot (_.isEmpty) getOrElse "<empty>"
res6: String = <empty>
Moreover, contrast the cost in anonfun classes for the closures and additional method invocations:
scala> :javap -
Size 1161 bytes
MD5 checksum 765f5f67b0c574252b059c8adfab1cf0
Compiled from "<console>"
[...]
9: getstatic #26 // Field scala/Option$.MODULE$:Lscala/Option$;
12: getstatic #31 // Field .MODULE$:L;
15: iconst_0
16: invokevirtual #35 // Method .s:(I)Ljava/lang/String;
19: invokevirtual #39 // Method scala/Option$.apply:(Ljava/lang/Object;)Lscala/Option;
22: new #41 // class $anonfun$1
25: dup
26: invokespecial #42 // Method $anonfun$1."<init>":()V
29: invokevirtual #48 // Method scala/Option.filterNot:(Lscala/Function1;)Lscala/Option;
32: new #50 // class $anonfun$2
35: dup
36: invokespecial #51 // Method $anonfun$2."<init>":()V
39: invokevirtual #55 // Method scala/Option.getOrElse:(Lscala/Function0;)Ljava/lang/Object;
42: checkcast #57 // class java/lang/String
45: putfield #17 // Field res6:Ljava/lang/String;
The pattern match is generally just an if-else, smaller and faster (even considering that it doesn't optimise s == "" to s.isEmpty):
scala> :javap -r #q
public java.lang.String q(int);
flags: ACC_PUBLIC
Code:
stack=2, locals=5, args_size=2
0: getstatic #19 // Field $line3/$read$$iw$$iw$.MODULE$:L$line3/$read$$iw$$iw$;
3: iload_1
4: invokevirtual #22 // Method $line3/$read$$iw$$iw$.s:(I)Ljava/lang/String;
7: astore_3
8: ldc #24 // String
10: aload_3
11: invokevirtual #28 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
14: ifeq 22
17: iconst_1
18: istore_2
19: goto 33
22: aload_3
23: ifnonnull 31
26: iconst_1
27: istore_2
28: goto 33
31: iconst_0
32: istore_2
33: iload_2
34: ifeq 44
37: ldc #30 // String <empty>
39: astore 4
41: goto 47
44: aload_3
45: astore 4
47: aload 4
49: areturn
But inspired by the other answer, even if I would never take this code home to meet my parents (because it incorrectly converts the value "null" if the expensive function returns it -- though maybe it's a feature to do that), here is a regex:
scala> def p(i: Int) = "" + s(i) replaceAll ("^null$|^$", "<empty>")
p: (i: Int)String
The "" + s(i) is a shorthand for String.valueOf, which of course produces the String "null" for a null reference value. I appreciate SO's ability not only to generate quick answers to questions, but to encourage some out-of-the-box thinking.
You could replace null with empty string in first step using Option and then substitute with default text if result is empty (whether because it was empty originally or because it was null):
Option(getSomeString).getOrElse("").replaceAll("^$","<empty>")
You could add a method to String using an implicit value class
object ImplicitClassContainer {
implicit class RichString(val s: String) extends AnyVal {
def getOrDefault(defaultValue: String): String = {
s match {
case null | "" => defaultValue
case x => x
}
}
}
to be used like this
import ImplicitClassContainer._
println("hi".getOrDefault("<empty1>"))
println("".getOrDefault("<empty2>"))
val s: String = null
println(s.getOrDefault("<empty3>"))
so even the method call on null is handled gracefully (Scala 2.10.1).
If you don't need changes:
Option(getSomeString).fold("<empty>")(s => s)
If you need to modify if for nonEmpty result. Simple example:
Option(getSomeString).fold("<empty>")(str => s"xxxxxxxx$str")
val myStr = getSomeString match { case ""|null => "<empty>" case s => s }

Double or single switch

Well it might be a dumb question, but I'm unable to find an answer:
The case is simple, I have a function with an integer in entry and two variable to assign depending of that. The problem is that the value assigned to the variables is common to certain case, but those case are not the same for the two variables. (if it's not clear enough, see the example).
I was wondering what was the best practice for such a case.
Is it something like :
function test(a){
var x,y;
switch (a){
case 0:
case 1:
case 7:
y=...;
break;
case 2:
case 6:
y=...;
break;
case 3:
case 4:
case 5:
y=...;
}
switch(a){
case 5:
case 6:
case 7:
x=...;
break;
case 0:
case 4:
x=...;
break;
case 1:
case 2:
case 3:
x=...;
}
...
}
or
function test(a){
var x,y;
switch (a){
case 0:
x=...;
y=...;
break;
case 1:
x=...;
y=...;
break;
case 2:
x=...;
y=...;
break;
case 3:
x=...;
y=...;
break;
case 4:
x=...;
y=...;
break;
case 5:
x=...;
y=...;
break;
case 6:
x=...;
y=...;
break;
case 7:
x=...;
y=...;
}
...
}
Or to use a mix of the two with the value assigned to x in each case and to make groups for the value of y?
Note that there might be more than 8 value, it's just for the example.
Thanks in advance.
If there's no real overlap between the different cases in the switch statements, you're probably better off having them separate.
If there was a majority of commonality between the actions, you could combine them with certain special cases within the individual case blocks but you seem to indicate that's not the case here.
However, if this isn't just a simple example of a much more complex case, you can achieve a more compact solution with something like:
// index: 0 1 2 3 4 5 6 7 8 9 10 11 12
static const int lookupX[] = { 2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9};
static const int lookupY[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9};
if ((a >=0) && (a < sizeof(lookupX) / sizeof(*lookupX)))
x = lookupX[a];
if ((a >=0) && (a < sizeof(lookupY) / sizeof(*lookupY)))
y = lookupY[a];
This will allow you to keep the values in a much smaller "configuration" section so that you can easily see the intent. The range checking and lookup then become very simple.
That code is tailored to C - I'm not sure what specific language you're using (because of the var statement) but it's basically only doing the lookup if the index will be valid. You'll need to translate that bit to your language of choice.

Resources