Google Dart Error - dart

import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random(); //create a random number generator
int answer = rand.nextInt(100); //gets a random integer from 0 to 99 do {
print("Enter your guess:");
String temp = stdin.readLineSync(); //read in from the keyboard guess = int.parse(temp); //convert String to integer
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
}
while (guess != answer);
print("You got it!");
}
What is wrong?? I deleted the } in
} while (guess != answer);
print("You got it!");
and now in the console it says
Enter your guess:
32
Breaking on exception: object of type NoSuchMethodError

Your guess variable is always null as it's never assigned (it's commented out), try changing your code to:
import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random(); //create a random number generator
int answer = rand.nextInt(100); //gets a random integer from 0 to 99
do {
print("Enter your guess:");
String temp = stdin.readLineSync(); //read in from the keyboard
guess = int.parse(temp); //convert String to integer <-- the assignment is what's missing here
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
} while (guess != answer);
print("You got it!");
}
There is a related issue with the readLineSync here which you might want to have a look.

I guess it is because guess is null and null has no >/< operator.
guess is null because you never assign a value.
If guess and answer are not equal this makes a perfect endless loop:
while (guess != answer);
print("You got it!");
}

Wow I failed. Man im tired seems I was typing way to sloppy/fast and left some of my code in the comments :( found it right after seeing this post sigh.. this is the correct working code
import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random(); //create a random number generator
int answer = rand.nextInt(100); //gets a random integer from 0 to 99
do {
print("Enter your guess:");
String temp = stdin.readLineSync(); //read in from the keyboard
guess = int.parse(temp); //convert String to integer
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
}
while (guess != answer);
print("You got it!");
}
Thats enough learning of Dart for today. Thanks everyone for the quick replies seems the Dart community is active!
im off to bed! have a great day

Related

How to emulate scanf in dart?

How to emulate scanf in dart?
I want to translate the following C code into dart.
#include <stdio.h>
void main() {
double a,b;
printf("a b? ");
scanf("%lf%lf",&a,&b);
printf("a=%lf b=%lf\n",a,b);
}
As I know, I cannot use call by reference, variable number arguments function call or destructuring assignment in dart.
So, it seems that it is impossible to make a function emulating scanf for now.
Here is my version in dart.
import "dart:io";
void main() {
stdout.write("a b? ");
var line = stdin.readLineSync();
var tokens = line?.split(RegExp(r'\s+'));
double a = double.tryParse(tokens?[0] ?? '0') ?? 0;
double b = double.tryParse(tokens?[1] ?? '0') ?? 0;
print("a=$a b=$b");
}
In there any possible improvement in the code?
Here is another version using Iterator.
import 'dart:io';
void main() {
stdout.write("a b? ");
var scan = Scan();
var a = scan.getDouble();
var b = scan.getInt();
print("a=$a b=$b");
}
class Scan {
Iterator? it;
Scan([String? line]) {
it = (line ?? stdin.readLineSync())?.split(RegExp(r'\s+')).iterator;
}
double getDouble() {
return double.tryParse(it?.moveNext() == true ? it?.current : '') ?? 0;
}
int getInt() {
return int.tryParse(it?.moveNext() == true ? it?.current : '') ?? 0;
}
}

I made a little game, but something get wrong

import 'dart:io';
import 'dart:math';
void main() {
String? name;
int numberWish = 0;
int myNumber = 90;
Random random = Random();
print("Hi! Let's play a game! \n What is your name?");
name = stdin.readLineSync();
print("So, $name, write below the number, And If it will be the same number, that I wish, you are the winner and hve the chance to choose gift!");
numberWish = int.parse(stdin.readLineSync()!);
for (var index = 0; index < numberWish; index++) {
myNumber = random.nextInt(numberWish);
print(myNumber);
}
if (myNumber == numberWish) {
print("You are the winner");
} else {
print("Sorry :(");
}
}
It is simple game, where I write the number and if it is the same with randomnumber, I win.
So I run it several times, but I can not win. Random specially choose numbers, which is different from my wished number. I think it is my fault :D. How can I fix it?

How to return two value from a function in dart?

here is my code
import 'dart:io';
import 'dart:math';
void main() {
bool flag = false;
for (int i = 0; i < 100; i++) {
gameCode();
if (userNumber == computerNumber) {
flag = true;
break;
}
}
}
int randomNumber(number) {
Random randNumber = Random();
int random = randNumber.nextInt(number);
return random;
}
gameCode() {
int computerNumber = randomNumber(9);
print("start guessing the number : ");
int userNumber = int.parse(stdin.readLineSync()!);
if (userNumber == computerNumber) {
print("You got it");
}
}
in this code you can see gameCode function. in that function there is two value that i need to use in main function.so how do i return those two keyword from that function ?
//userNumber // computerNumber
this is the variable that i want to return from that code
Dart not support return multiple values in function, you can return it with array, map, or you can use third lib tuple
Dart does not support returning multiple values in the current latest version. I would in your case recommend creating a class for the specific purpose of define the result from gameCode(). So something like this:
class GameCodeResult {
int userNumber;
int computerNumber;
GameCodeResult({
required this.userNumber,
required this.computerNumber,
});
}
Which we can then use like this in your program:
import 'dart:io';
import 'dart:math';
void main() {
bool flag = false;
for (int i = 0; i < 100; i++) {
GameCodeResult result = gameCode();
if (result.userNumber == result.computerNumber) {
flag = true;
break;
}
}
}
final _random = Random();
int randomNumber(int maxNumber) => _random.nextInt(maxNumber);
GameCodeResult gameCode() {
int computerNumber = randomNumber(9);
print("start guessing the number : ");
int userNumber = int.parse(stdin.readLineSync()!);
if (userNumber == computerNumber) {
print("You got it");
}
return GameCodeResult(userNumber: userNumber, computerNumber: computerNumber);
}
Note, I also fixed your randomNumber() method since it is not recommended to generate a new Random() object for each new random number you want. We should instead reuse an instance of Random in our program.
Please refer to below code
import 'dart:io';
import 'dart:math';
void main() {
bool flag = false;
for (int i = 0; i < 100; i++) {
Map<String, dynamic> res = gameCode();
print(res);
if (res['user_number'] == res['computer_number']) {
flag = true;
break;
}
}
}
int randomNumber(number) {
Random randNumber = Random();
int random = randNumber.nextInt(number);
return random;
}
Map<String, dynamic> gameCode() {
int computerNumber = randomNumber(9);
print("start guessing the number : ");
int userNumber =
int.parse(stdin.readLineSync()!);
if (userNumber == computerNumber) {
print("You got it");
}
return {
"computer_number": computerNumber,
"user_number": userNumber,
};
}

how to solve this error in dart language: NoSuchMethodError : method not found: 'readLineSync'

im new to dart language and when i want to take user input for a command line simple number guessing game using "stdin.readLineSync()", i get this error .
my code is:
import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random(); //create a random number generator
int answer = rand.nextInt(100); //gets a random integer from 0 to 99
do {
print("Enter your guess:");
String temp = stdin.readLineSync(); //read in from the keyboard
guess = int.parse(temp); //convert String to integer
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
} while (guess != answer);
print("You got it!");
}
the error is for this line: String temp = stdin.readLineSync(); //read in from the keyboard
and i get this notification:
Unhandled exception:
NoSuchMethodError : method not found: 'readLineSync'
Receiver: Instance of '_SocketInputStream#0x1da10ec4'
Arguments: []
#0 Object._noSuchMethod (dart:core-patch:1360:3)
#1 Object.noSuchMethod (dart:core-patch:1361:25)
That is a extremely old version of Dart you are using since Dart Editor has been removed a long time ago. Please use latest version of Dart (2.10.3) and use either IntelliJ (dart.dev/tools/jetbrains-plugin) or VS Code (dart.dev/tools/vs-code).
thanks to #julemand101.

The requested built-in library is not available on Dartium - library handler failed import 'dart:io';

I am beginner in Dart. I have build a simple application in Dart.
import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random();
int answer = rand.nextInt(100);
do{
print("Enter your guess number: ");
String temp = stdin.readLineSync();
guess = int.parse(temp);
if (guess < answer){
print("Too low");
}else if (guess > answer){
print("Too high");
}
}while(guess != answer);
print("Got it!");
}
But, when i run it on Dartium. It has an error.
What can I do to solve this? Thanks.
dart:io is for console/server applications. If you want to run the code in the browser you must not import it. Maybe dart:html is what you want instead. It exposes the browser API.

Resources