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!

3
  • Total should equal 4. Commented Apr 2, 2017 at 21:39
  • 2
    Possible duplicate of Division of integers in Java Commented Apr 2, 2017 at 21:42
  • 1
    The total works but I'm having trouble with the division part. Commented Apr 2, 2017 at 22:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

(The else's aren't strictly needed, since the conditions are mutually exclusive.)
If you are using Java 7, use switch case. It has better performance.
I am currently using IntelliJ Idea with Java8, but will definitely use switch case if I some how change to 7, thanks.
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

5 Comments

Why does multiplying the variable by 1.0 work? When I multiply it by 1 I get a totally different result.
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 :)
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.
I have upvoted the answer but I need 15 reputation for it to be visible! Sorry about that
That's no problem Glace. I appreciate that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.