I'm trying to learn and create a basic sales tax app (inputs from item price and tax). In order to do this, I need to use a float or double for the decimals. I am very new to kotlin and don't know much, so I do not understand how I can get my num1 to parseDouble, I can clearly do it with the Integer data type with no errors. I've also tried toDouble which is giving me errors. Any help would be appreciated, thank you!
val calcButton = findViewById<Button>(R.id.calcButton)
calcButton.setOnClickListener {
var textPrice: EditText = findViewById(R.id.textPrice)
var textTax: EditText = findViewById(R.id.textTax)
var textTotal: EditText = findViewById(R.id.textTotal)
// error here: "Unresolved reference: parseDouble"
var num1 = Double.parseDouble(textTax.getText().toString());
// "parseInt" is working, however
var num2 = Integer.parseInt(textTax.getText().toString());
}
Try:
textTax.getText().toString().toDouble();
The reason you can't use Double.parseDouble is because Double in kotlin is not exactly the same as Double in Java, therefore, just calling Double will call kotlin's version, which doesn't have parseDouble static method.
If you want to use the Double from java.lang, you must specify the full package name:
java.lang.Double.parseDouble(textTax.getText().toString());
Or you can also do:
import java.lang.Double.*;
parseDouble(textTax.getText().toString());
I would suggest just sticking with Kotlin's versions, as they usually result in shorter code.
Related
I'm learning Kotlin, and in my project I have something like the following
Utils.kt:
var weightInKilos = 100.0
//should multiply the above var
fun doSomething(multiplier: Double, weightInKilos: Double) {
weightInKilos = weightInKilos * multiplier
}
print(doSomething(4.2, weightInKilos))
This would be the entire file (it's not part of an object,) so I can't use the this keyword. I know I could just rename one of them but is there some kind of identifier I can use to distinguish the two vars so the code prints 420?
Use the package name as the identifier. If the enclosing package for Utils.kt file is com.example, you would use com.example.weightInKilos = weightInKilos * multiplier.
Thank you Android Studio autocomplete.
I'm trying to switch out a direct integer with a variable in swift, but for some reason I'm getting this error and I have no idea. The end goal is to get my currentValue (line 76) to replace the 100's on line 41 - could anyone let me know how I could accomplish this without the error? New to swift and having a hard time (background in objective-c, figured something this simple would not stop me in my tracks!)
Full .swift file here: http://pastebin.com/K6UHkNEv
EDIT:
// these values change the number of squares
let _gameView = CGOLView(gridWidth:100, gridHeight:100)
#IBOutlet weak var tileSizeSlider: UISlider!
#IBAction func sliderValueChanged(sender: UISlider) {
var currentValue = Int(sender.value)
print("\(currentValue)")
}
should work as:
// these values change the number of squares
let _gameView = CGOLView(gridWidth:currentValue, gridHeight:currentValue)
#IBOutlet weak var tileSizeSlider: UISlider!
#IBAction func sliderValueChanged(sender: UISlider) {
var currentValue = Int(sender.value)
print("\(currentValue)")
}
instead I get this error:
Use of unresolved identifier 'currentValue'
and if I try to create custom int's and input them:
var gridWidthValue = 50
var gridHeightValue = 50
like this:
let _gameView = CGOLView(gridWidth:gridWidthValue, gridHeight:gridHeightValue)
I get:
'ViewController.Type' does not have a member named 'gridHeightValue'
Any help would be appreciated - thanks stackoverflow community!
David.
currentValue is a local variable to sliderValueChanged.
Instead you should instantiate _gameView in init. Note however, you still won't be able to use currentValue.
If this is a one off sort of thing, you can always make _gameView an optional and then create it when you have adjusted the slider. This is admittedly a little clumsy.
I am not familiar with Conway's Game of Life, but looking at the code, it seems CGOLView's init does some adjustment based on the grid width and height. The reason I am mentioning this is that you could always change the view's frame size, however, you'd then also need to make some other mods to the tileViews for it to look proper.
As to why gridWidthValue/gridHeightValue is not working. Those are properties defined in an instance. Hence you would need to do somethign like self.gridWithValue to reference it. However, you cannot do that when defining the property such as
let _gameView = CGOLView(gridWidth:gridWidthValue, gridHeight:gridHeightValue)
This is also why instantiating _gameView in init is the way to go.
Your problem is that you cannot access the variable currentValue because it is inside of a function. You have to declare that value outside of the function to be able to use it outside of the function.
i have a Antlr generated Listener, and i call my tree walker to go through the tree from a parse function in another class. Looks like this:
public double calculate(){
ANTLRInputStream input = new ANTLRInputStream("5+2");
Lexer lexer = new Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Parser parser = new Parser(tokens);
ParseTree tree = parser.calculate();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(new Listener(), tree);
return 0;
}
So the listener works perfect with the enter() and quit() Functions and prints the correct value in the end:
public void exitParser(ParserContext ctx) {
result = stack.peek();
System.out.println(result);
}
But i wanna receive the final value in my calculate() function to return it there. Since exitParser(...) is void i dont know how to deal with it.
With the visitor i was able to do it like that:
public double calculate(){
// ...
String value = new WRBVisitor().visit(tree);
return Double.parseDouble(value);
}
Hope someone understands my problem and knows a solution for it.
Best regards
As mentioned in the comments: a visitor might be a better option in your case. A visitor's methods will always return a value, which is what you seem to be after. That could be a Double if your expressions always evaluate to a numeric value, or some sort of home-grown Value that could represent a Double, Boolean, etc.
Have a look at my demo expression evaluator (using a visitor) on GitHub: https://github.com/bkiers/Mu
I have below code which is giving null pointer exception in obtaining editText3 at line 4.
Intent intent = getIntent();
Double result = intent.getDoubleExtra(MainActivity.RESULT, 0);
setContentView(R.layout.activity_result);
EditText editText3 = (EditText)findViewById(R.id.editText3);
editText3.setText(Double.toString(result));
Can some please let me know what is the problem. I tried doing clean build but it didn't worked.
Thanks,
Rajan
You can double check if specify the wrong layout in setContentView() first.
import java.util.Scanner;
public class Program3_5
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
String input = new String();
System.out.println("Please enter a string: ");
input=scan.next();
int length;
length = input.length;
input.substring();
System.out.println(charAt(0));
while (length)
{
System.out.println(charAt(0 + 1));
}
}
}
I am getting an error stating that it "cannot find symbol - variable length"
I have tried numerous things yet I am having trouble getting it to work. New to Java! Thanks in advance.
For example if the user were to input: Hello There
The Output would print the letters on separate lines.
String#length() is a method, not a field, of String. You need to call the method. In Java, methods are called (or "invoked") using parentheses. So, change
length = input.length;
// to
length = input.length();
Anticipating the next compile error you see:
while (length)
won't compile in Java because length is an int, but the condition part of a while must be a boolean. I'm guessing you want to continue as long as the string is not empty, so change the while condition to be
while (length > 0)
Other problems you'll need to solve to get your code to compile:
String#substring() requires integer arguments
Also, the code will compile with the String input = new String(); but the assignment is completely unnecessary. In Java, you almost never need to new a string. Instead, use string literals.