• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding commas to output

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi am trying to output a list of permutation to the console in a comma delimited form but i have not been successful. Any help would be deeply appreciated

My result
hat
hta
aht

Expected results(This is not the actually results just a sample to understand what i want to achieve)
hat,hta,aht
abc,acb,bac

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I get what that codes supposed to achieve?

For example:

You read a line from the file, then split on a new line.
But there shouldn't be any new lines in that line as you've just read in a line.

Next is this:

The join() method takes a separator and either a load of CharSequences, or a collection of CharSequences.
In your case you supply only one String, so there's nothing to actually join.

Taking those two bits into account, I would take a step back and figure out what your steps should be and what should be the output from each step.
Start with reading from the file.

What are you reading and what do you want to do with each line you read?
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:I'm not sure I get what that codes supposed to achieve?

For example:

You read a line from the file, then split on a new line.
But there shouldn't be any new lines in that line as you've just read in a line.

Next is this:


The join() method takes a separator and either a load of CharSequences, or a collection of CharSequences.
In your case you supply only one String, so there's nothing to actually join.

Taking those two bits into account, I would take a step back and figure out what your steps should be and what should be the output from each step.
Start with reading from the file.

What are you reading and what do you want to do with each line you read?



You are right Dave i shouldn't create a new line and i just tried using the String.join to see if it would work but nothing happen.
So i'm reading from a file permute.txt and splitting the characters with
because i want each character to be treated differently. Then i'm "permutating" through the characters using the permuteMe method.
The output am getting is to stdout the permutations of the string separated by comma, in alphabetical order.

Permute File
hat
abc
Zu6

Expected output
aht,ath,hat,hta,tah,tha
abc,acb,bac,bca,cab,cba
6Zu,6uZ,Z6u,Zu6,u6Z,uZ6
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what does your code look like which produces that incorrect output, with any "test" code stripped out, because that stuff will simply confuse the matter.
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:So what does your code look like which produces that incorrect output, with any "test" code stripped out, because that stuff will simply confuse the matter.



On line 4 of the code

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why do you not simply add commas where you require them?

Is this an == true I see before me? Never write that sort of thing. Not only is it poor style, but also error‑prone if you write = instead of ==.
Not if (b == true)... but if (b)...
Not if (b == false)... but if (!b)...
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should have an array of permutations which should be completely separate from the logic used to output that array.

Regular System.out.print() is your friend - not System.out.println(). So for all the characters except the last for a given line use System.out.print(value + ","), then just System.out.print(value) for the last character and then add a newline character or just use System.out.println();
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic