Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

char array equality

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm having some issues with the following part of a game that I am working on.
I have spent hours trying to get my head around this and I think I've just hit a brick wall and could use some guidance, so thanks in advance for any help.

Output: Is 12 random letters.
Input: make a word using these 12 random letters only.
Processing: Check if the input word contains ONLY the random letters (no other characters).

Example:

OUTPUT: HBTDLUEPOIWE
INPUT: But
PROCESSING: CORRECT

OUTPUT: HBTDLUEPOIWE
INPUT: Java
PROCESSING: INCORRECT  


I have tried a number of different things to try and implement this validation such as for loops & traversals etc... I'm now onto trying to use the Arrays.equals function to validate the input. It works But only if the arrays are the same length. Is there a way I can ignore length? Or am I going about this in a totally wrong way?

Here is my latest attempt:



Thanks again for your help.

Robyn
 
Master Rancher
Posts: 4083
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried a loop with the String replace method?
 
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create two arrays with those twelve letters in and the letters in the word you are testing.
Sort the two arrays.
Create Strings with those letters in.
Test whether the smaller is a prefix of the larger.
There are methods in String which should be able to do those things for you.
 
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For each char in the input, check if that char is contained in the output.

Question: if output is, say, 'b' and the input is 'bb', is that correct?
 
Robin Byrde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your feedback, I will have a look into these now.

Reply to Piet Souris:  Question: if output is, say, 'b' and the input is 'bb', is that correct?

No. Each char can only be used once.
If you have ever seen the TV show countdown, it's a bit like that I guess.
 
Campbell Ritchie
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But a truly random choice allows repetition; your original text had two Es in.

Actually, on second thoughts, I suspect my suggestion about prefixes won't work. Try putting all the letters into List<Character>s. Try the containsAll() method. Try removing letters in the smaller List from the larger List(from both Lists in turn) and see what the remove() method returns. The links should give you more information. Remember you can easily try removal on JShell.
 
Robin Byrde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that. I've actually almost solved this now just by using for loops and CharAt. Still working on the repetition aspect by using deleteCharAt. Sometimes you just need to try and explain to someone else what the problem is for the solution to come to you.  Thanks again for your help.
 
Campbell Ritchie
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robin Byrde wrote:. . . deleteCharAt.

That doesn't sound like a String method. Are you using StringBuilder (good idea, by the way)?

. . . explain to someone else what the problem is . . .

A well‑known technique, sometimes called rubber duck programming.
 
Saloon Keeper
Posts: 7615
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[Edit...]
 
Campbell Ritchie
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jshell> List<Character> of = "HBTDLUEPOIWE".chars().mapToObj(c -> Character.valueOf((char)c)).collect(Collectors.toList());
of ==> [H, B, T, D, L, U, E, P, O, I, W, E]

jshell> List<Character> subset = "BEET".chars().mapToObj(c -> Character.valueOf((char)c)).collect(Collectors.toList());
  ...>
subset ==> [B, E, E, T]

jshell> of.containsAll(subset)
$3 ==> true

So far, so good.

jshell> of.remove(Character.valueOf('E'));
$5 ==> true

jshell> of.containsAll(subset)
$6 ==> true

So that didn't work either.
 
Master Rancher
Posts: 3754
48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But a truly random choice allows repetition; your original text had two Es in.


I think the idea was that the output can have repetition, as shown.  But the input (the response to the output) cannot use more of each letter than were present in the outout.  So if the output was "bb", the input (response) could be "b" or "bb", but not "bbb".  Robin, is that correct?
 
Carey Brown
Saloon Keeper
Posts: 7615
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or make from input and output two frequency. tables. It is then very easy to do the check.
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic