0

Can someone help me crack my Isogram code?

Having trouble comparing each value to one another https://code.sololearn.com/c0D0nHS75WC4/?ref=app

27th Feb 2023, 9:44 PM
Emmanuel Kiprop Kimutai
Emmanuel Kiprop Kimutai - avatar
9 Antworten
+ 2
Emmanuel Kiprop Kimutai It looks like the logic doesn't work. You're only testing three letters & showing false only if all three match (aaaaaaa will show false). You had the right idea, checking with extra print statements. The usual strategy for an isogram test is to test each character of the word against only the next and so on. So you can move thru the word at two different rates using two loops (ex. loop i & loop j). As this happens you need only one if statement to compare, for example: if (myArr[i] == myArr[j]) ... then false There are other ways to do it but this method is most common. Try again, if you have more questions ask. And try to avoid looking at full solutions from others unless you have to - tips are good but solving this yourself is good practice. The 5 xp for solving a Code Coach aren't worth much, the learning is. Unfortunately some will just post a full solution which is not really helping you learn to form strategies. https://code.sololearn.com/c1vtIn66Y2EI/?ref=app
28th Feb 2023, 2:46 AM
Scott D
Scott D - avatar
+ 2
Emmanuel Kiprop Kimutai I'm not sure what you mean - will what work? If the code works it will work for any word. If there is an error in the code it may work for some words but not others. It is a little tricky. The common way is using 2 for loops although that isn't the only solution. But they need to be offset, or the cormparison needs to be offset. If you compare arr[0] of one loop to arr[0] of another then it will match even if the character is in the word only once, which you don't want. So you can either offset the loops by 1 (start one loop on 0 & the other on 1) or offset the comparison: if(arr[ i ] == arr[ j +1]) ... false or offset both. And meanwhile use print to check the values so you can see what each loop is doing, such as putting: System.out.print(myArr[ i ] + "-"); System.out.print(myArr[ j ] + ","); into the inside loop to see what you are comparing. Maybe try that, post your code again if it's not working.
28th Feb 2023, 6:42 AM
Scott D
Scott D - avatar
+ 1
Emmanuel Kiprop Kimutai Ok, so check attached code. You'll see if you try "cerebral" some letters DO match! So not an isogram. You're close!! So first you need to fix the if statement: if(myArr[i]==myArr[j]){ isIsogram = false; }else{ isIsogram =true; If there is a match, it must print false immediately from the if statement and then return so it doesn't keep checking. Otherwise it keeps looping. And if it ends on two letters that don't match then you end up with true which is not what you want. Notice in code that it finds two matching e-e. However it keeps going and the last a-r don't match so falsely says "true". Also loops are not continuing long enough, neither gets to final char 'l'. https://code.sololearn.com/caBDEyjxqp8K/?ref=app
28th Feb 2023, 6:56 AM
Scott D
Scott D - avatar
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sc = input.nextLine(); boolean isIsogram = true; char[] myArr = sc.toCharArray(); for (int i = 0; i < myArr.length; i++) { for (int j = i + 1; j < myArr.length; j++) { if (myArr[i] == myArr[j]) { isIsogram = false; break; } } } System.out.println(isIsogram); } }
28th Feb 2023, 2:57 PM
k dot
k dot - avatar
0
Thanks. Yeah. I try my best to solve the code on my on. I tried two loops but used a while loop. Let me try again with a for loop
28th Feb 2023, 6:02 AM
Emmanuel Kiprop Kimutai
Emmanuel Kiprop Kimutai - avatar
0
Will this work for a word like cerebral? Scott D
28th Feb 2023, 6:22 AM
Emmanuel Kiprop Kimutai
Emmanuel Kiprop Kimutai - avatar
0
Emmanuel Kiprop Kimutai Oops, ok I see you did update your code... give me a minute here...
28th Feb 2023, 6:46 AM
Scott D
Scott D - avatar
0
Scott D Sourav Chettri Thanks for your help. I've updated the code. It works.
28th Feb 2023, 6:05 PM
Emmanuel Kiprop Kimutai
Emmanuel Kiprop Kimutai - avatar
0
БАМ БАМ БАМ БАМ ФУРА ЕДЕТ ПО ХОЛМАМ
1st Mar 2023, 4:07 PM
Эгор Третьяков
Эгор Третьяков - avatar