This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Lines in JOptionPane  RSS feed

 
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?
 
Saloon Keeper
Posts: 4756
52
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: 4756
52
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: 4756
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also: tutorial
 
Marshal
Posts: 60127
188
  • 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: 60127
188
  • 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: 4756
52
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: 60127
188
  • 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.
 
Rancher
Posts: 2829
96
  • 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: 60127
188
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!