2

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!

Glace
  • 127
  • 1
  • 3
  • 10

2 Answers2

6

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 ifs in the for loop would work too as the conditions are mutually exclusive.

Leon
  • 2,926
  • 1
  • 25
  • 34
1

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 ith 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);
    }
}

enter image description here

Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
  • 1
    Why does multiplying the variable by 1.0 work? When I multiply it by 1 I get a totally different result. – Glace Apr 02 '17 at 22:08
  • 1
    Multiplying by 1.0 which is of type double automatically converts the expression to type double. So 5/2 = 2 if not double. However, 5.0/2 = 2.5 since it is now a double :) – Devendra Lattu Apr 02 '17 at 22:09
  • 1
    Ah that's cool! So when I multiply it by 1 the compiler is actually rounding it to the nearest whole number because it's an integer. I reckon adding (double) would work too as stated by Leon. – Glace Apr 02 '17 at 22:11
  • 1
    I have upvoted the answer but I need 15 reputation for it to be visible! Sorry about that – Glace Apr 02 '17 at 22:11
  • That's no problem Glace. I appreciate that. – Devendra Lattu Apr 02 '17 at 22:12