Win a copy of High Performance Python for Data Analytics this week in the Python 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

Java Exercise

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

I'm trying to learn how to program in Java, but I'm stuck on an exercise.

I have to print all combinations of the string 'XYZ' in lexicographic order (the order that words are found in dictionary). I'm able to do that in Java. The output is:
XXX
XXY
XXZ
XYX
XYY
XYZ
(etc...)

Here is my program:



However, if I want to remove, let's say, all combininations that includes 'X' and 'Y', what should I do?

Thank you in advance!
Anni-Frid
 
Saloon Keeper
Posts: 7622
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
Wouldn't this work?
Please don't double space your lines of code.
 
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

, what should I do?


Is that a question about what the algorithm is to solve the problem?
Or do you have the algorithm for the solving the problem and are asking how to code it in java?
If the latter, post it with your questions about how the problems you are having writing the code.
 
Anni-Frid Lyngstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your replies.

Let me start again from the beginning (at first I tried to simplify the problem but now I realize that the way that I put it changes everything).

First of all, I have to write a code in Java that prints all the combinations of the string 'WXYZ' in lexicographic order (I removed the 'W' to make the problem simpler but now I realize that I should not have).

That should give me:

WWWW (first line)
WWWX
WWWY
WWWZ
WWXW
etc...
ZZZZ (last line)

That part I can get it done easily (with the code I posted above).

However, I have to remove (i.e. the code should not print) all the combinations that contain 'W' and 'Z' (at the same time).

For example, the combination WWWZ should not be in the output since it contains 'W' and 'Z'.

I don't know how to write that in Java.

@Corey: Sorry, I will be more careful next time.
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you use the String class methods?  For example use indexOf two times: once for W and once for Z
 
Carey Brown
Saloon Keeper
Posts: 7622
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
Just to take the requirements you just filled in and stubbing out the code you should have something like this. This would be "a" way of doing it.
 
Carey Brown
Saloon Keeper
Posts: 7622
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
Oops. There's a simple error in there, see if you can spot it. Still, stubbing things out helps narrow down the task.
 
Anni-Frid Lyngstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We need a return statement at line 21 but I am struggling to put it properly...
 
Anni-Frid Lyngstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I didn't see the "to do" comment that you have added in the code. At first I thought you put a random "1000" there (I need to put my glasses on).

But even when I add a "return false;" statement (because of the boolean type), I get the same result, i.e. it prints all the combinations without properly removing those with W and Z.

And don't get me wrong, I don't understand the use of the "return false;" statement in that context.
 
Anni-Frid Lyngstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Norm: Tomorrow I'll try to use indexOf, looks like a good idea!
 
Marshal
Posts: 71749
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why would you want indexOf()? What about contains()? But that doesn't answer my first question. “Trying out” a method looks like guessing, and it looks as though you hadn't yet worked out what(←read that link) you want to do. You cannot write code until you have worked out what you are doing, and then you write the algorithm on paper. Once you have the correct algorithm, the coding will become easy.
 
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose that your 'lexicographic' method has a parameter 'List<Character> doNotPrint', then it is efficient to take this into account in the 'for' loop, like

Another thing:
it is not very userfriendly to bother the user with a method that requires a sorted char[] and an output string. A possibility is to make this method private, rename it as say 'lexicographicHelper'. Have a user method 'lexicograpic(String string, List<Character> doNotPrint)', that calls the Helper method.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic