简体   繁体   中英

Check if string contains a character with for loop?

I am currently working on a simple code that will check if an user inputted String contains character(s) that are specified in the for loop.

My current code

import java.util.Scanner;
public class AutumnLeaves {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int G = 0;
    int R = 0;
    int Y = 0;
    int B = 0;
    String S = sc.nextLine();
    for (int i = 0; i < S.length(); i++) {
        if (S.contains("G")) {
            G++;
        } else {
            if (S.contains("R")) {
                R++;
            } else {
                if (S.contains("Y")) {
                    Y++;
                } else {
                    if (S.contains("B")) {
                        B++;
                    }
                }
            }
        }
    }
    int total = G + R + Y + B;
    System.out.println(G/total);
    System.out.println(R/total);
    System.out.println(Y/total);
    System.out.println(B/total);
}

}

As you can see, it checks if the string contains such characters and it will increase the counter of the character by one. However when I run it, I don't receive the results I predicted. If I input GGRY, it outputs 1 0 0 0. When the desired out put is

0.5 0.25 0.25 0.0

Any help would be appreciated!

The problem is that S.contains returns true if the whole string contains the given character. S.charAt should solve your problem:

for (int i = 0; i < S.length(); i++) {
    if (S.charAt(i) == 'G') G++;
    else if (S.charAt(i) == 'R') R++;
    else if (S.charAt(i) == 'Y') Y++;
    else if (S.charAt(i) == 'B') B++;
}

Also, dividing integers will return an integer (rounded down). As such your output would always be 0 unless all the characters are the same. Just cast them to double before printing:

System.out.println((double) G/total);
System.out.println((double) R/total);
System.out.println((double) Y/total);
System.out.println((double) B/total);

Edit: As pointed out by Sumit Gulati in a comment, a switch statement will have better performance in Java 7. Also, as David Conrad pointed out using only if s in the for loop would work too as the conditions are mutually exclusive.

Your earlier code S.contains("some character") was finding the index of the character in the entire string. Use S.charAt(i) to specifically find the index at i th location in the string. Finally, you need to convert the integer to floating point in order to print output as floating values.

public class AutumnLeaves {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int G = 0;
        int R = 0;
        int Y = 0;
        int B = 0;
        String S = sc.nextLine();
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) == 'G') {
                G++;
            } else {
                if (S.charAt(i) == 'R') {
                    R++;
                } else {
                    if (S.charAt(i) == 'Y') {
                        Y++;
                    } else {
                        if (S.charAt(i) == 'B') {
                            B++;
                        }
                    }
                }
            }
        }
        int total = G + R + Y + B;
        System.out.println(G * 1.0 / total);
        System.out.println(R * 1.0 / total);
        System.out.println(Y * 1.0 / total);
        System.out.println(B * 1.0 / total);
    }
}

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM