• Post Reply Bookmark Topic Watch Topic
  • New Topic

JOptionPane Printing from a for loop?  RSS feed

 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program that has to do with ciphers and cipher shifts. The program itself works fine, but I am having trouble printing out the answers to JOptionPane. I know the basics of JOptionPane, but my problem is this:
Majority of my program takes place in a for loop, and resolves the cipher (it is a basic cipher program) 1 digit at a time. So, only the last DIGIT (I don't know how to convert a digit to a CHAR in JOptionPane) is printed to JOptionPane. Any suggestions? Here is my code: (apologies for how obscure this problem is)

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use this API Character.toChars(answer) to convert the intermediate answer int to array of characters and then append this to an answer string.
 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unfamiliar with appending to Strings. What does this mean? Could you possibly post an example?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give you an example:

You have to declare a variable to hold your answer:

Then convert your answer int to a string and append it to your answer string:

The API Character.toChars(int) returns an array of characters. We then convert that array of characters into a String and concatenate it to our final answer string.

The answerStr holds your final string.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!