Dart find the capital word in a given String - flutter

How can I write a code that finds capital words in a given string in dart
for example
String name ='deryaKimlonLeo'
//output='KL'

Hmmm try this using pattern if you only want to get only the big letter then try this
String name ='deryaKimlonLeo';
print(name.replaceAll(RegExp(r'[a-z]'),""));
//Output you wanted will be
// KL
try this on dart pad it works

A basic way of doing this is like
void main() {
String name = 'deryaKimlonLeo';
String result = '';
for (int i = 0; i < name.length; i++) {
if (name.codeUnitAt(i) <= 90 && name.codeUnitAt(i) >= 65) {
result += name[i];
}
}
print(result);
}
More about ASCII and codeUnitAt.

sample code:
void main() {
String test = "SDFSDdsfdDFDS";
for (int i = 0; i < test.length; i++) {
if (test[i].compareTo('A') >= 0 && test[i].compareTo('Z') <= 0)
print(test[i]);
}
}

Related

Dart Fails to save Bytes to PNG, JPEG

I have been trying for hours to figure out why my code is not working. Basically, I have an image. I load its bytes into dart as a list of Uint8List. Then, I replace the values of the list with some other values. The problem is that after replacing the values, when I call the File().writeAsBytes() function, the image is CORRUPTED. Don't know why this is happening. Tried doing everything I could.
var b = File("assets/1K91k (1).jpg").readAsBytesSync();
void main() {
runApp(const MyApp());
for (int i = 0; i < b.length; i++) {
double check = b[i] / 255;
if (check > 0.8) {
b[i] = 255;
} else {
b[i] = 2;
}
}
File("/home/kq1231/Desktop/test.jpg")
..createSync()
..writeAsBytesSync(b);
}
I tried converting the b list to a Uint8List but to no avail.
Feels funny to answer my own question but here's how it got working:
import 'package:image/image.dart' as image;
import 'dart:io';
image.Image prettify(String fileName, String exportPath, String imageName,
double threshold, int blurRadius) {
var b = image.decodeImage(File(fileName).readAsBytesSync());
b = image.gaussianBlur(b!, blurRadius);
for (int i = 0; i < b.width; i++) {
for (int j = 0; j < b.height; j++) {
var pix = b.getPixel(i, j);
if ((image.getBlue(pix) + image.getRed(pix) + image.getGreen(pix)) /
(255 * 3) >
threshold) {
b.setPixel(i, j, 0xffffff);
} else {
b.setPixel(i, j, 0);
}
}
}
File("$exportPath/$imageName")
..createSync()
..writeAsBytesSync(image.encodePng(b));
return b;
}
This is a function called prettify. It applies a specific operation to a given image. First, decode the image. Then, loop through each pixel, average the R, G and B values to get the grayscale value (get the value of the pixel using image.getPixel() and set its value using image.setPixel()). Then, encode it back to .png format and save it.
Note that image is the name of the library imported.

multiplication Table in dart

[1][I write a simple program in dart to print multiplication table but the output was not I Except][1]
void main{
int num=10;
for(var i=1;i<=10;++i){
print('$num*$i=$num');
}
}
this was my code
Finally I found the answer
var num = 10;
for (var i = 1; i < 10; i++) {
print("$num * $i = ${num * i}");
}
any other ways to print multiplication table in dart using for loop
You forget to add the parenthesis in the main function which acted like a function declaration.
And you also missed to multiply the result of the multiplication by i.
The correct code is :
void main(){
int num=10;
for(var i=1;i<=10;++i){
print('$num*$i=${num*i}');
}
}
instead of this:
void main{
int num=10;
for(var i=1;i<=10;++i){
print('$num*$i=$num');
}
}
void main()
{
int num =20;
for(int i=1;i<=10;i++)
{
print("num* $i = ${num*2}");
}
}

Attempts to call a method in the same class not working (java)

I'm creating a random number generator which then sorts the digits from largest to smallest. Initially it worked but then I changed a few things. As far as I'm aware I undid all the changes (ctrl + z) but now I have errors at the points where i try to call the methods. This is probably a very amateur problem but I haven't found an answer. The error i'm met with is "method in class cannot be applied to given types"
Here's my code:
public class RandomMath {
public static void main(String[] args) {
String bigger = bigger(); /*ERROR HERE*/
System.out.println(bigger);
}
//create method for generating random numbers
public static int generator(int n){
Random randomGen = new Random();
//set max int to 10000 as generator works between 0 and n-1
for(int i=0; i<1; i++){
n = randomGen.nextInt(10000);
// exclude 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 0000
if((n==1111 || n==2222 || n==3333 || n ==4444 || n==5555)
||(n==6666 || n==7777 || n==8888 || n==9999 || n==0000)){
i--;
}
}
return n;
}
//create method for denoting the bigger number
public static String bigger(int generated){
generated = generator(); /*ERROR HERE*/
System.out.println(generated);
int[] times = new int[10];
while (generated != 0) {
int val = generated % 10;
times[val]++;
generated /= 10;
}
String bigger = "";
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < times[i]; j++) {
bigger += i;
}
}
return bigger;
}
}
You have not defined a method bigger(), only bigger(int generated). Therefore, you must call your bigger method with an integer parameter.

issue in my if statement to make comparison in my java program

any help please, so i already wrote the prog but my if statement in my for loop is not working. the prog need to generate 6 random nos,then apply bubble sort which i already did.then the user must enter 6 numbers and these numbers must be compared against the random numbers and must say whether numbers are found in the random numbers or not. here's the code. something is wrong with the if statement ` public static void main(String[] args) {
try {
int numbers[] = new int[6]; //random numbers will be stored in new array
//2 loop will be created to avoid duplication of numbers
System.out.println("Array before Bubble sort");
for (int i = 0; i < 6; i++) {
numbers[i] = (int) (Math.random() * 40);
if (i > 0) {
for (int b = 0; b < i; b++) { //
if (numbers[b] == numbers[i]) {
i--; //decrement to continue the for loop if the integer has been repeated
}
}
}
System.out.print(numbers[i] + ","); //random numbers will be printed before using sorting bubble sort
}
//sort an array using bubble sort
bubbleSort(numbers);
System.out.println(" \nArray after bubble sort");
for (int i = 0; i < 6; i++) {
System.out.print(numbers[i] + ",");
}
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\ninput 6 number between 1 and 40");
int inputNumber = Integer.parseInt(input.readLine());
for (int b = 0; b < 6; b++) {
System.out.println("number:");
int outcome=Integer.parseInt(input.readLine());
if(outcome==numbers){
System.out.println("found in random numbers");
}else{
System.out.println("not found in random numbers");
}
}
} catch (Exception e) {
System.out.println("error");
}
}
public static void bubbleSort(int[] numbers) {
int n = numbers.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (numbers[j - 1] > numbers[j]) { //swap the element
temp = numbers[j - 1];
numbers[j - 1] = numbers[j];
numbers[j] = temp;
}
}
}
}
}`
System.out.println("\ninput 6 number between 1 and 40");
//Scanner is specifically designed for getting an input purpose and introduced in Java 5,so better use it
Scanner s = new Scanner(System.in);
//you need to have nested loop here
//But the best way to search is use binary search,as you have already sorted the array
while (s.hasNextInt()) {
//one at a time from the input is put to outcome
int outcome = s.nextInt();
boolean found = false;
for (int b = 0; b < 6; b++) {
found = false;
if (outcome == numbers[b]) {
found = true;
//remember to break the inner loop if a match is found
break;
} else {
found = false;
}
}
if (found == true) {
System.out.println("found in random numbers");
} else {
System.out.println("not found in random numbers");
}

check palindrome in java code

I'm making a really simple app in java code, but for some reason it doesnt work. its a palindrome checker.
here is the code.
MAIN:
public class main {
public static void main(String[] args) {
Palindroom.palindroomChecker("RACECAR");
}
}
`Palindroom class:
public class Palindroom {
public static void palindroomChecker(String input) {
String omgekeerd = "";
boolean isPalindroom = false;
int length = input.length();
for(int i = 0; i < length; i++ ) {
String hulp = "" + input.charAt(i);
omgekeerd = omgekeerd + hulp;
}
System.out.println(omgekeerd);
System.out.println(input);
if(omgekeerd.equals(input)){
System.out.println("DIT IS EEN PALINDROOM!");
}
else {
System.out.println("HELAAS, DIT IS GEEN PALINDROOM!");
}
}
}`
For some reason the check in the if-statement doesnt go as it has to go. As you can see i checked omgekeerd and input and i also checked earlier the length of omgekeerd to see if there were clear spaces.
Can someone help me out!
thanks in advance
greetings Mauro Palsgraaf
Your logic is flawed. You're reconstructing a new string by appending every char of the input, in the same order, and then check that both strings are equal. So your method always says that the input is a palindrome.
You should construct a new string by appending the chars in the reverse order.
Or you could make it faster by checking that the nth character is the same as the character at the length - 1 - n position, for each n between 0 and length / 2.
You are not actually reversing the string, looks like omgekeerd will be in the same order as input.
Replace for with for(int i = length-1; i >= 0; i--) {
This can be simplified a lot
boolean isPalindrome = new StringBuilder(input).reverse().equals(input);
Maybe this would work for you?
String str = "madam i'm adam."; // String to compare
str = str.replaceAll("[//\\s,',,,.]",""); // Remove special characters
int len = str.length();
boolean isSame = false;
for(int i =0; i<len;i++){
if(str.charAt(i) == str.charAt(len-1-i)){
isSame = true;
}
else{
isSame = false;
break;
}
}
if(isSame){
System.out.print("Equal");
}
else{
System.out.print("Not equal");
}
i=0;
j=str.length()-1; //length of given string
String str; // your input string
while((i<j)||(i!=j)){
if(str.charAt(i)!=str.charAt(j)){
System.out.println("not palindrome");
break;
}
i++;
j--;
}
System.out.print("palindrome");
//this can used for checking without the need of generating and storing a reverse string