0

I am writing a program for school. I have finished most of it but I keep getting an error unexpected type: required value found class right after my scanner object. It look correct to me but I keep getting this error. Any help would be appreciated.

/**
 *
 * @author Randy
 */
import java.util.Scanner;//Import scanner

public class RandyGilmanhw2a {
    static int year_of_birth;
    static int age;

    public RandyGilmanhw2a (){//begin constructor
        year_of_birth = 1900;
        age = 0;
    }//end constructor

    public int getYear(){//get year method
        return year_of_birth;
    }//end method

    public int getAge(int year_of_birth){//get year method
        age = 2014 - year_of_birth;
    return age;
    }//end get year method

    public void setYear (int year){//set year method
        this.year_of_birth = year;
    }//end method

    public static void main(String[] args) {//begin main

        RandyGilmanhw2a user1 = new RandyGilmanhw2a();

        Scanner year = new Scanner(System.in);//create a scanner object
        System.out.println("Please enter the year you were born: ");
        int year_of_birth = int year.nextInt();


        while( year_of_birth < 1900 || year_of_birth > 2014 ) {//begin while loop
            System.out.print("Please reenter the year you were born." );
            System.out.print("You must have an integer between 1900 and 2014:" );
            System.out.print("\n");
        }//end while 

        user1.getAge(year_of_birth);
        System.out.print("You are " + age + "years old." );

    }//end main

}//end class

Specifically, it is at this code.

int year_of_birth = int year.nextInt();

Thanks

Randy Gilman
  • 457
  • 1
  • 11
  • 21
  • Remove the second `int` from that line. – shmosel Jul 04 '14 at 07:02
  • 2 things -- those `static` fields probably shouldn't be `static`, and you really don't need to mark your braces like that -- good indentation should do that for you. – awksp Jul 04 '14 at 07:06
  • I got rid of the static in my variables year_of_birth and age. However, at this code "System.out.print("You are " + age + "years old." );", I get a non-static variable age cannot be referenced from a static context error. – Randy Gilman Jul 04 '14 at 07:11

2 Answers2

1

problem:

 int year.nextInt();

It is not a local field, it is an object that is calling its method(nextInt) that will return an integer so the correct way of initializing to year_of_birth is:

int year_of_birth = year.nextInt();//remove the int
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Ok that worked, I keep having a problem with my year_of_birth variable as well. After I fixed that error in the same line I get: local variable hides a field. – Randy Gilman Jul 04 '14 at 07:05
  • @RandyGilman if you are using netbeans then you need to disable that error because it is a bug, follow this thread http://stackoverflow.com/questions/4122959/usage-of-eclipse-warning-field-declaration-hides-another-field-or-variable – Rod_Algonquin Jul 04 '14 at 07:09
0

it is not an error to have a local variable with the same name as a field of the class.

IDE's such as Eclipse often have options that will flag such name shadowing as a warning, so you'll know when you accidentally do this.

But note that you can always access the class's field by prepending "this.", for example, "this.year_of_birth".

Changes to this, to prove what I´m saying:

   int year_of_birth_test = year.nextInt();//remove the int
paul
  • 12,873
  • 23
  • 91
  • 153