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 4 of 4

Thread: 'local variable hides a field'

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:

    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!");
     
     
       }
          }
     
     
    }

    //********************************************************************
    //  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 java-forums.com
    Last edited by mindfcked; October 21st, 2012 at 11:31 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: 'local variable hides a field'

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/64122-local-variable-hides-field.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: 'local variable hides a field'

    Quote Originally Posted by copeg View Post
    This thread has been cross posted here:...
    At least he mentioned the cross-post in his original post. For that I give him kudos.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: 'local variable hides a field'

    Quote Originally Posted by curmudgeon View Post
    At least he mentioned the cross-post in his original post. For that I give him kudos.
    True . But no link - hence I provided one

Similar Threads

  1. How to compare variable with multiple variable?
    By mharck in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2012, 09:02 AM
  2. scope of a class created as local variable
    By Samaras in forum Java Theory & Questions
    Replies: 6
    Last Post: August 25th, 2012, 06:01 PM
  3. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM