This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Lines in JOptionPane  RSS feed

 
Shawn Kuriakose
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Im new to this forum and also new to java, I need some help for my intro to java homework. Ive done most of the logic. The problem is to write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6. Numbers are separated by exactly one space. My professor wants it to be done in a Joptionpane window. When I try to do that, only one answer pops up in a window. How do I make my answers appear ten in a line, separated by exactly one space in only one window?
 
Carey Brown
Saloon Keeper
Posts: 3235
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need a StringBuilder in order to assemble the output into one single String. Then pass that string to JOptionPane.
 
Shawn Kuriakose
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im sorry, but what is a StringBuilder?
 
Carey Brown
Saloon Keeper
Posts: 3235
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Java Docs
 
Carey Brown
Saloon Keeper
Posts: 3235
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also: tutorial
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Assuming you are using Java8+, you may find a StringJoiner easier to use (it uses a StringBuilder object behind the scenes). Look through the StringJoiner documentation and there is a simple example similar to what you want. You can pass one argument to the constructor if you don't want start and end sequences. You don't have to call the add method several times on the same line.

What does divisible by 5 and 6 mean? Does 15 count? Or 18? What about 30?
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware: a StringJoiner only takes Strings as its method arguments: don't add i but String.valueOf(i).
 
Carey Brown
Saloon Keeper
Posts: 3235
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringJoiner doesn't work so well with the "ten per line" requirement.  Good to know about the class though.
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:StringJoiner doesn't work so well with the "ten per line" requirement. . . .
Good point; you would have to create a new StringJoiner every ten items.
 
Piet Souris
Rancher
Posts: 2014
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if OP has not been taught about StringBuilders, then why advise the use of a StringJoiner?

A very simple solution is:
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP appeared to know neither about StringBuilder nor StringJoiner; after looking at the requirements we concluded that StringJoiner was probably unsuitable
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!