Randy Gilman
Randy Gilman

Reputation: 457

Scanner Object giving me error in While loop

I am having a small issue with getting this program to run properly.

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

public class RandyGilmanhw2a {
    int year_of_birth;
    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 = year.nextInt();

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

            System.out.println("Please enter the year you were born: ");
            int year_of_birth = year.nextInt();//ERROR OCCURS HERE SAYS VARIABLE
            //year_of_birth ALREADY DEFINED IN METHOD MAIN
        }//end while 

        user1.getAge(year_of_birth);
        System.out.println("You are " + age + " years old." );//ERROR HERE SAYS NON-STATIC
        // VARIABLE age CANNOT BE REFERENCED FROM A STAIC CONTEXT

    }//end main

}//end class

I have commented on the areas that are displaying the error. I am trying to make a program that displays the age of a person by them entering there age. However, if they enter a year before 1900 or after 2014, I want it to ask the user to reenter their year of birth. I can't seem to find the problem. Any help would be appreciated.

Upvotes: 1

Views: 106

Answers (4)

Nejc Lubej
Nejc Lubej

Reputation: 21

Remove int at int year_of_birth = year.nextInt(); and change age output to this:

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

Upvotes: 2

Wundwin Born
Wundwin Born

Reputation: 3475

Don't define year_of_birth as int again in while loop as you have defined in out of loop.

 while( year_of_birth < 1900 || year_of_birth > 2014 ) {//begin while loop
        ...
        year_of_birth = year.nextInt();//just assign next value
        ...
    }

Upvotes: 1

Erik Gillespie
Erik Gillespie

Reputation: 3959

Remove the int from the second initialization of year_of_birth and your problem should go away.

Upvotes: 1

Hugo Sousa
Hugo Sousa

Reputation: 1936

Just remove the int declaration. That way, you're redifining the variable. So, switch this:

int year_of_birth = year.nextInt();

to this:

year_of_birth = year.nextInt();

Upvotes: 3

Related Questions