Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Please help me with my String variable and switch statement problem.

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Please help me with my String variable and switch statement problem.

    Here is an example of my code:

    import java.util.Random;
     
    public class cards {
    public void Dimonds(){
    	int Rnumber; 
    	Random rng = new Random();
    	String SN;
     
    	Rnumber = rng.nextInt(51);
     
    		switch(Rnumber){
    		case 0:
    		SN = "Ace of Dimonds";
    		break;
     
    		case 1:
    		SN = "2 of Dimonds";
    		break;
     
    		case 2:
    		SN = "3 of Dimonds";
    		break;
     
    		case 3:
    		SN = "4 of Dimonds";
    		break;
    ...
    I know that the switch statement is right (I did finish it off at the bottom it was just to long full deck of cards worth.). My problem here is that the variable SN that I made is not storing. When I try to print it via a println it says "The local varriable SN may not have been initialised. If you need any more information please feel free to let my know.


  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Please help me with my String variable and switch statement problem.

    just one question. Why don't you just put all these cards in, say, an arrayList and use the randomly generated number as the index?


    The warning that it may not be initialized just means that it is null when you start working with it and could cause a NPE if done wrongly.
    Also the reason it won't print is probably because you are declaring it locally, in stead try something like this:

    public class cards {
     
    public String SN; //or private with a getter if you want it to be safe :)
     
    private ArrayList<String> cards;
     
    public cards()
    {
      cards = new ArrayList<String>();
     
      cards.add("Ace of Dimons");
      cards.add("2 of Dimonds");
      ... etc.
    }
     
    public void Dimonds()
    {
       SN = ""; //to initialize it
     
      // do other stuff here
     
      SN = cards.get(randomNumber);
    }

    then whenever you call it, it should hold a value.

  3. The Following User Says Thank You to killernerd For This Useful Post:

    ace1 (August 7th, 2013)

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help me with my String variable and switch statement problem.

    The problem I have with doing that is I don't know how to work with arrays as well as switch statements, but I will jump ahead in my totorials to check them out. I'm trying to learn java (at least) before I go to college. Either way Thanks. Oh and what do you mean by I am declaring locally?

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help me with my String variable and switch statement problem.

    The error is because not all possible code paths initialize SN. Giving SN a value when it is declared, (even null), will take care of that, but the most important thing is to be sure that SN does get a valid value through all possible code paths.
    Without seeing the full body of the switch, it is hard to say for sure, but the compiler probably worries that no switch case will be met, and SN will never be initialized.
    Post the full body of the method if you still need help with it

  6. The Following User Says Thank You to jps For This Useful Post:

    ace1 (August 7th, 2013)

  7. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Please help me with my String variable and switch statement problem.

    <nitpick>
    Diamonds
    </nitpick>
    BTW this is a very inefficient way to create a deck of cards
    Improving the world one idiot at a time!

  8. The Following User Says Thank You to Junky For This Useful Post:

    jps (August 7th, 2013)

  9. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help me with my String variable and switch statement problem.

    Thanks that did work!

    --- Update ---

    Thanks for the help. Is there a way for me to mark the thread as "solved" or "finished"?

  10. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help me with my String variable and switch statement problem.

    You can mark the thread as solved from the Thread Tools just above the first post.
    Thread marked as solved.

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. Switch statement using a String with Java 1.7
    By eloel33 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 23rd, 2012, 05:11 AM
  4. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM

Tags for this Thread