Can not figure out Javas strings? -
i student @ moment still learning. picked vb pretty quick , simple java on other hand pretty confused on.
the assignment have been given time has me confused "write method determine number of positions 2 strings differ by. example,"peace" , "piece" differ in 2 positions. method declared int compare(string word1, string word2); if strings identical, method returns 0. returns -1 if 2 strings have different lengths."
additional "write main method test method. main method should tell how many, positions strings differ, or identical, or if different lengths, state lengths. strings console. far @ , looking break down in dumdum terms if can don't need solution understanding it.
package arraysandstrings; import java.util.scanner; public class differstrings { public static void main (string agrs[]){ scanner scanner = new scanner (system.in); system.out.print("enter word"); string word1; string word2; word1 = scanner.next(); system.out.print("enter word"); word2 = scanner.next(); int count = 0; int length = word1.length(); for(int x = 0; x >= length; x = x+1) { if (word1.charat(x) == word2.charat(x)) { count = count + 1; system.out.print (count); } } } }
additional question
package arraysandstrings; import java.util.scanner; public class differstrings { public static void main (string agrs[]){ scanner scanner = new scanner (system.in); system.out.println("enter word"); string word1 = scanner.next(); system.out.println("enter word"); string word2 = scanner.next(); int count = 0; int word1length = word1.length(); int word2length = word2.length(); if (word1length != word2length) { system.out.println ("words diffrent length"); system.out.println (word1 + "has" + word1.length() + " chars"); system.out.println (word2 + "has" + word2.length() + " chars"); } for(int x = 0; x < word1length; x = x+1) { if (word1.charat(x) != word2.charat(x)) { count = count + 1; }}} system.out.println (count+" different chars");
}
after implementing knowledge iv gained responses have ran in problem last line:
system.out.println (count+" different chars");
it says error expected worked before added next part of assignment this:
if (word1length != word2length) { system.out.println ("words diffrent length"); system.out.println (word1 + "has" + word1.length() + " chars"); system.out.println (word2 + "has" + word2.length() + " chars"); }
for(int x = 0; x >= length; x = x+1) {
you mean
for(int x = 0; x < length; x = x+1) {
Comments
Post a Comment