File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String assignment from method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String assignment from method" Watch "String assignment from method" New topic
Author

String assignment from method

Hai-Tan Le
Greenhorn

Joined: May 27, 2011
Posts: 6
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

Campbell, you are slipping. I beat you by over a minute this time!
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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

Joined: Oct 13, 2005
Posts: 40041
    
  28
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

Joined: May 27, 2011
Posts: 6
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

Joined: Jul 09, 2010
Posts: 16
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
 
subject: String assignment from method