Problem with getting user input , How do i solve this problem? - dart

import 'dart:io';
void main (List<String> args){
print('Enter name');
String? name = stdin.readLineSync();
print (name);
print ("Enter first digit");
int?num1 = int.parse(stdin.readLinesync()!);
print ("Enter second digit");
int num2? = int. parse(stdin.readLineSync()!);
int result = num1 + num2;
print(result);
}
There is absolutely nothing wrong with the code. I'm sure of it.
Getting user input in the debug console in visual studio code is a bit cumbersome. I'm input a integer alright. But when I want to print. It doesn't work quite accurately.
f
For instance when input an integer in the console and i want to print "the number you entered is(the integer that was imputed in the console), on the integer displays but the string doesn't display.
I'm also finding it difficult perform any arithmetic calculations with the integers that i input in the debug console.
Also when I specify that the user input should be an integer, I'm able to input an integer and a string as well. This shouldn't be

Related

How to read an integer input from the user input in dart [duplicate]

This question already has answers here:
"The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()
(3 answers)
Closed 1 year ago.
I actually don't know how to take a integer input from the input console. Then I tried this after a little research.
My Code:
import 'dart:io';
void main() {
stdout.write("Enter a number: ");
int num1 = int.parse(stdin.readLineSync());
print(num1);
}
But it doesn't work, showing an error message,
ERROR: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
Then finally I came to know that in dart2.12+ versions dart introduced null safety. Any suggestion to do it properly in the null safety environment.
The readLineSync() method returns a String?. The ? symbol indicates this variable may be null.
On the other hand, the int.parse() method expects a String, without the ? symbol. This means it doesn't know how to handle if the result from the readLine method comes null.
The easiest way to solve this is to give a default value in case the result comes null:
int num1 = int.parse(stdin.readLineSync() ?? '0');
The ?? operator makes the expression evaluates to the right side, if the left side is null. So giving it a default value it won't have to bother with a nullable type.
There are other operators you can use. You can read more about it in the Dart documentation about it.
Try this
import 'dart:io';
void main()
{
// Asking for favourite number
print("Enter your favourite number:");
// Scanning number
int n = int.parse(stdin.readLineSync());
// Printing that number
print("Your favourite number is $n");
}
If this not work I think you should try this answer link here

not able to complete the program [duplicate]

This question already has answers here:
"The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()
(3 answers)
Closed 1 year ago.
I'm new to dart and having a hard time figuring out things. so, I just need some help completing the program below. I have no idea where I'm going wrong.
All I'm getting is an error related to null safety
question :-
Write a program to obtain a number N and increment its value by 1 if the number is divisible by 4 otherwise decrement its value by 1.
import 'dart:io';
void main(){
String? input = stdin.readLineSync();
int number = int.parse(input);
}
This is all that came to my mind, I know the logic, but I'm stuck at getting the user input and converting it.
As the comment suggest, in this answer is explained how Dart will handle if stdin.readLineSync does give a null value. So this should work, notice the ! at the end of stdin.readLineSync.
import 'dart:io';
void main() {
var input = stdin.readLineSync()!;
var number = int.parse(input);
}

How to identify text within an object's string

I want to find specified text within a string in a given column, and count how many times that string is repeated throughout the entire column.
For example, Find "XX" within a string in a column and print to dialogue box the number of times that text was found.
Module m = current
Object o
string s
string x
int offset = null
int len = null
int c
for o in m do
{
string s = probeAttr_(o, "AttributeA")
x = o."Object Text" ""
if(findPlainText(s, "XX", offset, len, false)){
print "Success "
} else {
print "Failed to match"
}
}
I have tried to use command findPlainText but I am inadvertently passing every object as true.
As well I placed the output to print 'success' or 'Failed to match' so I can at least get a number count of what is being passed. Unfortunately it seems like everything is being passed!
My understanding is that 'probeAttr_(o, "AttributeA")' allows me to specify and enter what column to search. As well o."Object Text" "" now allows me to look within any object and search for any text contained. I also realize that variable x is not being used but assume it has some way of being used to solve this issue.
I only use DOORS at a surface level but having this ability will save other staff tons of time. I realize this may be accomplished using the DOORS advanced filtering capability but I'd be able to compound this code with other simple commands to save time.
Thank you in advance for your help!!
If you want to count every occurence of a specified string in a text in an attribute for all objects, I think Mike's proposal is the correct answer. If you are only interested, if the specified string occurs once in that object's attribute, I suggest using Regexp, as I find it very fast, quite powerful and nevertheless easy to use, e.g.:
Regexp reSearch = regexp2 "XX"
int iCounter = 0
string strOT = ""
for o in m do {
strOT = o."Object Text" ""
if (reSearch strOT) {
iCounter++
}
}
print "Counted: '" iCounter "'\n"
Most of this has been answered in (DXL/Doors) How to find and count a text in a string?
You can easily exchange the "print" with a counter.

Int Variable won't let me add data from UI text field

I'm currently working on making a savings app in Xcode 10. I'm working on a feature that lets users add the amount of money they have saved for something into the app through a UI text field. I can't find a way to return the text from the text field to an Integer and add that to the total sum of money that has been saved. Also whenever I tried to add a test variable I got plenty of errors.
var amountSavedSoFar += amountOfMoneySaved
Both I have set to be integers. I'm trying to set amountOfMoneySaved equal to the numbers in the text field, but it doesn't seem to work.
'+=' is not a prefix unary operator
Consecutive statements on a line must be separated by ';'
Type annotation missing in pattern
Unary operator cannot be separated from its operand
You've got a few issues as you mentioned:
amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.
amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)
amountSavedSoFar isn't typed or initialized.
Try something like:
var amountSavedSoFar: Int = 0
#IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}

Can anyone please provide hive udf code for truncating particular column?

I have a column that contain double value 42.2223. I want to truncate the last four digits of this particular column.
Can any one please provide hive UDF for this particular scenario?
If you want to truncate the last four digits and get an integer, you can use the built-in functions floor(double a) or ceiling(double a), depending on the the kind of rounding (upper or lower) that you want.
If you wanted your double to be truncated to d decimal places (and get a double, instead of an integer), you could use round(double a, int d).
EDITED
In order to round without truncating, you can use this
CAST((column * 100) AS int)/100
Wrote an UDF for the above question where we can specify number of characters it can be truncated
package com.hive.udf.truncate;
import java.math.BigDecimal;
import org.apache.hadoop.hive.ql.exec.UDF;
public class Trunc extends UDF {
public double evaluate(double input,int numberOfDecimals){
if ( input > 0) {
return new BigDecimal(String.valueOf(input)).setScale(numberOfDecimals, BigDecimal.ROUND_FLOOR).doubleValue();
}
else {
return new BigDecimal(String.valueOf(input)).setScale(numberOfDecimals, BigDecimal.ROUND_CEILING).doubleValue();
}
}
}

Resources