Results 1 to 9 of 9
  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default operator undefined for argument type int, String

    Hello all,

    having the above error for the below class2.java (error line in bold and big below)

    class1.java

    Java Code:
    import java.util.Scanner;
    
    public class class1 {
    
    	public static void main(String args[]){
    		int curyear = 2011;
    		int old;
    		
    		Scanner input = new Scanner(System.in);
    		
    		class2 newobject = new class2();
    		
    		System.out.println("Please enter the year when you were born");
    		String year = input.nextLine();
    		
    		//System.out.println(age);
    		
    		newobject.c2method(year);
    	}
    }
    class2.java

    Java Code:
    public class class2 {
    
    	public void c2method(String year){
    		int curyear = 2011;
    		int old;
    		old = curyear - year // was bold and big
    		
    		System.out.println("You were born in " + year);
    	}
    }

    Your help is much appreciated
    Last edited by DarrylBurke; 05-29-2011 at 05:50 PM. Reason: Added code tags, removed vertical whitespace and size tags

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As the error says, you are trying to subtract a string from an int. It won't work, try parsing the string.

    Also, when you ask for help, use code tags
    [code]
    YOUR CODE HERE
    [/code]

    And always post the exact error message(copy/paste, not paraphrased)
    Last edited by sunde887; 05-29-2011 at 05:17 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default

    As the error message says: you can't subtract a String from an int. The only operator defined for the String operand is the binary addition operator +. Read the API documentation for the Integer class for a suitable method that converts a String to an int (so you can subtract two ints).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    As the error says, you are trying to add and int and a string and store it in an int. It won't work, try parsing the string.
    Adding a String and an int does work, subtracting them is undefined.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    Adding a String and an int does work, subtracting them is undefined.

    kind regards,

    Jos
    >< misread the question. Edited my post to be correct. For some reason I saw a + instead of a -. Perhaps I need to sleep.

  6. #6
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    Sorry guys this was my first post ever related to java, I will try to find out how I can convert the string to an integer so that the subtraction works.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    There's a static method in the Integer class to do just that conversion. Also the Numbers and Strings section of Oracle's Tutorial will be useful.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Errors aside, your program doesn't make sense.

    You ask the user to enter the year they were born. That value gets passed to the other class/method. A calculation is performed. Result is ignored. "you were born in ...." is displayed. Not very impressive considering you asked the user to input that value in the first place.

    Perhaps you meant to calculate their age. Or ask user to input age and calculate the year they were born.

  9. #9
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    I got the age that I wanted by changed the Scanner to an int type

    Java Code:
    import java.util.Scanner;
    
    
    
    
    public class class1 {
    	public static void main(String args[]){
    		int curyear = 2011;
    		int old;
    		
    		Scanner input = new Scanner(System.in);
    		
    		class2 newobject = new class2();
    		
    		
    		
    		System.out.println("Please enter the year when you were born");
    		int year = input.nextInt();
    		
    		
    		//System.out.println(age);
    		
    		newobject.c2method(year);
    		
    	}
    
    }

    Java Code:
    public class class2 {
    	public void c2method(int year){
    		
    		
    		int curyear = 2011;
    		int old;
    		old = curyear - year;
    		
    		
    		System.out.println("You are " + old + " years old");
    				
    	}
    
    }

Similar Threads

  1. Replies: 0
    Last Post: 12-16-2010, 07:35 AM
  2. Replies: 10
    Last Post: 12-07-2010, 12:29 PM
  3. Replies: 2
    Last Post: 05-07-2010, 11:02 PM
  4. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  5. Replies: 0
    Last Post: 05-27-2009, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •