Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add brackets and commas to an array with random number  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When trying to search this on google it seems that everything is how to remove the brackets, but when I output the data it doesn't automatically output. Also if I have a random group of numbers such as {1, 2, 3} in an array I want to make it so that it outputs exactly like that, but at the same time, if I get an empty array it outputs just {}. I have put the code and it compiles, but when it runs through, it does the 1, 2, 3 output correctly but fails when it tries to output the empty array.

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an interesting scenario there, but it is easily solved by thinking from least to greatest--process the empty set first and then skip the code to process the non trivial sets.
 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Les, that seemed to do the trick. So if I was trying to get them in reverse order(ex: 1, 2, 3 would output {3, 2, 1} would I use similar code but reverse the loop? I tried to just use a for loop without the if else. It doesn't seem to be compiling though, I think it's because this isn't a void class and it wants me to input a return value, but I don't know what to put in. I tried a[i] but that didn't work.

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

The code is the same, the same number of lines, the same process, but you use a count down instead of a count up, and you change the if clause appropriately--so everything is exactly the same except you change some parameters in the for loop and condition in the if statement. Think about it for a while and you should see the process, write the steps out on paper, that is the problem solving approach and most of the task of programming, and implement them. I'll give you a word of advise that was giving decades ago in one of my classes: "There is not magic. Everything has a well defined process to get to a solution." Basically you just need to think of the process and implement those in Java. Find the process and you'll be alright.

In each problem you usually have 2 things to solve--the process, and the halting condition. You're halting condition is when all of the elements of the array have been exhausted, and all is left is adding the closing curly brace. Notice that halting process is the same in your empty set: in both conditions you have to put an open brace, but when there are no more elements you have to close the set with a closing curly brace. Also notice the pattern that is created when you apply the individual elements of the arrays, is there a point the pattern changes? If there is, that is where you need the if statement.

Les

BTW: if you do not know how to fix the problem with the "void" class, actually method, then you need to go back to your Java book and do some studying--therein could be a lot of your problem.

Jeff Sak wrote:Thank you Les, that seemed to do the trick. So if I was trying to get them in reverse order(ex: 1, 2, 3 would output {3, 2, 1} would I use similar code but reverse the loop? I tried to just use a for loop without the if else. It doesn't seem to be compiling though, I think it's because this isn't a void class and it wants me to input a return value, but I don't know what to put in. I tried a[i] but that didn't work.

 
Campbell Ritchie
Marshal
Posts: 55691
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read about String Joiners here, and it also shows you how to use Collectors.joining to do the same in a Stream.
 
Junilu Lacar
Sheriff
Posts: 11141
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:System.out.println(sj);[/code]You can read about String Joiners here, and it also shows you how to use Collectors.joining to do the same in a Stream.

You learn something every day on the Ranch. Thanks, Campbell!
 
Campbell Ritchie
Marshal
Posts: 55691
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure I forgot to say those classes are Java8+ only.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!