Problems with the operator -

I can't seem to get the last part of this to work. Keeps giving error that I can't use this operator. This is only my fifth week of working on Java and any advice would be great. Thanks and here is the code.

public class Assignment2 {

// main(): application entry point

public static void main(String[] args) throws IOException {

// set up the input stream

Scanner stdin = new Scanner(System.in);

// prompt and extract the first and last names

System.out.print ("Enter your first name: ");

String firstName = stdin.nextLine();

System.out.print ("Enter your last name: ");

String lastName = stdin.nextLine();

//compute the length of the name

int wordLength = firstName.length() + lastName.length();

//displays Full name plus name length

System.out.print ("Hello " + firstName + " " + lastName + ", you have " + wordLength );

System.out.println (" characters in your name." );

//Add uppercase parameter

String upperCaseName = firstName.toUpperCase() + " " + lastName.toUpperCase();

//display message with name in uppercase

System.out.println ("Your name with all capital letters looks like the following: " );

System.out.println ( upperCaseName );

//set initials

String firstInit = firstName.substring (0,1);

String lastInit = lastName.substring (0,1);

//display initials

System.out.println ("Your initials are: " + firstInit + lastInit );

//Ask user for year born

System.out.print ("Now " + firstName + ", please enter the year that you were born in: ");

String yearBorn = stdin.nextLine();

//display year born

System.out.println (yearBorn);

//define current year and calculate age

int currentYear = 2007;

String currentAge = (currentYear - yearBorn);

//subtact current year from year born and display message

System.out.println ("You are " + currentAge + " years old and it is a wonderful age!");

//display exit message

System.out.println ("Goodbye " + firstName + "!");

}

}

Getting error

Assignment2.java:54: operator - cannot be applied to int,java.lang.String

String currentAge = (currentYear - yearBorn);

^

1 error

-jGRASP wedge2: exit code for process is 1.

-jGRASP: operation complete

Message was edited by:

javaGreenhorn

[2430 byte] By [javaGreenhorna] at [2007-11-27 10:05:52]
# 1
You cannot subtract a String from an int. You should first parse the String to an int before subtracting it. See the Integer class how to parse it: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html
prometheuzza at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 2

I understand what you are saying. I thought the same thing, but unable to figure out how to do such a task. I am also new to the website you listed but know of it and have looked around in there before. It doesn't seem to give a clear answer on how to parse a string to an int value. Problem is that I have no idea how to write that kind of statement. I reviewed the listed site and I really couldn't figure out how to write it. I tried a few variations, but was not able to come up with the proper syntax. I realize this seems feeble, but I swear I'm trying here. Just trying to understand it.

javaGreenhorna at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 3
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
jGardnera at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 4
Post your best attempt at parsing a string. Ask a specific question.
BigDaddyLoveHandlesa at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 5
These 2 are what I thought would fix it. parseInt(yearBorn)or public static int parseInt (yearBorn) throws NumberFormatExceptionBoth of which result in errors.I just can't seem to get it.
javaGreenhorna at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 6

> These 2 are what I thought would fix it.

>

> parseInt(yearBorn)

parseInt is a static method of class Integer. Do you know how to call a static method?

> public static int parseInt (yearBorn) throws NumberFormatException

You really are confused. You are not writing your own method, simply

calling an existing one. Do you understand the difference?

String yearBorn = "1955";

int year = Integer.parseInt(yearBorn);

BigDaddyLoveHandlesa at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 7

I somewhat understand. With time I'm sure I will fully understand. I knew I had to assign and int to a new variable as you did.

int year

I was just unsure how to address the rest of the statement. The othe problem was that the statement was from stdin and made it a little tricky on me being new to Java and all. But I can't thank you enough for all the help. I just needed an example. Thank you, I understand a lot more now.

javaGreenhorna at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 8
Good luck, then. It takes a while to be able to hink in a programming language's syntax.
BigDaddyLoveHandlesa at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...
# 9

> I somewhat understand. With time I'm sure I will

> fully understand. I knew I had to assign and int to

> a new variable as you did.

He didn't assign int to a new variable. You don't have to assign anything to call the method.

>

> int year

That declares the variable year to be of type int. It doesn't assign anything.

year = Integer.parseInt(yearBorn);

That is what assigns a value to the variable year.

jverda at 2007-7-13 0:42:04 > top of Java-index,Java Essentials,Java Programming...