3 Replies - 368 Views - Last Post: 08 April 2012 - 06:42 PM Rate Topic: -----

#1 tcssamuel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-April 12

If/Else Statement Not Initializing Variable?

Posted 08 April 2012 - 06:19 PM

Hi, this is the first part of a program I'm working on, but I've run into trouble. When trying to run my program thus far, I recieve the following error:

[error]
"ProbabilityFair2.java:26: error: variable stage1 might not have been initialized
System.out.println("You have " + stage1 + " stage one.");
^
1 error"
[/error]

I've tried searching around for information on if/else statements, and I feel like based on what I've found, there shouldn't be a problem. I've tried changing my variable names and made sure they all matched, but have been unsuccessful. I originally thought that the random variable might be causing the problem, but on another program I've made, I was able to use an if/else statement with a random variable. I'm just looking for a way to solve this problem. Thanks!

   
import cs1.Keyboard;
   import java.util.*;

   public class ProbabilityFair2
   {
      public static void main(String [] args)
      {
      
         System.out.println("Please choose a shell: 1, 2, or 3: ");
         int shell;
         shell = Keyboard.readInt();
         System.out.println("You have chosen shell: " + shell);
         System.out.println("You have decided to keep the shell you chose.");
      
      
         Random generator = new Random();
         int rnumber = generator.nextInt(3)+1;
         String stage1;
        
         if (rnumber >= 2) 
            {stage1 = "lost";}
         else if (rnumber <= 1)
            {stage1 = "won";}
         
            
         System.out.println("You have " + stage1 + " stage one.");
      }}



Is This A Good Question/Topic? 0
  • +

Replies To: If/Else Statement Not Initializing Variable?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: If/Else Statement Not Initializing Variable?

Posted 08 April 2012 - 06:34 PM


if (rnumber >= 2) 
            {stage1 = "lost";}
         else if (rnumber <= 1)
            {stage1 = "won";}


May be YOU know that rnumber will be for sure in those brackets range but the compiler does not know
You will have to initialize String stage1 = null; to satisfy it
Was This Post Helpful? 2
  • +
  • -

#3 tcssamuel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-April 12

Re: If/Else Statement Not Initializing Variable?

Posted 08 April 2012 - 06:41 PM

View Postpbl, on 08 April 2012 - 06:34 PM, said:


if (rnumber >= 2) 
            {stage1 = "lost";}
         else if (rnumber <= 1)
            {stage1 = "won";}


May be YOU know that rnumber will be for sure in those brackets range but the compiler does not know
You will have to initialize String stage1 = null; to satisfy it


Thanks for helping a newbie to Java out! Worked perfectly.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: If/Else Statement Not Initializing Variable?

Posted 08 April 2012 - 06:42 PM

Glad I could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1