Mav
Mav

Reputation: 1

bad operand types for binary operator "-" first type: int; second type: java.lang.String

I'm having troubles converting a String(birthyear) into an int(age). I want someone to type in their year of birth, and have the program do a simple subtraction calculation to work out their age. I'm new to programming, so I've been searching around, and most places tell me the same thing.

Integer.parseInt(birthyear);

However, having done that, when I try to do the math...

int age = year-birthyear;

I get the error in the title.

public class WordGameScanner
{
    public static void main(String[] argus)
    {
        String name;
        String home;
        String birthyear;
        String course;
        String reason;
        String feedback;
        int year = 2013;

        Scanner input = new Scanner(System.in);
        System.out.print("What is your name: ");
        name = input.nextLine();
        System.out.print("Where are you from: ");
        home = input.nextLine();
        System.out.print("What year were you born: ");
        birthyear = input.nextLine();
        Integer.parseInt(birthyear);
        System.out.print("What are you studying: ");
        course = input.nextLine();
        System.out.print("Why are you studying " + course + ": ");
        reason = input.nextLine();
        System.out.print("How is " + course + " coming along so far: ");
        feedback = input.nextLine();

        int age = year-birthyear;

        System.out.println("There once was a person named " + name +
            " who came all the way from " + home +
            " to study for the " + course +
            " degree at --------------.\n\n" + name +
            " was born in " + birthyear + " and so will turn " + age +
            " this year.");
        System.out.println(name + " decided to study the unit ------------, because \"" +
            reason + "\". So far, ----------- is turning out to be " +
            feedback + ".");
    }
}

I'm sorry if this is in the wrong place, this is only my second post here. I just clicked "ask a question" and followed the directions >.<

Upvotes: 0

Views: 15647

Answers (4)

Mike Samuel
Mike Samuel

Reputation: 120516

int age = year-Integer.parseInt(birthyear);

Calling parseInt doesn't redefine the variable String birthYear as an int, it just returns an int value that you can store in another variable (like int birthYearInt = Integer.parseInt(birthYear);) or use in an expression as above.


You may also want to take a minute to think about inputs.

Your users may enter just the last two digits ("83" instead of "1983"), so you could do:

int birthYearInt = Integer.parseInt(birthYear);
if (birthYear.length() == 2) {
  // One way to adjust 2-digit year to 1900.
  // Problem: There might not be more users born in 1900 than born in 2000.
  birthYearInt = birthYearInt + 1900; 
}
int age = year = birthYearInt;

Alternatively, you could use java.text.NumberFormat to properly handle commas in inputs. NumberFormat is a good way to deal with numbers that come from humans since it handles the ways people format numbers differently from computers.


Another problem is that this uses the Chinese age numbering system where everyone's age increments by one on the New Year (of the Chinese Lunar calendar, not the Gregorian calendar). This is not they way ages are calculated worldwide though. In the US and most of Europe, for example, your age increments on the anniversary of your birth.

Upvotes: 7

Ravindra Gullapalli
Ravindra Gullapalli

Reputation: 9178

even after executing Integer.parseInt(birthyear); without assigning to anything, birthyear will be a string.

Either use int age = year-Integer.parseInt(birthyear);

OR

int ibirthyear = Integer.parseInt(birthyear);
int age = year - ibirthyear;

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122384

Integer.parseInt doesn't change birthyear from a String to an int, it simply returns the int that the string represents. You need to assign this returned value to another variable and use that in the subtraction.

Upvotes: 0

BackSlash
BackSlash

Reputation: 22243

Integer.parseInt(birthyear);

does not overwrite birthyear value and does not change it's type from String to int

so you have to do

int intbirthyear = Integer.parseInt(birthyear);

and then

int age = year-intbirthyear;

Upvotes: 2

Related Questions