File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String assignment from method

 
Hai-Tan Le
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, I get a return int value from my method. How do I set the value to a string character? My string character would change depending on the random int value generated.
I think I should be using an array, but I had problem setting a int to a String. please help, thanks

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are a couple of things you'd need to do...

First, you'd need to declare your method to return a string instead of an int:



Then, you'd need to build a string in your method and return that. So, after you get your random number, you probably would convert it (depending on the value) to "one", "two"... or "diamond", "heart", etc. and return that string instead of your int.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it a bit difficult to understand the question. Do you mean, "how do you get ace instead of 1?" That would require a String[] array, or a Map<Integer, String>. The array would look something like

String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};

and the Map might be filled like this . . .
myMap.put(1, "Clubs");
myMap.put(2, "Diamonds"); etc etc.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, you are slipping. I beat you by over a minute this time!
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I set the value to a string character?


I'm probably misunderstanding the question but I'll still answer it anyway. You turn int into String by concatenating it with a String, it can be an empty String like:
You can do the reverse with Integer.parseInt() like so:
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unnar Björnsson wrote:I'm probably misunderstanding the question but I'll still answer it anyway. You turn int into String by concatenating it with a String, it can be an empty String like:
You can do the reverse with Integer.parseInt() like so:

My guess is that is not what they want.

in the run() method on line 8, it looks like they are trying to print "Jack of Diamonds". Simply concatenating their int to "" would ultimately end up printing something like "8 of 2".

 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Campbell, you are slipping. I beat you by over a minute this time!
Yes, I know, but I only bother when it's Rob
 
Hai-Tan Le
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my program. When I run it, the output is only "This program displays a randomly chosen card."

Am I missing something? Why isn't the card and suits printed?
Thanks again

 
Christopher Nortje
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hai,

When you create a primitive array and specify the length, you are basically saying this array has X amount of values.
Example:

you are expressing the the array named card has 13 values.

The first value of an array is always at index 0
Example:

The last value of an array is always at index (length - 1)
Example:


In your case you need to increase your card size to 15, since your adding to the 14th index.
Example:


The same goes for your suits, even though your only adding four values to that array, your starting at the second index suits[1] and ending at an unallocated index of suits[4], you would have to increase your suit size to 5.

your application is most likely throwing an java.lang.ArrayIndexOutOfBoundsException
See http://download.oracle.com/javase/1.4.2/docs/api/java/lang/ArrayIndexOutOfBoundsException.html

Have a look at this tutorial java arrays, this explains it in more detail.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Also note each time the method is executed that array is created.
If your using java 1.5 or greater you could read up on java enumeration.
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic