+ 1
Error in the Code. Java De Javu Problem.
I've been trying to solve this but only 2/5 cases are correct so far. So what i've been trying to do is split the given word into an array with each slot storing an alphabet. Then i compare an alphabet from each slot with the rest. Let's just say the word is "aye". First, i compare "a" with "y" and "e". After the comparisons, i jump to "y" and then compare it with "e". I used a boolean expression to record true when the letters match and using that boolean result, i would output whether or not the letters are unique or not. I've been trying this for an hour and still don't know where i get it wrong.
5 Answers
+ 1
Thank You everyone.
I used all your suggestions and it works finally.
0
Here's the code -
Scanner sc = new Scanner(System.in);
String phrase = sc.nextLine();
String arrSplit[] = phrase.split("");
boolean tf = false;
int k = 1;
for (int j=0;j==(arrSplit.length-1);j++){
for (int i = k;i>(arrSplit.length-1);i++){
if (arrSplit[j] == arrSplit[i]){
tf = true;
break;
}else{
tf = false;
}
}k = k+1;
}
if (tf=true){
System.out.println("Deja Vu");
}else{
System.out.println("Unique");
}
}
}
0
You need to use the equals() method when comparing strings for equality, not ==
your outer loop is using == instead of < in the comparison and should be length -2
Otherwise it looks like you're close.
ps you can get rid of the else block and the variable k isn't needed. you can use i = i+1 in the inner for instead. it should work after those changes.
0
I think, this program contains only 1 word input, so need of split method.. But if you are following different method of using 0 length in loop, fine but in this, 2nd loop, is Infinite.
Check out.. Its only comparing first charecter, with Remaing characters if it is not match then it is Infinite loop.. If it matches, then only breaks..
for (int i = k;i>(arrSplit.length-1);i++){
So it i<arrSplit[0].length works, for j also...
if (tf=true){ here use, == to compare..
0
Actually you can just use
if (tf) {}
No need for any comparison