Results 1 to 3 of 3
  1. #1
    mindfcked is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 'local variable hides a field'

    I'm getting this error when trying to set the value of an int to a specific user input with the scanner. here's my code:

    Java Code:
    package coinflip;
    
    import java.util.Scanner;
    
    public class CoinFlip
    {
         public int userinput;
       //-----------------------------------------------------------------
       //  Creates first coin object, and flips it. 
       //-----------------------------------------------------------------
       public void main (String[] args)
       {
               Scanner scan = new Scanner(System.in);
               System.out.println("Enter the desired number of flips.");
               int userinput = scan.nextInt();
    
                   }
       {
          Coin c1 = new Coin();
         
         c1.flip();
          c1. getvalue();
          System.out.println(c1.getvalue());
          
          
          Coin c2 = new Coin();
          c2.getvalue();
          System.out.println(c2.getvalue());
          
          if (c1.getvalue() > c2.getvalue()) {
          System.out.println ("Tails! Player 2 Wins!");
          }
          else if (c1.getvalue() < c2.getvalue()) {
              System.out.println("Heads! Player 1 Wins");
          }
         else if (c1.getvalue() == c2.getvalue()) {
              System.out.println("Tie!");
              
              
       }
          }
       
       
    }
    Java Code:
    //********************************************************************
    //  Coin.java       Author: Lewis/Loftus
    //
    //  Represents a coin with two sides that can be flipped.
    //********************************************************************
    
    package coinflip;
    
    public class Coin
    {
       private final int HEADS = 0;
       private final int TAILS = 1;
    
       private int face; 
       private int p1wins = 0;
       private int p2wins =0;
       private int ties = 0;
    
       //-----------------------------------------------------------------
       //  Sets up the coin by flipping it initially.
       //-----------------------------------------------------------------
       public Coin ()
               
       {
          flip();
       }
    
       public int p1; //increment p1win each time player 1 wins, must be tied into flip
       {
           p1wins++;
       }
       public int p2; //incremnt p2win each time players 1 wins, must be tied into flip
       {
           p2wins++;
       }
       public int tie;
       {
           ties++;
       }
       
       public int getvalue ()
       {
           return face; 
       }
       //-----------------------------------------------------------------
       //  Flips the coin by randomly choosing a face value.
       //-----------------------------------------------------------------
       public void flip ()
       {
          face = (int) (Math.random() * 2);
       }
       {
           System.out.println("Face is: "+ face);
       }
    }
    I also need advice as for how to tell the program to repeat the coinflip a certain number of times. Would this be through a conditional statement? I think it is, but I can;t find one that seems to work.

    also posted on javaprogrammingforums.com

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: 'local variable hides a field'

    Your code as formatted is very difficult to read. Please consider editing your post and fixing your indentation style so that it is uniform and consistent. I usually avoid using tabs for indenting (forum software often doesn't play well with tabs) and indent each code block 3 spaces.

    As for your actual problem, the warning message (it's not an error) is telling you that you're re-declaring a class variable inside of a method or constructor.

    By the way, thanks for notifying us about your cross-post. This is *much* appreciated!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: 'local variable hides a field'

    And actually if you correctly format your code, you'll see that it is not written correctly, that you have an initializer block (a block of code in curly braces but not in a method or constructor), and I'm pretty sure that you don't want to do that. Also note that a functioning main method must be declared static.

Similar Threads

  1. Local variable might not have been initialized
    By outforlunch in forum New To Java
    Replies: 1
    Last Post: 09-17-2012, 09:16 PM
  2. The Local variable is never read
    By Gunaseelan in forum Java Servlet
    Replies: 2
    Last Post: 01-05-2011, 09:02 AM
  3. BUG:local variable is never read
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-03-2010, 08:23 AM
  4. Replies: 2
    Last Post: 12-05-2009, 11:24 AM
  5. Replies: 2
    Last Post: 03-31-2008, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •