Question

In Java. I have a character array that I need to look into and see if...

In Java.

I have a character array that I need to look into and see if it contains certain characters and print something,
and if it doesn't contain those characters I need to print something.

Example:

char[] array = {w, ' ', K, Q, k, q, ' ', -, 0, ' ', 1};
if the array has 'K' print White
if the array has 'Q' print white
if the array has 'k' print Black
if the array has 'q' print black
if the array doesn't have K Q k or q, print none/

thank you in advance.

Homework Answers

Answer #1
public class CharArrayCheck {

    public static void main(String[] args) {
        char[] array = {'w', ' ', 'K', 'Q', 'k', 'q', ' ', '-', '0', ' ', '1'};
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == 'K') {
                System.out.println("White");
                ++count;
            } else if (array[i] == 'Q') {
                System.out.println("white");
                ++count;
            } else if (array[i] == 'k') {
                System.out.println("Black");
                ++count;
            } else if (array[i] == 'q') {
                System.out.println("black");
                ++count;
            }
        }
        if (count == 0) {
            System.out.println("none");
        }
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods...
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods Create a folder called Unit03 and put all your source files in this folder. Write a program named Unit03Prog1.java. This program will contain a main() method and a method called printChars() that has the following header: public static void printChars(char c1, char c2) The printChars() method will print out on the console all the characters between c1 and c2 inclusive. It will print 10...
Write a function called char_counter that counts the number of a certain character in a text...
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 <...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 < input.txt * * Reads in a text file and for each line verifies whether the word has * unique characters. * * % cat input.txt * Hello * World * * % java Assignment1 < input.txt * False * True * ******************************************************************************/ import java.util.*; public class SortInput { /* a function for checking uniqueness of characters in a word */ private static boolean isUniqueChar(String...
Hi, I need this program written in Java for my intro CS class. Thank you in...
Hi, I need this program written in Java for my intro CS class. Thank you in advance. (also here is the description given in class for the code) Write a program that displays all the leap years, ten per line, from 101 to 2100, separated by exactly one space. Also display the number of leap years in this period. - You should use the method isLeap which returns "true" if the year is a leap year, otherwise it should return...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
I am having some trouble writing some java code involving strings. I have included the code...
I am having some trouble writing some java code involving strings. I have included the code provided by my professor below. that has the instructions in it as well public class StringExercise { public static void main(String[] args) { String given = "The quick brown fox jumped over the moon!"; String given2 = "mary"; String given3 = "D"; //Write a Java code snippet to check (print a boolean) whether the given String ends with the contents of "oon!", see endsWith...
I cannot see it, I need a different pair of eyes. The tangents of the equation...
I cannot see it, I need a different pair of eyes. The tangents of the equation 4x^2+y^2=72 pass through (4,4) have an equation that look like this: y-k=-4h/k(x-h) since we know (4, 4) is on the tangent, we have 4-k=-4h/k(4-h). How on earth do we solve for (h, k)? My professor solved for h=21/5 and k=6/5 and I am trying to review and hitting a wall.
I tried using the modulo to skip the chars but I doesnt work in some cases....
I tried using the modulo to skip the chars but I doesnt work in some cases. There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include #include // print...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT