Variable is never used..

Answered

i can copy & paste this code into IJ 2018.. or 19 EAP and it builds and runs with error , however if I try to write the same code.. 'variable is never used. will popup.. will not run the code code  correctly 

 

public class GFG {
// Java program to Compare two strings
// lexicographically

// This method compares two strings
// lexicographically without using
// library functions
public static int stringCompare(String str1, String str2)
{

int l1 = str1.length();
int l2 = str2.length();
int lmin = Math.min(l1, l2);

for (int i = 0; i < lmin; i++) {
int str1_ch = (int)str1.charAt(i);
int str2_ch = (int)str2.charAt(i);

if (str1_ch != str2_ch) {
return str1_ch - str2_ch;
}
}

// Edge case for strings like
// String 1="Geeks" and String 2="Geeksforgeeks"
if (l1 != l2) {
return l1 - l2;
}

// If none of the above conditions is true,
// it implies both the strings are equal
else {
return 0;
}
}

// Driver function to test the above program
public static void main(String[] args)
{
String string1 = "Geeksforgeeks";
String string2 = "Practice";
String string3 = "Geeks";
String string4 = "Geeks";

// Comparing for String 1 < String 2
System.out.println("Comparing " + string1 + " and " + string2
+ " : " + stringCompare(string1, string2));

// Comparing for String 3 = String 4
System.out.println("Comparing " + string3 + " and " + string4
+ " : " + stringCompare(string3, string4));

// Comparing for String 1 > String 4
System.out.println("Comparing " + string1 + " and " + string4
+ " : " + stringCompare(string1, string4));
}
}
1 comment
Comment actions Permalink

What do you mean by "type the same code"? Can you just diff what you've typed vs what was pasted to see where is the difference/typo?

Can you share the sample file that doesn't build/run?

0

Please sign in to leave a comment.