Results 1 to 10 of 10
  1. #1
    Gavinsco is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Incompatible Types

    I get an error message something along the lines of "Required int, found java.lang.string" so it leads me to believe that the program believes "usernumber" is a string even though it is clearly declared as an integer. Just new to Java so it's probably really obvious but I'm still trying to understand the code.

    import java.util.Scanner;

    //Created by Gavin S. on 09/12/13.

    public class First_Attempt {
    public static int main(int[] args){
    System.out.print("Enter Number");
    //int usernumber = Scan.nextline();
    Scanner sc = new Scanner(System.in);
    int usernumber = sc.next();
    System.out.println((usernumber * 2) + " Should be The Number Doubled");
    }
    }

  2. #2
    Gavinsco is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Incompatible Types

    Just ignore:
    //int usernumber = Scan.nextline();

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Incompatible Types

    What does the next() method return?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Gavinsco is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Incompatible Types

    I have absolutely no idea what it even is, i just saw it online and thought it looked necessary for the scanner.... what is it exactly?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Incompatible Types

    If you look closely at the API documentation for the next() and nextLine() methods, you can read that they return a String type object, but you want to assign it to an int type variable; the compiler doesn't like it at all ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Gavinsco is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Incompatible Types

    but the line of code is INT usernumber.... does that not make it an integer? how would i code it so that it does? and what the hell is a 'method'??

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Incompatible Types

    what the hell is a 'method'??
    That should be covered in the first week of java.
    See: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    line of code is INT usernumber.... does that not make it an integer?
    Yes, that does not make usernumber an int. That line defines: usernumber as an INT object. INT being the name of some class.
    Last edited by Norm; 12-13-2013 at 08:17 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Incompatible Types

    Quote Originally Posted by Norm View Post
    [/url]

    Yes, that does not make usernumber an int. That line defines: usernumber as an INT object. INT being the name of some class.
    I think you're taking the poor OP's words a little too literally, the code in the first post clearly shows an int primitive and not some fictive INT class.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Incompatible Types

    Yes, the objective was to show that with java spelling and case are important. When talking about a class or data type, it is important to be specific. I suppose it was: Way too subtle
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Incompatible Types

    Quote Originally Posted by Gavinsco View Post
    but the line of code is INT usernumber.... does that not make it an integer? how would i code it so that it does? and what the hell is a 'method'??
    The variable 'usernumber' is an int (not an INT) but the scan.next() and/or the scan.nextLine() methods return a String; it's as if the variable is a round hole but the method returns a square block; they don't fit.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. incompatible types
    By XNOViiCE in forum New To Java
    Replies: 7
    Last Post: 10-22-2013, 05:28 AM
  2. Why are these types incompatible?
    By ras_oscar in forum New To Java
    Replies: 4
    Last Post: 10-07-2013, 11:08 AM
  3. incompatible types
    By mephistochen in forum New To Java
    Replies: 14
    Last Post: 05-20-2013, 11:40 AM
  4. incompatible types?
    By slider57 in forum New To Java
    Replies: 3
    Last Post: 09-26-2012, 12:55 PM
  5. Incompatible types
    By bayan in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 08:43 AM

Posting Permissions

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