• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String assignment from method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, you are slipping. I beat you by over a minute this time!
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic