Question

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 each element and return the difference

For example, input "000", differs 2 times with element "101" of the array.

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

PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING

  • As you mentioned in the comment section, i'm returning count value for each using loop in main.
  • if you want me to change/add anything, please let me know by commenting.
  • I used comments for better understanding. Comments starts with ' // '
  • Please refer to the screenshot of the code to understand the indentation of the code

Language : Java

Filename : Difference.java

IDE : Java Eclipse

:::::::::::::::::::::::::::::::::::::::::::: CODE ::::::::::::::::::::::::::::::::::::::::

public class Difference {

   // function to return array of counts of differs
   public static int Differ(String value,String input) {
       int count = 0;
       // loop for input times
       for(int j=0; j<input.length(); j++) {
           // compare characters at index j
           int compare = Character.compare(value.charAt(j), input.charAt(j));
           // if both characters at index j are not equal
           if(compare!=0) {
               // increase count by 1
               count++;
           }
       }

       // return count
       return count;
   }

   public static void main(String[] args) {
       String array [] = {"101", "010", "111"};
       String input = "000";

       // print all strings
       for(int i=0; i<array.length; i++) {
           int differs = Differ(array[i],input);
           System.out.println(array[i]+"\t"+differs);
       }
   }
}

:::::::::::::::::::::::::::::::::::::::::::: OUTPUT ::::::::::::::::::::::::::::::::::::::::

Console X = Progress <terminated> Difference (Java Application] C:\Program FilesVava\jdk-14.0.2\bin\javaw.exe 101 2 010 1 111

:::::::::::::::::::::::::::::::::::::: CODE in EDITOR ::::::::::::::::::::::::::::::::::

3 5 Difference.java X 1 public class Difference { 2 // function to return array of counts of differs 40 public static int Dif

_________________________________________________________________

Dear Friend, Feel Free to Ask Any Doubts by Commenting. ASAP i'll respond when i'm available.

I'm on a critical Situation. Please Do Not Forget To Give A Thumbs UP +1. It will Helps me A Lot.

Thank YOU :-)

Add a comment
Know the answer?
Add Answer to:
JAVA QUESTION I have an array String array [] = {"101", "010", 111"} I have an...
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 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.

  • Java Counts the occurrences of each digit in a string

    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s) The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a test program that prompts the...

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • In java, write method, shuffle, which accepts an array of int, creates a copy of the...

    In java, write method, shuffle, which accepts an array of int, creates a copy of the formal parameter, shuffles the copy, then returns the copied, shuffled array, leaving the formal parameter unchanged. Shuffling is a process of swapping each element of array with another element randomly chosen from the array. For testing the shuffle method have main call shuffle, repeatedly having it shuffle its previously returned array until the returned (shuffled) array is identical (equal) to the original array that...

  • DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an...

    DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an unknown number of cells filled with data. As before, the array will already be filled with strings, some of which contain the string "Angela". You will create a method called countItUp which returns and integer array containing the number of times each of the letters in my name occur within the strings within this array. For example, if the input array was wordFun =...

  • DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an...

    DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an unknown number of cells filled with data. As before, the array will already be filled with strings, some of which contain the string "Angela". You will create a method called countItUp which returns and integer array containing the number of times each of the letters in my name occur within the strings within this array. For example, if the input array was wordFun =...

  • Exercise #2: 10 M gals water per day 71-80 81-90 91-100 101-110 111-120 121-130 131-140 10...

    Exercise #2: 10 M gals water per day 71-80 81-90 91-100 101-110 111-120 121-130 131-140 10 M gals water per day 71-80 91-100 101-110 111-120 121-130 131-140 Create a data file named water.dat with the following data: 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111. Each number represents the number of millions of gallons of water provided to...

  • JAVA How do I write a code that receive a string, detect the length of each...

    JAVA How do I write a code that receive a string, detect the length of each token with array, save the frequency of the length and print it? If the token length is longer than 10, the length is considered to be 0. If the frequency of the lengtg is 0 the information will not be printed ex) If the input is "Count the length of the token tokenlongerthanten" the output will be Length0 : 1 Length2 : 1 Length3...

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