Question

JAVA CODING QUESTION You have string A = "00011" and String B = "00000" Code a...

JAVA CODING QUESTION

You have string A = "00011" and String B = "00000"

Code a for loop in java to count how many times there is a difference in a character when comparing the two strings

Looking at them we know they differ twice.

CODE A FOR LOOP THAT RETURNS HOW MANY TIMES THOSE TWO STRINGS DIFFER.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code works will work for strings whose lengths are the same. Here is the output

The number of characters by which the two strings differ are 2

And here is the java code

public class Main
{
        public static void main(String[] args) {
            String A = new String("00011");
            String B = new String("00000");
            int difference=0;
            
            for(int i=0;i<5;i++)
            {
                if((int)A.charAt(i)!=(int)B.charAt(i))
                {
                    difference++;
                }
            }
                System.out.println("The number of characters by which the two strings differ are " + difference);
        }
}

If you liked the solution, please give a positive rating to the answer. Your rating motivates experts to help other students also. Thank you :)

Add a comment
Know the answer?
Add Answer to:
JAVA CODING QUESTION You have string A = "00011" and String B = "00000" Code a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA QUESTION I have an array String array [] = {"101", "010", 111"} I have an...

    JAVA QUESTION I have an array String array [] = {"101", "010", 111"} I have an input of "000" that I want to compare to each value in the array and count how many times they Differ. So String input = "000" CODE A FUNCTION THAT ACCEPTS THE ARRAY AND THE INPUT VARIABLE AS PARAMETERS. IN THIS FUNCTION, CODE A FOR LOOP THAT COMPARES THE String input to each element of the array ,COUNTS how many time they differ for...

  • PLEASE SOLVE THESE QUESTIONS. USE JAVA FOR CODING. Q.5.8 Write short Java code to display the...

    PLEASE SOLVE THESE QUESTIONS. USE JAVA FOR CODING. Q.5.8 Write short Java code to display the following screen (no need to do it practically using a compiler, just basic coding to display the screen): Sample screenshot Input х ? Enter (1) to display the vowel count. Enter (2) to display the non vowel count. Enter (0) to exit. OK Cancel Q.5.9 Write a short Java code to display the following screen (no need to do it practically using a compiler,...

  • Python 3 Coding Functions: Here is any code required to help code what is below: def...

    Python 3 Coding Functions: Here is any code required to help code what is below: def pokemon_by_types(db, types): new_db = {} for pokemon_type in types: for key in db: # iterate through all the type in types list if db[key][1] == pokemon_type or db[key][2] == pokemon_type: if key not in new_db: new_db[key] = db[key] return new_db I need help coding the functions listed below in the image: Thank you get types(db): Given a database db, this function determines all the...

  • In Java As in previous labs, you can enter all the necessary Java code fragments into...

    In Java As in previous labs, you can enter all the necessary Java code fragments into the DrJava Interactions pane. This will introduce you to reading input from a file and writing data to a file. As a warmup, we are going to read input from a String. Look up the StringReader class in the API. Create a StringReader object using the string "EECS 132". Then, use the read method to read each character, one at a time, from the...

  • Write code in Java. In this step, functions and methods are synonymous. You may assume that...

    Write code in Java. In this step, functions and methods are synonymous. You may assume that strings only contain uppercase letters A through Z. 1. Write a function as follows: o Receive a string as a parameter. o Count the length of the string Count the occurrences of each distinguishable symbol in the string (use an array or map/dictionary). o Divide the factorial of the length by the product of the factorials of the occurrences. o Return the quotient as...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string...

    Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string +Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor +setName(string name) : void +setPhone(string pn) : void +setEmail(string em) : void +getName() : string +getPhone() : string +getEmail() : string +printContact() : void         //print out the name, phone numbers and email address Create New Project Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT